Completed
Push — master ( 28afa9...69e2c3 )
by JHONATAN
02:50
created

ProxyFactory::fetchBelongsToMulti()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 6

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 6
crap 2
1
<?php
2
3
namespace Vox\Webservice\Proxy;
4
5
use ProxyManager\Configuration;
6
use ProxyManager\Factory\AccessInterceptorValueHolderFactory;
7
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface;
8
use Vox\Metadata\PropertyMetadata;
9
use Vox\Webservice\Mapping\BelongsTo;
10
use Vox\Webservice\Mapping\HasMany;
11
use Vox\Webservice\Mapping\HasOne;
12
use Vox\Webservice\Metadata\TransferMetadata;
13
use Vox\Webservice\TransferManagerInterface;
14
15
/**
16
 * a proxy factory for the transfers, it uses the ocramius proxy generator
17
 * 
18
 * @author Jhonatan Teixeira <[email protected]>
19
 */
20
class ProxyFactory implements ProxyFactoryInterface
21
{
22
    /**
23
     * @var AccessInterceptorValueHolderFactory
24
     */
25
    private $accessInterceptorFactory;
26
    
27
    /**
28
     * @var Configuration
29
     */
30
    private $proxyConfig;
31
32 20
    public function __construct(Configuration $proxyConfig = null)
33
    {
34 20
        $this->accessInterceptorFactory = new AccessInterceptorValueHolderFactory($proxyConfig);
35 20
        $this->proxyConfig              = $proxyConfig;
36 20
    }
37
    
38 16
    public function createProxy($class, TransferManagerInterface $transferManager): AccessInterceptorValueHolderInterface
39
    {
40 16
        $className = is_object($class) ? get_class($class) : $class;
41 16
        $metadata  = $transferManager->getClassMetadata($className);
42 16
        $object    = is_object($class) ? $class : new $class();
43
44 16
        $interceptors = [];
45
46 16
        foreach ($metadata->propertyMetadata as $name => $config) {
47 16
            $getter = sprintf('get%s', ucfirst($name));
48
49 16
            if (isset($metadata->methodMetadata[$getter])) {
50 16
                $interceptors[$getter] = $this->createGetterInterceptor($metadata, $name, $object, $transferManager);
51
            }
52
        }
53
54 16
        return $this->accessInterceptorFactory->createProxy($object, $interceptors);
55
    }
56
57
    private function createGetterInterceptor(
58
        TransferMetadata $metadata,
59
        string $name,
60
        $object,
61
        TransferManagerInterface $transferManager
62
    ): callable {
63 11
        return function () use ($metadata, $name, $object, $transferManager) {
64
            /* @var $propertyMetadata PropertyMetadata */
65 5
            $propertyMetadata = $metadata->propertyMetadata[$name];
66 5
            $type             = $propertyMetadata->getParsedType();
67
68 5
            if (class_exists($type)) {
69 4
                $this->fetchBelongsTo($metadata, $propertyMetadata, $transferManager, $object, $type);
70 4
                $this->fetchHasOne($metadata, $propertyMetadata, $transferManager, $object, $type);
71 4
                $this->fetchHasMany($metadata, $propertyMetadata, $transferManager, $object, $type);
72
            }
73 11
        };
74
    }
75
    
76 4
    private function fetchBelongsTo(
77
        TransferMetadata $metadata,
78
        PropertyMetadata $propertyMetadata,
79
        TransferManagerInterface $transferManager,
80
        $object,
81
        string $type
82
    ) {
83 4
        $belongsTo = $propertyMetadata->getAnnotation(BelongsTo::class);
84
        
85 4
        if ($belongsTo instanceof BelongsTo && empty($propertyMetadata->getValue($object))) {
86
87 4
            if (is_array($belongsTo->foreignField)) {
88 2
                $this->fetchBelongsToMulti($object, $type, $metadata, $propertyMetadata, $belongsTo, $transferManager);
89
            } else {
90 4
                $this->fetchBelongsToSingle($object, $type, $metadata, $propertyMetadata, $belongsTo, $transferManager);
91
            }
92
        }
93 4
    }
94
95 4
    private function fetchBelongsToSingle(
96
        $object,
97
        string $type,
98
        TransferMetadata $metadata,
99
        PropertyMetadata $propertyMetadata,
100
        BelongsTo $belongsTo,
101
        TransferManagerInterface $transferManager
102
    ) {
103 4
        $idValue  = $metadata->propertyMetadata[$belongsTo->foreignField]->getValue($object);
104
105 4
        if (!$idValue) {
106
            return;
107
        }
108
109
        $data = $transferManager
110 4
            ->find($type, $idValue);
111
112 4
        $propertyMetadata->setValue($object, $data);
113 4
    }
114
115 2 View Code Duplication
    private function fetchBelongsToMulti(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
        $object,
117
        string $type,
118
        TransferMetadata $metadata,
119
        PropertyMetadata $propertyMetadata,
120
        BelongsTo $belongsTo,
121
        TransferManagerInterface $transferManager
122
123
    ) {
124 2
        $criteria = [];
125
126 2
        foreach ($belongsTo->foreignField as $field) {
127 2
            $criteria[$field] = $metadata->propertyMetadata[$field]->getValue($object);
128
        }
129
130 2
        $data = $transferManager->getRepository($type)
131 2
            ->findOneBy($criteria);
132
133 2
        $propertyMetadata->setValue($object, $data);
134 2
    }
135
136 4 View Code Duplication
    private function fetchHasOne(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
        TransferMetadata $metadata,
138
        PropertyMetadata $propertyMetadata,
139
        TransferManagerInterface $transferManager,
140
        $object,
141
        string $type
142
    ) {
143 4
        $hasOne = $propertyMetadata->getAnnotation(HasOne::class);
144 4
        $id     = $metadata->id->getValue($object);
145
        
146 4
        if ($hasOne instanceof HasOne && !empty($id)) {
147 2
            $data = $transferManager->getRepository($type)
148 2
                ->findOneBy([$hasOne->foreignField => $id]);
149
150 2
            $propertyMetadata->setValue($object, $data);
151
        }
152 4
    }
153
154 4 View Code Duplication
    private function fetchHasMany(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
        TransferMetadata $metadata,
156
        PropertyMetadata $propertyMetadata,
157
        TransferManagerInterface $transferManager,
158
        $object,
159
        string $type
160
    ) {
161 4
        $hasMany = $propertyMetadata->getAnnotation(HasMany::class);
162 4
        $id      = $metadata->id->getValue($object);
163
164 4
        if ($hasMany instanceof HasMany && !empty($id)) {
165 2
            $data = $transferManager->getRepository($type)
166 2
                ->findBy([$hasMany->foreignField => $metadata->id->getValue($object)]);
167
168 2
            $propertyMetadata->setValue($object, $data);
169
        }
170 4
    }
171
172 2
    public function registerProxyAutoloader()
173
    {
174 2
        if ($this->proxyConfig) {
175 2
            spl_autoload_register($this->proxyConfig->getProxyAutoloader());
176
        }
177 2
    }
178
}
179