JobManager::createSerializer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 0
1
<?php
2
namespace Workana\AsyncJobs;
3
4
use Bernard\Driver;
5
use Bernard\Serializer;
6
use Bernard\QueueFactory\PersistentFactory;
7
use Interop\Container\ContainerInterface;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
use Normalt\Normalizer\AggregateNormalizer;
10
use Workana\AsyncJobs\Dispatcher\AsyncJobDispatcher;
11
use Bernard\Normalizer\EnvelopeNormalizer;
12
use Workana\AsyncJobs\Doctrine\QueueableEntityNormalizer;
13
use Workana\AsyncJobs\Normalizer\AsyncActionNormalizer;
14
use Workana\AsyncJobs\Normalizer\ParameterNormalizer;
15
use Workana\AsyncJobs\Normalizer\ScalarNormalizer;
16
use Workana\AsyncJobs\Normalizer\SerializableEventNormalizer;
17
18
/**
19
 * Job Manager
20
 *
21
 * @author Carlos Frutos <[email protected]>
22
 */
23
class JobManager
24
{
25
    /**
26
     * @var Driver
27
     */
28
    private $driver;
29
30
    /**
31
     * @var Configuration
32
     */
33
    private $config;
34
35
    /**
36
     * @var ContainerInterface
37
     */
38
    private $container;
39
40
    /**
41
     * @var EventDispatcherInterface
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @var Bernard\QueueFactory
47
     */
48
    private $queueFactory;
49
50
    /**
51
     * @var Bernard\Router
52
     */
53
    private $router;
54
55
    /**
56
     * @var JobDispatcher
57
     */
58
    private $jobDispatcher;
59
60
    /**
61
     * Creates a new job manager
62
     *
63
     * @param Configuration $config
64
     * @param ContainerInterface $container
65
     */
66
    public function __construct(Configuration $config, ContainerInterface $container)
67
    {
68
        $this->driver = $container->get($config->getDriverClass());
69
        $this->config = $config;
70
        $this->container = $container;
71
        $this->eventDispatcher = new EventDispatcher();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\E...tcher\EventDispatcher() of type object<Symfony\Component...atcher\EventDispatcher> is incompatible with the declared type object<Workana\AsyncJobs...entDispatcherInterface> of property $eventDispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
73
        $serializer = $this->createSerializer();
74
        $this->queueFactory = new PersistentFactory($this->driver, $serializer);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Bernard\QueueFactor...s->driver, $serializer) of type object<Bernard\QueueFactory\PersistentFactory> is incompatible with the declared type object<Workana\AsyncJobs\Bernard\QueueFactory> of property $queueFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
76
        $this->router = $this->container->get($config->getRouterClass());
77
        $this->jobDispatcher = $this->createDispatcher();
78
    }
79
80
    /**
81
     * @return JobDispatcher
82
     */
83
    private function createDispatcher()
84
    {
85
        if ($this->config->isSync()) {
86
            throw new \Exception('Not implemented yed: sync mode');
87
        } else {
88
            return new AsyncJobDispatcher($this);
89
        }
90
    }
91
92
    /**
93
     * Creates serializer based on declared normalizers
94
     *
95
     * @return Serializer
96
     */
97
    private function createSerializer()
98
    {
99
        $normalizerClasses = array_merge([
100
            EnvelopeNormalizer::class,
101
            SerializableEventNormalizer::class,
102
            AsyncActionNormalizer::class,
103
            ParameterNormalizer::class,
104
            ScalarNormalizer::class
105
        ], $this->config->getNormalizerClasses());
106
107
        if ($this->config->isUsingDoctrine()) {
108
            $normalizerClasses[] = QueueableEntityNormalizer::class;
109
        }
110
111
        $normalizers = array_map([$this->container, 'get'], $normalizerClasses);
112
        $aggregate = new AggregateNormalizer($normalizers);
113
114
        return new Serializer($aggregate);
115
    }
116
117
    /**
118
     * Creates a new worker builder
119
     *
120
     * @return WorkerBuilder
121
     */
122
    public function createWorkerBuilder()
123
    {
124
        return new WorkerBuilder($this);
125
    }
126
127
    /**
128
     * Dispatch a job
129
     */
130
    public function dispatch(Job $job)
131
    {
132
        $this->jobDispatcher->dispatch($job);
133
    }
134
135
    /**
136
     * @return Configuration
137
     */
138
    public function getConfiguration()
139
    {
140
        return $this->config;
141
    }
142
143
    /**
144
     * Get driver
145
     *
146
     * @return Driver
147
     */
148
    public function getDriver()
149
    {
150
        return $this->driver;
151
    }
152
153
    /**
154
     * @return ContainerInterface
155
     */
156
    public function getContainer()
157
    {
158
        return $this->container;
159
    }
160
161
    /**
162
     * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
163
     */
164
    public function getEventDispatcher()
165
    {
166
        return $this->eventDispatcher;
167
    }
168
169
    /**
170
     * @return \Bernard\QueueFactory
171
     */
172
    public function getQueueFactory()
173
    {
174
        return $this->queueFactory;
175
    }
176
177
    /**
178
     * @return \Bernard\Router
179
     */
180
    public function getRouter()
181
    {
182
        return $this->router;
183
    }
184
}