Passed
Push — master ( e648bb...78c887 )
by JHONATAN
03:51
created

TransferManagerBuilder::withEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Vox\Webservice\Factory;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Cache\Cache;
7
use Metadata\Cache\CacheInterface;
8
use Metadata\Cache\DoctrineCacheAdapter;
9
use Metadata\Cache\FileCache;
10
use Metadata\MetadataFactoryInterface;
11
use ProxyManager\Configuration;
12
use RuntimeException;
13
use Symfony\Component\Serializer\SerializerInterface;
14
use Vox\Metadata\Factory\MetadataFactoryFactory;
15
use Vox\Metadata\Factory\MetadataFactoryFactoryInterface;
16
use Vox\Serializer\Factory\SerializerFactory;
17
use Vox\Serializer\Factory\SerializerFactoryInterface;
18
use Vox\Webservice\ClientRegistryInterface;
19
use Vox\Webservice\EventDispatcherInterface;
20
use Vox\Webservice\Metadata\TransferMetadata;
21
use Vox\Webservice\Proxy\ProxyFactory;
22
use Vox\Webservice\Proxy\ProxyFactoryInterface;
23
use Vox\Webservice\TransferManager;
24
use Vox\Webservice\TransferManagerInterface;
25
use Vox\Webservice\WebserviceClientInterface;
26
27
/**
28
 * Class to help the creation of a TransferManager
29
 * 
30
 * @author Jhonatan Teixeira <[email protected]>
31
 */
