Completed
Push — master ( 69e2c3...3de1cd )
by JHONATAN
02:37
created

TransferManagerBuilder::createTransferManager()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 0
crap 3.009
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\Metadata\TransferMetadata;
20
use Vox\Webservice\Proxy\ProxyFactory;
21
use Vox\Webservice\Proxy\ProxyFactoryInterface;
22
use Vox\Webservice\TransferManager;
23
use Vox\Webservice\TransferManagerInterface;
24
use Vox\Webservice\WebserviceClientInterface;
25
26
class TransferManagerBuilder
27
{
28
    /**
29
     * @var ProxyFactoryInterface
30
     */
31
    private $proxyFactory;
32
33
    /**
34
     * @var string
35
     */
36
    private $metadataCache;
37
38
    /**
39
     * @var string
40
     */
41
    private $cacheDir = '/tmp/cache';
42
43
    /**
44
     * @var string
45
     */
46
    private $metadataDriver = 'annotation';
47
48
    /**
49
     * @var ClientRegistryInterface
50
     */
51
    private $clientRegistry;
52
53
    /**
54
     * @var string
55
     */
56
    private $metadataPath;
57
58
    /**
59
     * @var string
60
     */
61
    private $metadataClassName = TransferMetadata::class;
62
63
    /**
64
     * @var bool 
65
     */
66
    private $debug = false;
67
68
    /**
69
     * @var Cache
70
     */
71
    private $doctrineCache;
72
    
73
    /**
74
     * @var SerializerFactoryInterface
75
     */
76
    private $serializerFactory;
77
    
78
    /**
79
     * @var MetadataFactoryFactoryInterface
80
     */
81
    private $metadaFactoryFactory;
82
    
83
    /**
84
     * @var ClientFactory
85
     */
86
    private $clientFactory;
87
    
88
    /**
89
     * @var SerializerInterface
90
     */
91
    private $serializer;
92
    
93
    /**
94
     * @var MetadataFactoryInterface
95
     */
96
    private $metadaFactory;
97
    
98
    /**
99
     * @var WebserviceClientInterface
100
     */
101
    private $webserviceClient;
102
103 4
    public function __construct(
104
        Cache $doctrineCache = null,
105
        SerializerFactoryInterface $serializerFactory = null,
106
        MetadataFactoryFactoryInterface $metadaFactoryFactory = null,
107
        ClientFactory $clientFactory = null
108
    ) {
109 4
        $this->doctrineCache        = $doctrineCache;
110 4
        $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...
111 4
        $this->metadaFactoryFactory = $metadaFactoryFactory ?? new MetadataFactoryFactory();
112 4
        $this->clientFactory        = $clientFactory ?? new ClientFactory();
113 4
    }
114
115 3
    private function createMetadataFactory(): MetadataFactoryInterface
116
    {
117 3
        switch ($this->metadataDriver) {
118 3
            case 'annotation':
119 2
                return $this->metadaFactoryFactory->createAnnotationMetadataFactory(
120 2
                    $this->metadataClassName,
121 2
                    $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...
122
                );
123 1
            case 'yaml':
124 1
                return $this->metadaFactoryFactory
125 1
                    ->createYmlMetadataFactory($this->metadataPath, $this->metadataClassName);
126
            default:
127
                throw new RuntimeException('invalid driver provided');
128
        }
129
    }
130
131 2
    private function createMetadataCache(): CacheInterface
132
    {
133 2
        switch ($this->metadataCache) {
134 2
            case 'file':
135 1
                return new FileCache($this->cacheDir);
136 1
            case 'doctrine':
137 1
                return new DoctrineCacheAdapter('metadata', $this->doctrineCache);
138
            default:
139
                throw new RuntimeException('invalid metadata cache chosen');
140
        }
141
    }
142
143 4
    private function getProxyFactory(): ProxyFactoryInterface
144
    {
145 4
        if (isset($this->proxyFactory)) {
146
            return $this->proxyFactory;
147
        }
148
149 4
        $config = new Configuration();
150 4
        $config->setProxiesTargetDir($this->cacheDir);
151 4
        $proxyFactory = new ProxyFactory($config);
152 4
        $proxyFactory->registerProxyAutoloader();
153
154 4
        return $this->proxyFactory = $proxyFactory;
155
    }
156
157 4
    public function createTransferManager(): TransferManagerInterface
158
    {
159 4
        $metadataFactory = $this->metadaFactory ?? $this->createMetadataFactory();
160 4
        $serializer      = $this->serializer ?? $this->serializerFactory->createSerialzer($metadataFactory);
161
162 4
        if (null !== $this->metadataCache) {
163 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

163
            $metadataFactory->/** @scrutinizer ignore-call */ 
164
                              setCache($this->createMetadataCache());
Loading history...
164
        }
165
166 4
        if (!isset($this->clientRegistry)) {
167
            throw new RuntimeException('no client registry added');
168
        }
169
170 4
        $webServiceClient = $this->webserviceClient ?? $this->clientFactory
171 4
            ->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

171
            ->/** @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...
172
173 4
        return new TransferManager($metadataFactory, $webServiceClient, $this->getProxyFactory());
174
    }
175
176
    public function withProxyFactory(ProxyFactoryInterface $proxyFactory)
177
    {
178
        $this->proxyFactory = $proxyFactory;
179
180
        return $this;
181
    }
182
183 2
    public function withMetadataCache(string $metadataCache)
184
    {
185 2
        $this->metadataCache = $metadataCache;
186
187 2
        return $this;
188
    }
189
190 2
    public function withCacheDir(string $cacheDir)
191
    {
192 2
        $this->cacheDir = $cacheDir;
193
194 2
        return $this;
195
    }
196
197 2
    public function withMetadataDriver(string $metadataDriver)
198
    {
199 2
        $this->metadataDriver = $metadataDriver;
200
201 2
        return $this;
202
    }
203
204 4
    public function withClientRegistry(ClientRegistryInterface $clientRegistry)
205
    {
206 4
        $this->clientRegistry = $clientRegistry;
207
208 4
        return $this;
209
    }
210
211 1
    public function withMetadataPath(string $metadataPath)
212
    {
213 1
        $this->metadataPath = $metadataPath;
214
215 1
        return $this;
216
    }
217
218 2
    public function withMetadataClassName(string $metadataClassName)
219
    {
220 2
        $this->metadataClassName = $metadataClassName;
221
222 2
        return $this;
223
    }
224
225 2
    public function withDebug(bool $debug)
226
    {
227 2
        $this->debug = $debug;
228
229 2
        return $this;
230
    }
231
    
232 1
    public function withSerializer(SerializerInterface $serializer)
233
    {
234 1
        $this->serializer = $serializer;
235
        
236 1
        return $this;
237
    }
238
239 1
    public function withMetadaFactory(MetadataFactoryInterface $metadaFactory)
240
    {
241 1
        $this->metadaFactory = $metadaFactory;
242
        
243 1
        return $this;
244
    }
245
    
246 1
    public function withWebserviceClient(WebserviceClientInterface $webserviceClient)
247
    {
248 1
        $this->webserviceClient = $webserviceClient;
249
        
250 1
        return $this;
251
    }
252
}