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