32
class TransferManagerBuilder
33
{
34
    /**
35
     * @var ProxyFactoryInterface
36
     */
37
    private $proxyFactory;
38
39
    /**
40
     * @var string
41
     */
42
    private $metadataCache;
43
44
    /**
45
     * @var string
46
     */
47
    private $cacheDir = '/tmp/cache';
48
49
    /**
50
     * @var string
51
     */
52
    private $metadataDriver = 'annotation';
53
54
    /**
55
     * @var ClientRegistryInterface
56
     */
57
    private $clientRegistry;
58
59
    /**
60
     * @var string
61
     */
62
    private $metadataPath;
63
64
    /**
65
     * @var string
66
     */
67
    private $metadataClassName = TransferMetadata::class;
68
69
    /**
70
     * @var bool 
71
     */
72
    private $debug = false;
73
74
    /**
75
     * @var Cache
76
     */
77
    private $doctrineCache;
78
    
79
    /**
80
     * @var SerializerFactoryInterface
81
     */
82
    private $serializerFactory;
83
    
84
    /**
85
     * @var MetadataFactoryFactoryInterface
86
     */
87
    private $metadaFactoryFactory;
88
    
89
    /**
90
     * @var ClientFactory
91
     */
92
    private $clientFactory;
93
    
94
    /**
95
     * @var SerializerInterface
96
     */
97
    private $serializer;
98
    
99
    /**
100
     * @var MetadataFactoryInterface
101
     */
102
    private $metadaFactory;
103
    
104
    /**
105
     * @var WebserviceClientInterface
106
     */
107
    private $webserviceClient;
108
    
109
    /**
110
     * @var EventDispatcherInterface
111
     */
112
    private $eventDispatcher;
113
114 8
    public function __construct(
115
        Cache $doctrineCache = null,
116
        SerializerFactoryInterface $serializerFactory = null,
117
        MetadataFactoryFactoryInterface $metadaFactoryFactory = null,
118
        ClientFactory $clientFactory = null
119
    ) {
120 8
        $this->doctrineCache        = $doctrineCache;
121 8
        $this->serializerFactory    = $serializerFactory ?? new SerializerFactory();
0 ignored issues
show
Documentation Bug introduced by
It seems like $serializerFactory ?? ne...ory\SerializerFactory() can also be of type Vox\Serializer\Factory\SerializerFactory. However, the property $serializerFactory is declared as type Vox\Serializer\Factory\SerializerFactoryInterface. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
122 8
        $this->metadaFactoryFactory = $metadaFactoryFactory ?? new MetadataFactoryFactory();
123 8
        $this->clientFactory        = $clientFactory ?? new ClientFactory();
124 8
    }
125
126 7
    private function createMetadataFactory(): MetadataFactoryInterface
127
    {
128 7
        switch ($this->metadataDriver) {
129 7
            case 'annotation':
130 5
                return $this->metadaFactoryFactory->createAnnotationMetadataFactory(
131 5
                    $this->metadataClassName,
132 5
                    $this->annotationReader ?? new AnnotationReader()
0 ignored issues
show
Bug Best Practice introduced by
The property annotationReader does not exist on Vox\Webservice\Factory\TransferManagerBuilder. Did you maybe forget to declare it?
Loading history...
133
                );
134 2
            case 'yaml':
135 2
                return $this->metadaFactoryFactory
136 2
                    ->createYmlMetadataFactory($this->metadataPath, $this->metadataClassName);
137
            default:
138
                throw new RuntimeException('invalid driver provided');
139
        }
140
    }
141
142 2
    private function createMetadataCache(): CacheInterface
143
    {
144 2
        switch ($this->metadataCache) {
145 2
            case 'file':
146 1
                return new FileCache($this->cacheDir);
147 1
            case 'doctrine':
148 1
                return new DoctrineCacheAdapter('metadata', $this->doctrineCache);
149
            default:
150
                throw new RuntimeException('invalid metadata cache chosen');
151
        }
152
    }
153
154 8
    private function getProxyFactory(): ProxyFactoryInterface
155
    {
156 8
        if (isset($this->proxyFactory)) {
157
            return $this->proxyFactory;
158
        }
159
160 8
        $config = new Configuration();
161 8
        $config->setProxiesTargetDir($this->cacheDir);
162 8
        $proxyFactory = new ProxyFactory($config);
163 8
        $proxyFactory->registerProxyAutoloader();
164
165 8
        return $this->proxyFactory = $proxyFactory;
166
    }
167
168 8
    public function createTransferManager(): TransferManagerInterface
169
    {
170 8
        $metadataFactory = $this->metadaFactory ?? $this->createMetadataFactory();
171 8
        $serializer      = $this->serializer ?? $this->serializerFactory->createSerialzer($metadataFactory);
172
173 8
        if (null !== $this->metadataCache) {
174 2
            $metadataFactory->setCache($this->createMetadataCache());
0 ignored issues
show
Bug introduced by
The method setCache() does not exist on Metadata\MetadataFactoryInterface. It seems like you code against a sub-type of Metadata\MetadataFactoryInterface such as Metadata\MetadataFactory. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
            $metadataFactory->/** @scrutinizer ignore-call */ 
175
                              setCache($this->createMetadataCache());
Loading history...
175
        }
176
177 8
        if (!isset($this->clientRegistry)) {
178
            throw new RuntimeException('no client registry added');
179
        }
180
181 8
        $webServiceClient = $this->webserviceClient ?? $this->clientFactory
182 8
            ->createWebserviceClient($this->clientRegistry, $metadataFactory, $serializer, $serializer);
0 ignored issues
show
Unused Code introduced by
The call to Vox\Webservice\Factory\C...reateWebserviceClient() has too many arguments starting with $serializer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
            ->/** @scrutinizer ignore-call */ createWebserviceClient($this->clientRegistry, $metadataFactory, $serializer, $serializer);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
183
184 8
        return new TransferManager(
185 8
            $metadataFactory,
186 8
            $webServiceClient,
187 8
            $this->getProxyFactory(),
188 8
            $this->eventDispatcher
189
        );
190
    }
191
192
    public function withProxyFactory(ProxyFactoryInterface $proxyFactory)
193
    {
194
        $this->proxyFactory = $proxyFactory;
195
196
        return $this;
197
    }
198
199 2
    public function withMetadataCache(string $metadataCache)
200
    {
201 2
        $this->metadataCache = $metadataCache;
202
203 2
        return $this;
204
    }
205
206 2
    public function withCacheDir(string $cacheDir)
207
    {
208 2
        $this->cacheDir = $cacheDir;
209
210 2
        return $this;
211
    }
212
213 3
    public function withMetadataDriver(string $metadataDriver)
214
    {
215 3
        $this->metadataDriver = $metadataDriver;
216
217 3
        return $this;
218
    }
219
220 8
    public function withClientRegistry(ClientRegistryInterface $clientRegistry)
221
    {
222 8
        $this->clientRegistry = $clientRegistry;
223
224 8
        return $this;
225
    }
226
227 2
    public function withMetadataPath(string $metadataPath)
228
    {
229 2
        $this->metadataPath = $metadataPath;
230
231 2
        return $this;
232
    }
233
234 2
    public function withMetadataClassName(string $metadataClassName)
235
    {
236 2
        $this->metadataClassName = $metadataClassName;
237
238 2
        return $this;
239
    }
240
241 2
    public function withDebug(bool $debug)
242
    {
243 2
        $this->debug = $debug;
244
245 2
        return $this;
246
    }
247
    
248 1
    public function withSerializer(SerializerInterface $serializer)
249
    {
250 1
        $this->serializer = $serializer;
251
        
252 1
        return $this;
253
    }
254
255 1
    public function withMetadaFactory(MetadataFactoryInterface $metadaFactory)
256
    {
257 1
        $this->metadaFactory = $metadaFactory;
258
        
259 1
        return $this;
260
    }
261
    
262 2
    public function withWebserviceClient(WebserviceClientInterface $webserviceClient)
263
    {
264 2
        $this->webserviceClient = $webserviceClient;
265
        
266 2
        return $this;
267
    }
268
    
269 1
    public function withEventDispatcher(EventDispatcherInterface $eventDispatcher)
270
    {
271 1
        $this->eventDispatcher = $eventDispatcher;
272
        
273 1
        return $this;
274
    }
275
}