Passed
Branch new_stuff (69e2c3)
by JHONATAN
03:08
created

TransferManagerBuilder::withMetadataPath()   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
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
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\Driver\DriverInterface;
11
use Metadata\MetadataFactory;
12
use ProxyManager\Configuration;
13
use Symfony\Component\Serializer\Encoder\JsonEncoder;
14
use Symfony\Component\Serializer\Encoder\XmlEncoder;
15
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
16
use Symfony\Component\Serializer\Serializer;
17
use Vox\Data\ObjectHydrator;
18
use Vox\Metadata\Driver\AnnotationDriver;
19
use Vox\Metadata\Driver\YmlDriver;
20
use Vox\Serializer\Denormalizer;
21
use Vox\Serializer\Normalizer;
22
use Vox\Serializer\ObjectNormalizer;
23
use Vox\Webservice\ClientRegistryInterface;
24
use Vox\Webservice\Metadata\TransferMetadata;
25
use Vox\Webservice\Proxy\ProxyFactory;
26
use Vox\Webservice\Proxy\ProxyFactoryInterface;
27
use Vox\Webservice\TransferManager;
28
use Vox\Webservice\TransferManagerInterface;
29
use Vox\Webservice\WebserviceClient;
30
31
class TransferManagerBuilder
32
{
33
    /**
34
     * @var ProxyFactoryInterface
35
     */
36
    private $proxyFactory;
37
38
    /**
39
     * @var string
40
     */
41
    private $metadataCache;
42
43
    /**
44
     * @var string
45
     */
46
    private $cacheDir = '/tmp/cache';
47
48
    /**
49
     * @var string
50
     */
51
    private $metadataDriver = 'annotation';
52
53
    /**
54
     * @var ClientRegistryInterface
55
     */
56
    private $clientRegistry;
57
58
    /**
59
     * @var string
60
     */
61
    private $metadataPath;
62
63
    /**
64
     * @var string
65
     */
66
    private $metadataClassName = TransferMetadata::class;
67
68
    /**
69
     * @var bool 
70
     */
71
    private $debug = false;
72
73
    /**
74
     * @var Cache
75
     */
76
    private $doctrineCache;
77
78 2
    public function __construct(Cache $doctrineCache = null)
79
    {
80 2
        $this->doctrineCache = $doctrineCache;
81 2
    }
82
83 2
    private function createMetadataDriver(): DriverInterface
84
    {
85 2
        switch ($this->metadataDriver) {
86 2
            case 'annotation':
87 1
                $driver = new AnnotationDriver(
88 1
                    $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...
89 1
                    $this->metadataClassName
90
                );
91 1
                break;
92 1
            case 'yaml':
93 1
                $driver = new YmlDriver($this->metadataPath, $this->metadataClassName);
94 1
                break;
95
            default:
96
                throw new \RuntimeException('invalid driver provided');
97
        }
98
99 2
        return $driver;
100
    }
101
102 2
    private function createMetadataCache(): CacheInterface
103
    {
104 2
        switch ($this->metadataCache) {
105 2
            case 'file':
106 1
                $cache = new FileCache($this->cacheDir);
107 1
                break;
108 1
            case 'doctrine':
109 1
                $cache = new DoctrineCacheAdapter('metadata', $this->doctrineCache);
110 1
                break;
111
            default:
112
                throw new \RuntimeException('invalid metadata cache chosen');
113
        }
114
115 2
        return $cache;
116
    }
117
118 2
    private function getProxyFactory(): ProxyFactoryInterface
119
    {
120 2
        if (isset($this->proxyFactory)) {
121
            return $this->proxyFactory;
122
        }
123
124 2
        $config = new Configuration();
125 2
        $config->setProxiesTargetDir($this->cacheDir);
126 2
        $proxyFactory = new ProxyFactory($config);
127 2
        $proxyFactory->registerProxyAutoloader();
128
129 2
        return $this->proxyFactory = $proxyFactory;
130
    }
131
132 2
    public function createTransferManager(): TransferManagerInterface
133
    {
134 2
        $driver           = $this->createMetadataDriver();
135 2
        $metadataFactory  = new MetadataFactory($driver, ClassHierarchyMetadata::class, $this->debug);
0 ignored issues
show
Bug introduced by
The type Vox\Webservice\Factory\ClassHierarchyMetadata was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
136 2
        $objectHydrator   = new ObjectHydrator($metadataFactory);
137 2
        $normalizer       = new Normalizer($metadataFactory);
138 2
        $denormalizer     = new Denormalizer($objectHydrator);
139 2
        $objectNormalizer = new ObjectNormalizer($normalizer, $denormalizer);
140 2
        $serializer       = new Serializer(
141 2
            [$objectNormalizer, new DateTimeNormalizer()],
142 2
            [new JsonEncoder(), new XmlEncoder()]
143
        );
144
145 2
        if (null !== $this->metadataCache) {
146 2
            $metadataFactory->setCache($this->createMetadataCache());
147
        }
148
149 2
        if (!isset($this->clientRegistry)) {
150
            throw new \RuntimeException('no client registry added');
151
        }
152
153 2
        $webServiceClient = new WebserviceClient($this->clientRegistry, $metadataFactory, $serializer, $serializer);
154
155 2
        return new TransferManager($metadataFactory, $webServiceClient, $this->getProxyFactory());
156
    }
157
158
    public function withProxyFactory(ProxyFactoryInterface $proxyFactory)
159
    {
160
        $this->proxyFactory = $proxyFactory;
161
162
        return $this;
163
    }
164
165 2
    public function withMetadataCache(string $metadataCache)
166
    {
167 2
        $this->metadataCache = $metadataCache;
168
169 2
        return $this;
170
    }
171
172 2
    public function withCacheDir(string $cacheDir)
173
    {
174 2
        $this->cacheDir = $cacheDir;
175
176 2
        return $this;
177
    }
178
179 2
    public function withMetadataDriver(string $metadataDriver)
180
    {
181 2
        $this->metadataDriver = $metadataDriver;
182
183 2
        return $this;
184
    }
185
186 2
    public function withClientRegistry(ClientRegistryInterface $clientRegistry)
187
    {
188 2
        $this->clientRegistry = $clientRegistry;
189
190 2
        return $this;
191
    }
192
193 1
    public function withMetadataPath(string $metadataPath)
194
    {
195 1
        $this->metadataPath = $metadataPath;
196
197 1
        return $this;
198
    }
199
200 2
    public function withMetadataClassName(string $metadataClassName)
201
    {
202 2
        $this->metadataClassName = $metadataClassName;
203
204 2
        return $this;
205
    }
206
207 2
    public function withDebug(bool $debug)
208
    {
209 2
        $this->debug = $debug;
210
211 2
        return $this;
212
    }
213
}