3
3
namespace ReputationVIP \Bundle \QueueClientBundle \Command ;
4
4
5
5
use InvalidArgumentException ;
6
- use Psr \Log \LoggerInterface ;
7
- use ReputationVIP \Bundle \QueueClientBundle \Utils \Output ;
8
6
use ReputationVIP \Bundle \QueueClientBundle \Configuration \QueuesConfiguration ;
9
7
use ReputationVIP \QueueClient \QueueClientInterface ;
10
8
use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
13
11
use Symfony \Component \Console \Input \InputInterface ;
14
12
use Symfony \Component \Console \Input \InputOption ;
15
13
use Symfony \Component \Console \Output \OutputInterface ;
16
- use Symfony \Component \DependencyInjection \Exception \ServiceNotFoundException ;
17
14
use Symfony \Component \Yaml \Yaml ;
18
15
19
16
class CreateQueuesCommand extends ContainerAwareCommand
20
17
{
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
+ }
25
27
26
28
protected function configure ()
27
29
{
@@ -47,42 +49,44 @@ protected function configure()
47
49
}
48
50
49
51
/**
52
+ * @param OutputInterface $output
50
53
* @param QueueClientInterface $queueClient
51
54
* @param string $fileName
55
+ *
52
56
* @return int
53
57
*/
54
- private function createFromFile ($ queueClient , $ fileName )
58
+ private function createFromFile (OutputInterface $ output , QueueClientInterface $ queueClient , $ fileName )
55
59
{
56
60
try {
57
61
$ processor = new Processor ();
58
62
$ configuration = new QueuesConfiguration ();
59
63
$ processedConfiguration = $ processor ->processConfiguration ($ configuration , Yaml::parse (file_get_contents ($ fileName )));
60
64
61
65
} catch (\Exception $ e ) {
62
- $ this -> output ->write ($ e ->getMessage (), Output:: CRITICAL );
66
+ $ output ->writeln ($ e ->getMessage ());
63
67
64
68
return 1 ;
65
69
}
66
70
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. ' );
68
72
foreach ($ processedConfiguration [QueuesConfiguration::QUEUES_NODE ] as $ queue ) {
69
73
$ queueName = $ queue [QueuesConfiguration::QUEUE_NAME_NODE ];
70
74
try {
71
75
$ queueClient ->createQueue ($ queueName );
72
- $ this -> output ->write ('Queue ' . $ queueName . ' created. ' , Output:: INFO );
76
+ $ output ->writeln ('Queue ' . $ queueName . ' created. ' );
73
77
} catch (\Exception $ e ) {
74
- $ this -> output ->write ($ e ->getMessage (), Output:: WARNING );
78
+ $ output ->writeln ($ e ->getMessage ());
75
79
}
76
80
foreach ($ queue [QueuesConfiguration::QUEUE_ALIASES_NODE ] as $ alias ) {
77
81
try {
78
82
$ queueClient ->addAlias ($ queueName , $ alias );
79
- $ this -> output ->write ('Queue alias ' . $ alias . ' -> ' . $ queueName . ' found. ' , Output:: INFO );
83
+ $ output ->writeln ('Queue alias ' . $ alias . ' -> ' . $ queueName . ' found. ' );
80
84
} catch (\Exception $ e ) {
81
- $ this -> output ->write ($ e ->getMessage (), Output:: WARNING );
85
+ $ output ->writeln ($ e ->getMessage ());
82
86
}
83
87
}
84
88
}
85
- $ this -> output ->write ('End create queue. ' , Output:: INFO );
89
+ $ output ->writeln ('End create queue. ' );
86
90
87
91
return 0 ;
88
92
}
@@ -94,34 +98,19 @@ private function createFromFile($queueClient, $fileName)
94
98
*/
95
99
protected function execute (InputInterface $ input , OutputInterface $ output )
96
100
{
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
- }
112
101
if ($ input ->getOption ('file ' )) {
113
102
$ fileName = $ input ->getOption ('file ' );
114
103
115
- return $ this ->createFromFile ($ queueClient , $ fileName );
104
+ return $ this ->createFromFile ($ output , $ this -> queueClient , $ fileName );
116
105
} else {
117
106
$ queues = $ input ->getArgument ('queues ' );
118
107
if (count ($ queues )) {
119
108
foreach ($ queues as $ queue ) {
120
109
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. ' );
123
112
} catch (\Exception $ e ) {
124
- $ this -> output ->write ($ e ->getMessage (), Output:: WARNING );
113
+ $ output ->writeln ($ e ->getMessage ());
125
114
}
126
115
}
127
116
@@ -130,9 +119,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
130
119
try {
131
120
$ fileName = $ this ->getContainer ()->getParameter ('queue_client.queues_file ' );
132
121
133
- return $ this ->createFromFile ($ queueClient , $ fileName );
122
+ return $ this ->createFromFile ($ output , $ this -> queueClient , $ fileName );
134
123
} 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. ' );
136
125
137
126
return 1 ;
138
127
}
0 commit comments