33namespace ReputationVIP \Bundle \QueueClientBundle \Command ;
44
55use InvalidArgumentException ;
6- use Psr \Log \LoggerInterface ;
7- use ReputationVIP \Bundle \QueueClientBundle \Utils \Output ;
86use ReputationVIP \Bundle \QueueClientBundle \Configuration \QueuesConfiguration ;
97use ReputationVIP \QueueClient \QueueClientInterface ;
108use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
1311use Symfony \Component \Console \Input \InputInterface ;
1412use Symfony \Component \Console \Input \InputOption ;
1513use Symfony \Component \Console \Output \OutputInterface ;
16- use Symfony \Component \DependencyInjection \Exception \ServiceNotFoundException ;
1714use Symfony \Component \Yaml \Yaml ;
1815
1916class CreateQueuesCommand extends ContainerAwareCommand
2017{
21- /**
22- * @var Output $output
23- */
24- private $ output ;
18+ /** @var QueueClientInterface */
19+ private $ queueClient ;
20+
21+ public function __construct (QueueClientInterface $ queueClient )
22+ {
23+ parent ::__construct ();
24+
25+ $ this ->queueClient = $ queueClient ;
26+ }
2527
2628 protected function configure ()
2729 {
@@ -47,42 +49,44 @@ protected function configure()
4749 }
4850
4951 /**
52+ * @param OutputInterface $output
5053 * @param QueueClientInterface $queueClient
5154 * @param string $fileName
55+ *
5256 * @return int
5357 */
54- private function createFromFile ($ queueClient , $ fileName )
58+ private function createFromFile (OutputInterface $ output , QueueClientInterface $ queueClient , $ fileName )
5559 {
5660 try {
5761 $ processor = new Processor ();
5862 $ configuration = new QueuesConfiguration ();
5963 $ processedConfiguration = $ processor ->processConfiguration ($ configuration , Yaml::parse (file_get_contents ($ fileName )));
6064
6165 } catch (\Exception $ e ) {
62- $ this -> output ->write ($ e ->getMessage (), Output:: CRITICAL );
66+ $ output ->writeln ($ e ->getMessage ());
6367
6468 return 1 ;
6569 }
6670 array_walk_recursive ($ processedConfiguration , 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters ' , $ this ->getContainer ());
67- $ this -> output ->write ('Start create queue. ' , Output:: INFO );
71+ $ output ->writeln ('Start create queue. ' );
6872 foreach ($ processedConfiguration [QueuesConfiguration::QUEUES_NODE ] as $ queue ) {
6973 $ queueName = $ queue [QueuesConfiguration::QUEUE_NAME_NODE ];
7074 try {
7175 $ queueClient ->createQueue ($ queueName );
72- $ this -> output ->write ('Queue ' . $ queueName . ' created. ' , Output:: INFO );
76+ $ output ->writeln ('Queue ' . $ queueName . ' created. ' );
7377 } catch (\Exception $ e ) {
74- $ this -> output ->write ($ e ->getMessage (), Output:: WARNING );
78+ $ output ->writeln ($ e ->getMessage ());
7579 }
7680 foreach ($ queue [QueuesConfiguration::QUEUE_ALIASES_NODE ] as $ alias ) {
7781 try {
7882 $ queueClient ->addAlias ($ queueName , $ alias );
79- $ this -> output ->write ('Queue alias ' . $ alias . ' -> ' . $ queueName . ' found. ' , Output:: INFO );
83+ $ output ->writeln ('Queue alias ' . $ alias . ' -> ' . $ queueName . ' found. ' );
8084 } catch (\Exception $ e ) {
81- $ this -> output ->write ($ e ->getMessage (), Output:: WARNING );
85+ $ output ->writeln ($ e ->getMessage ());
8286 }
8387 }
8488 }
85- $ this -> output ->write ('End create queue. ' , Output:: INFO );
89+ $ output ->writeln ('End create queue. ' );
8690
8791 return 0 ;
8892 }
@@ -94,34 +98,19 @@ private function createFromFile($queueClient, $fileName)
9498 */
9599 protected function execute (InputInterface $ input , OutputInterface $ output )
96100 {
97- try {
98- /** @var LoggerInterface $logger */
99- $ logger = $ this ->getContainer ()->get ('logger ' );
100- } catch (ServiceNotFoundException $ e ) {
101- $ logger = null ;
102- }
103- $ this ->output = new Output ($ logger , $ output );
104- try {
105- /** @var QueueClientInterface $queueClient */
106- $ queueClient = $ this ->getContainer ()->get ('queue_client ' );
107- } catch (ServiceNotFoundException $ e ) {
108- $ this ->output ->write ('No queue client service found. ' , Output::CRITICAL );
109-
110- return 1 ;
111- }
112101 if ($ input ->getOption ('file ' )) {
113102 $ fileName = $ input ->getOption ('file ' );
114103
115- return $ this ->createFromFile ($ queueClient , $ fileName );
104+ return $ this ->createFromFile ($ output , $ this -> queueClient , $ fileName );
116105 } else {
117106 $ queues = $ input ->getArgument ('queues ' );
118107 if (count ($ queues )) {
119108 foreach ($ queues as $ queue ) {
120109 try {
121- $ queueClient ->createQueue ($ queue );
122- $ this -> output ->write ('Queue ' . $ queue . ' created. ' , Output:: INFO );
110+ $ this -> queueClient ->createQueue ($ queue );
111+ $ output ->writeln ('Queue ' . $ queue . ' created. ' );
123112 } catch (\Exception $ e ) {
124- $ this -> output ->write ($ e ->getMessage (), Output:: WARNING );
113+ $ output ->writeln ($ e ->getMessage ());
125114 }
126115 }
127116
@@ -130,9 +119,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
130119 try {
131120 $ fileName = $ this ->getContainer ()->getParameter ('queue_client.queues_file ' );
132121
133- return $ this ->createFromFile ($ queueClient , $ fileName );
122+ return $ this ->createFromFile ($ output , $ this -> queueClient , $ fileName );
134123 } catch (InvalidArgumentException $ e ) {
135- $ this -> output ->write ('No queue_client.queues_file parameter found. ' , Output:: CRITICAL );
124+ $ output ->writeln ('No queue_client.queues_file parameter found. ' );
136125
137126 return 1 ;
138127 }
0 commit comments