QueueExtension::createJobProcess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
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\Command\RetryCommand;
8
use SfCod\QueueBundle\Command\RunJobCommand;
9
use SfCod\QueueBundle\Command\WorkCommand;
10
use SfCod\QueueBundle\Failer\FailedJobProviderInterface;
11
use SfCod\QueueBundle\Handler\ExceptionHandler;
12
use SfCod\QueueBundle\Handler\ExceptionHandlerInterface;
13
use SfCod\QueueBundle\Service\JobProcess;
14
use SfCod\QueueBundle\Service\JobQueue;
15
use SfCod\QueueBundle\Service\JobResolver;
16
use SfCod\QueueBundle\Service\QueueManager;
17
use SfCod\QueueBundle\Worker\Worker;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Reference;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
/**
25
 * Class SfcodQueueExtension
26
 *
27
 * @author Alexey Orlov <[email protected]>
28
 * @author Virchenko Maksim <[email protected]>
29
 *
30
 * @package SfCod\QueueBundle\DependencyInjection
31
 */
32
class QueueExtension extends Extension
33
{
34
    /**
35
     * Loads a specific configuration.
36
     *
37
     * @param array $config
38
     * @param ContainerBuilder $container
39
     *
40
     * @throws \ReflectionException
41
     */
42
    public function load(array $config, ContainerBuilder $container)
43
    {
44
        $configuration = new QueueConfiguration();
45
46
        $config = $this->processConfiguration($configuration, $config);
47
48
        $jobs = $this->grabJobs($config, $container);
0 ignored issues
show
Deprecated Code introduced by
The method SfCod\QueueBundle\Depend...ueExtension::grabJobs() has been deprecated with message: will be removed some day, use services with tag "sfcod.jobqueue.job"

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
49
        foreach ($jobs as $job) {
50
            $definition = new Definition($job);
51
            $definition
52
                ->setAutowired(true)
53
                ->setAutoconfigured(true)
54
                ->setPublic(true)
55
                ->addTag('sfcod.jobqueue.job');
56
            $container->setDefinition($job, $definition);
57
        }
58
59
        $this->createJobQueue($config, $container);
60
        $this->createWorker($config, $container);
61
        $this->createJobProcess($config, $container);
62
        $this->createCommands($config, $container);
63
        $this->createManager($config, $container);
64
    }
65
66
    /**
67
     * Get extension alias
68
     *
69
     * @return string
70
     */
71
    public function getAlias()
72
    {
73
        return 'sfcod_queue';
74
    }
75
76
    /**
77
     * @deprecated will be removed some day, use services with tag "sfcod.jobqueue.job"
78
     *
79
     * @param array $config
80
     * @param ContainerBuilder $container
81
     *
82
     * @return array
83
     */
84
    private function grabJobs(array $config, ContainerBuilder $container): array
85
    {
86
        $jobs = [];
87
        foreach ($config['namespaces'] as $key => $namespace) {
88
            $alias = $container->getParameter('kernel.root_dir') . '/../' . str_replace('\\', DIRECTORY_SEPARATOR, trim($namespace, '\\'));
89
90
            foreach (glob(sprintf('%s/**.php', $alias)) as $file) {
91
                $className = sprintf('%s\%s', $namespace, basename($file, '.php'));
92
                if (method_exists($className, 'fire')) {
93
                    $jobs[] = $className;
94
                }
95
            }
96
        }
97
98
        return $jobs;
99
    }
100
101
    /**
102
     * Create command
103
     *
104
     * @param array $config
105
     * @param ContainerBuilder $container
106
     */
107
    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...
108
    {
109
        $work = new Definition(WorkCommand::class);
110
        $work->setArguments([
111
            new Reference(Worker::class),
112
        ]);
113
        $work->addTag('console.command');
114
115
        $retry = new Definition(RetryCommand::class);
116
        $retry->setArguments([
117
            new Reference(JobQueue::class),
118
            new Reference(FailedJobProviderInterface::class),
119
        ]);
120
        $retry->addTag('console.command');
121
122
        $runJob = new Definition(RunJobCommand::class);
123
        $runJob->setArguments([
124
            new Reference(Worker::class),
125
        ]);
126
        $runJob->addTag('console.command');
127
128
        $container->addDefinitions([
129
            WorkCommand::class => $work,
130
            RetryCommand::class => $retry,
131
            RunJobCommand::class => $runJob,
132
        ]);
133
    }
134
135
    /**
136
     * Create queue manager
137
     *
138
     * @param array $config
139
     * @param ContainerBuilder $container
140
     *
141
     * @throws \Exception
142
     */
143
    private function createManager(array $config, ContainerBuilder $container)
144
    {
145
        $resolver = new Definition(JobResolverInterface::class);
146
        $resolver->setClass(JobResolver::class);
147
148
        $manager = new Definition(QueueManager::class);
149
150
        foreach ($config['drivers'] as $name => $service) {
151
            $manager->addMethodCall('addConnector', [
152
                $name,
153
                new Reference(ltrim($service, '@')),
154
            ]);
155
        }
156
157
        foreach ($config['connections'] as $name => $params) {
158
            $manager->addMethodCall('addConnection', [
159
                $params,
160
                $name,
161
            ]);
162
        }
163
164
        $container->addDefinitions([
165
            JobResolverInterface::class => $resolver,
166
            QueueManager::class => $manager,
167
        ]);
168
    }
169
170
    /**
171
     * Create job queue
172
     *
173
     * @param array $config
174
     * @param ContainerBuilder $container
175
     */
176
    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...
177
    {
178
        $jobQueue = new Definition(JobQueue::class);
179
        $jobQueue->setPublic(true);
180
        $jobQueue->setArguments([
181
            new Reference(QueueManager::class),
182
        ]);
183
184
        $container->setDefinition(JobQueue::class, $jobQueue);
185
    }
186
187
    /**
188
     * Create worker
189
     *
190
     * @param array $config
191
     * @param ContainerBuilder $container
192
     */
193
    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...
194
    {
195
        $worker = new Definition(Worker::class);
196
        $worker
197
            ->setArguments([
198
                new Reference(QueueManager::class),
199
                new Reference(JobProcess::class),
200
                new Reference(FailedJobProviderInterface::class),
201
                new Reference(ExceptionHandlerInterface::class),
202
                new Reference(EventDispatcherInterface::class),
203
            ]);
204
205
        $exceptionHandler = new Definition(ExceptionHandlerInterface::class);
206
        $exceptionHandler
207
            ->setClass(ExceptionHandler::class)
208
            ->setArguments([
209
                new Reference(LoggerInterface::class),
210
            ]);
211
212
        $container->addDefinitions([
213
            Worker::class => $worker,
214
            ExceptionHandlerInterface::class => $exceptionHandler,
215
        ]);
216
    }
217
218
    /**
219
     * Create job process
220
     *
221
     * @param array $config
222
     * @param ContainerBuilder $container
223
     */
224
    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...
225
    {
226
        $jobProcess = new Definition(JobProcess::class);
227
        $jobProcess->setArguments([
228
            'console',
229
            sprintf('%s/bin', $container->getParameter('kernel.project_dir')),
230
            $container->getParameter('kernel.environment'),
231
        ]);
232
233
        $container->setDefinition(JobProcess::class, $jobProcess);
234
    }
235
}
236