1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vox\Webservice; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Vox\Webservice\Exception\WebserviceResponseException; |
8
|
|
|
use Vox\Webservice\Proxy\ProxyFactoryInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* the transfer repository does the job of requiring data from the webservice client for the correct transfer |
12
|
|
|
* however this pattern should be more flexible, a future release will fix this |
13
|
|
|
* |
14
|
|
|
* @author Jhonatan Teixeira <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class TransferRepository implements TransferRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
private $transferName; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var WebserviceClientInterface |
22
|
|
|
*/ |
23
|
|
|
private $webserviceClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ObjectStorageInterface |
27
|
|
|
*/ |
28
|
|
|
private $objectStorage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var TransferManagerInterface |
32
|
|
|
*/ |
33
|
|
|
private $transferManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ProxyFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $proxyFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $transferName |
42
|
|
|
* @param WebserviceClientInterface $webserviceClient |
43
|
|
|
* @param ObjectStorageInterface $objectStorage |
44
|
|
|
* @param TransferManagerInterface $transferManager |
45
|
|
|
* @param ProxyFactoryInterface $proxyFactory |
46
|
|
|
*/ |
47
|
21 |
|
public function __construct( |
48
|
|
|
string $transferName, |
49
|
|
|
WebserviceClientInterface $webserviceClient, |
50
|
|
|
ObjectStorageInterface $objectStorage, |
51
|
|
|
TransferManagerInterface $transferManager, |
52
|
|
|
ProxyFactoryInterface $proxyFactory |
53
|
|
|
) { |
54
|
21 |
|
$this->transferName = $transferName; |
55
|
21 |
|
$this->webserviceClient = $webserviceClient; |
56
|
21 |
|
$this->objectStorage = $objectStorage; |
57
|
21 |
|
$this->transferManager = $transferManager; |
58
|
21 |
|
$this->proxyFactory = $proxyFactory; |
59
|
21 |
|
} |
60
|
|
|
|
61
|
11 |
|
public function find($id) |
62
|
|
|
{ |
63
|
11 |
|
$transfer = $this->objectStorage->fetchByParams($this->transferName, $id) |
64
|
11 |
|
?? $this->proxyFactory |
65
|
11 |
|
->createProxy($this->webserviceClient->get($this->transferName, $id), $this->transferManager); |
66
|
|
|
|
67
|
11 |
|
if ($transfer && !$this->objectStorage->contains($transfer)) { |
68
|
11 |
|
$this->objectStorage->attach($transfer); |
69
|
|
|
} |
70
|
|
|
|
71
|
11 |
|
return $transfer; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function findAll(): Collection |
75
|
|
|
{ |
76
|
2 |
|
$collection = $this->webserviceClient->cGet($this->transferName); |
77
|
|
|
|
78
|
2 |
|
$this->prepareCollection($collection); |
79
|
|
|
|
80
|
2 |
|
return $collection; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null): Collection |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
3 |
|
$collection = $this->webserviceClient->cGet($this->transferName, $criteria); |
87
|
1 |
|
} catch (WebserviceResponseException $exception) { |
88
|
1 |
|
if ($exception->getCode() == '404') { |
89
|
1 |
|
return new ArrayCollection(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw $exception; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$this->prepareCollection($collection); |
96
|
|
|
|
97
|
2 |
|
return $collection; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function findOneBy(array $criteria) |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
2 |
|
$collection = $this->webserviceClient->cGet($this->transferName, $criteria); |
104
|
1 |
|
} catch (WebserviceResponseException $exception) { |
105
|
1 |
|
if ($exception->getCode() == '404') { |
106
|
1 |
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
throw $exception; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
$this->prepareCollection($collection); |
113
|
|
|
|
114
|
1 |
|
if ($collection->count() > 0) { |
115
|
1 |
|
return $collection->first(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
public function findByCriteria(CriteriaInterface $criteria): Collection |
120
|
|
|
{ |
121
|
2 |
|
$criteria->withOperationType(CriteriaInterface::OPERATION_TYPE_COLLECTION); |
122
|
|
|
|
123
|
|
|
try { |
124
|
2 |
|
$collection = $this->webserviceClient->getByCriteria($criteria, $this->transferName); |
125
|
1 |
|
} catch (WebserviceResponseException $ex) { |
126
|
1 |
|
if ($ex->getCode() == '404') { |
127
|
1 |
|
return new ArrayCollection(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
throw $ex; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
$this->prepareCollection($collection); |
134
|
|
|
|
135
|
1 |
|
return $collection; |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
public function findOneByCriteria(CriteriaInterface $criteria) |
139
|
|
|
{ |
140
|
2 |
|
$criteria->withOperationType(CriteriaInterface::OPERATION_TYPE_ITEM); |
141
|
|
|
|
142
|
|
|
try { |
143
|
2 |
|
$transfer = $this->webserviceClient->getByCriteria($criteria, $this->transferName); |
144
|
1 |
|
} catch (WebserviceResponseException $exception) { |
145
|
1 |
|
if ($exception->getCode() == '404') { |
146
|
1 |
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
throw $exception; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
if ($transfer && !$this->objectStorage->contains($transfer)) { |
153
|
1 |
|
$this->objectStorage->attach($transfer); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
if ($transfer) { |
157
|
1 |
|
$transfer = $this->proxyFactory->createProxy($transfer, $this->transferManager); |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
return $transfer; |
161
|
|
|
} |
162
|
|
|
|
163
|
6 |
|
private function prepareCollection(TransferCollection $collection) |
164
|
|
|
{ |
165
|
6 |
|
$collection->setObjectStorage($this->objectStorage) |
166
|
6 |
|
->setTransferManager($this->transferManager) |
167
|
6 |
|
->setProxyFactory($this->proxyFactory); |
168
|
6 |
|
} |
169
|
|
|
|
170
|
|
|
public function getClassName(): string |
171
|
|
|
{ |
172
|
|
|
return $this->transferName; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|