Completed
Pull Request — master (#2)
by
unknown
38:19
created

QueueExtension::createManager()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 2
1
<?php
2
3
namespace SfCod\QueueBundle\DependencyInjection;
4
5
use Psr\Log\LoggerInterface;
6
use SfCod\QueueBundle\Base\JobResolverInterface;
7
use SfCod\QueueBundle\Base\MongoDriverInterface;
8
use SfCod\QueueBundle\Command\RetryCommand;
9
use SfCod\QueueBundle\Command\RunJobCommand;
10
use SfCod\QueueBundle\Command\WorkCommand;
11
use SfCod\QueueBundle\Connector\ConnectorInterface;
12
use SfCod\QueueBundle\Connector\MongoConnector;
13
use SfCod\QueueBundle\Failer\FailedJobProviderInterface;
14
use SfCod\QueueBundle\Failer\MongoFailedJobProvider;
15
use SfCod\QueueBundle\Handler\ExceptionHandler;
16
use SfCod\QueueBundle\Handler\ExceptionHandlerInterface;
17
use SfCod\QueueBundle\Service\JobProcess;
18
use SfCod\QueueBundle\Service\JobQueue;
19
use SfCod\QueueBundle\Service\JobResolver;
20
use SfCod\QueueBundle\Service\MongoDriver;
21
use SfCod\QueueBundle\Service\QueueManager;
22
use SfCod\QueueBundle\Worker\Worker;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use Symfony\Component\DependencyInjection\Definition;
26
use Symfony\Component\DependencyInjection\Reference;
27
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
29
30
/**
31
 * Class SfcodQueueExtension
32
 *
33
 * @author Alexey Orlov <[email protected]>
34
 * @author Virchenko Maksim <[email protected]>
35
 *
36
 * @package SfCod\QueueBundle\DependencyInjection
37
 */
38
class QueueExtension extends Extension
39
{
40
    /**
41
     * Loads a specific configuration.
42
     *
43
     * @param array $config
44
     * @param ContainerBuilder $container
45
     *
46
     * @throws \ReflectionException
47
     */
48
    public function load(array $config, ContainerBuilder $container)
49
    {
50
        $configuration = new QueueConfiguration();
51
52
        $config = $this->processConfiguration($configuration, $config);
53
54
        $jobs = $this->grabJobs($config, $container);
55
56
        foreach ($jobs as $job) {
57
            $definition = new Definition($job);
58
            $definition
59
                ->setAutowired(true)
60
                ->setAutoconfigured(true)
61
                ->setPublic(true);
62
            $container->setDefinition($job, $definition);
63
        }
64
65
        $this->createDriver($config, $container);
66
        $this->createJobQueue($config, $container);
67
        $this->createWorker($config, $container);
68
        $this->createJobProcess($config, $container);
69
        $this->createCommands($config, $container);
70
        $this->createManager($config, $container);
71
    }
72
73
    /**
74
     * Get extension alias
75
     *
76
     * @return string
77
     */
78
    public function getAlias()
79
    {
80
        return 'sfcod_queue';
81
    }
82
83
    /**
84
     * @param array $config
85
     *
86
     * @return array
87
     */
88
    private function grabJobs(array $config, ContainerBuilder $container): array
89
    {
90
        $jobs = [];
91
        foreach ($config['namespaces'] as $key => $namespace) {
92
            $alias = $container->getParameter('kernel.root_dir') . '/../' . str_replace('\\', DIRECTORY_SEPARATOR, trim($namespace, '\\'));
93
94
            foreach (glob(sprintf('%s/**.php', $alias)) as $file) {
95
                $className = sprintf('%s\%s', $namespace, basename($file, '.php'));
96
                if (method_exists($className, 'fire')) {
97
                    $jobs[] = $className;
98
                }
99
            }
100
        }
101
102
        return $jobs;
103
    }
104
105
    /**
106
     * Create command
107
     *
108
     * @param array $config
109
     * @param ContainerBuilder $container
110
     */
111
    private function createCommands(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        $work = new Definition(WorkCommand::class);
114
        $work->setArguments([
115
            new Reference(Worker::class),
116
        ]);
117
        $work->addTag('console.command');
118
119
        $retry = new Definition(RetryCommand::class);
120
        $retry->setArguments([
121
            new Reference(JobQueue::class),
122
            new Reference(FailedJobProviderInterface::class),
123
        ]);
124
        $retry->addTag('console.command');
125
126
        $runJob = new Definition(RunJobCommand::class);
127
        $runJob->setArguments([
128
            new Reference(Worker::class),
129
        ]);
130
        $runJob->addTag('console.command');
131
132
        $container->addDefinitions([
133
            WorkCommand::class => $work,
134
            RetryCommand::class => $retry,
135
            RunJobCommand::class => $runJob,
136
        ]);
137
    }
138
139
    /**
140
     * Create queue manager
141
     *
142
     * @param array $config
143
     * @param ContainerBuilder $container
144
     */
145
    private function createManager(array $config, ContainerBuilder $container)
146
    {
147
        $resolver = new Definition(JobResolverInterface::class);
148
        $resolver->setClass(JobResolver::class);
149
        $resolver->setArguments([
150
            new Reference(ContainerInterface::class),
151
        ]);
152
153
        $connector = new Definition(ConnectorInterface::class);
154
        $connector->setClass(MongoConnector::class);
155
        $connector->setArguments([
156
            new Reference(JobResolverInterface::class),
157
            new Reference(MongoDriverInterface::class),
158
        ]);
159
160
        $manager = new Definition(QueueManager::class);
161
        $manager->addMethodCall('addConnector', [
162
            'mongo-thread',
163
            new Reference(ConnectorInterface::class),
164
        ]);
165
166
        foreach ($config['connections'] as $name => $params) {
167
            $manager->addMethodCall('addConnection', [
168
                $params,
169
                $name,
170
            ]);
171
        }
172
173
        $container->addDefinitions([
174
            JobResolverInterface::class => $resolver,
175
            ConnectorInterface::class => $connector,
176
            QueueManager::class => $manager,
177
        ]);
178
    }
179
180
    /**
181
     * Create driver
182
     *
183
     * @param array $config
184
     * @param ContainerBuilder $container
185
     */
186
    private function createDriver(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
187
    {
188
        $mongo = new Definition(MongoDriverInterface::class);
189
        $mongo->setClass(MongoDriver::class);
190
        $mongo->addMethodCall('setCredentials', [
191
            getenv('MONGODB_URL'),
192
        ]);
193
        $mongo->addMethodCall('setDbname', [
194
            getenv('MONGODB_DB'),
195
        ]);
196
197
        $container->setDefinition(MongoDriverInterface::class, $mongo);
198
    }
199
200
    /**
201
     * Create job queue
202
     *
203
     * @param array $config
204
     * @param ContainerBuilder $container
205
     */
206
    private function createJobQueue(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
    {
208
        $jobQueue = new Definition(JobQueue::class);
209
        $jobQueue->setPublic(true);
210
        $jobQueue->setArguments([
211
            new Reference(QueueManager::class),
212
        ]);
213
214
        $container->setDefinition(JobQueue::class, $jobQueue);
215
    }
216
217
    /**
218
     * Create worker
219
     *
220
     * @param array $config
221
     * @param ContainerBuilder $container
222
     */
223
    private function createWorker(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
224
    {
225
        $worker = new Definition(Worker::class);
226
        $worker
227
            ->setArguments([
228
                new Reference(QueueManager::class),
229
                new Reference(JobProcess::class),
230
                new Reference(FailedJobProviderInterface::class),
231
                new Reference(ExceptionHandlerInterface::class),
232
                new Reference(EventDispatcherInterface::class),
233
            ]);
234
235
        $failedProvider = new Definition(FailedJobProviderInterface::class);
236
        $failedProvider
237
            ->setClass(MongoFailedJobProvider::class)
238
            ->setArguments([
239
                new Reference(MongoDriverInterface::class),
240
                'queue_jobs_failed',
241
            ]);
242
243
        $exceptionHandler = new Definition(ExceptionHandlerInterface::class);
244
        $exceptionHandler
245
            ->setClass(ExceptionHandler::class)
246
            ->setArguments([
247
                new Reference(LoggerInterface::class),
248
            ]);
249
250
        $container->addDefinitions([
251
            Worker::class => $worker,
252
            FailedJobProviderInterface::class => $failedProvider,
253
            ExceptionHandlerInterface::class => $exceptionHandler,
254
        ]);
255
    }
256
257
    /**
258
     * Create job process
259
     *
260
     * @param array $config
261
     * @param ContainerBuilder $container
262
     */
263
    private function createJobProcess(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
264
    {
265
        $jobProcess = new Definition(JobProcess::class);
266
        $jobProcess->setArguments([
267
            'console',
268
            sprintf('%s/bin', $container->getParameter('kernel.project_dir')),
269
        ]);
270
271
        $container->setDefinition(JobProcess::class, $jobProcess);
272
    }
273
}
274