1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\AppnexusClient\repository; |
4
|
|
|
|
5
|
|
|
use Audiens\AppnexusClient\CachableTrait; |
6
|
|
|
use Audiens\AppnexusClient\CacheableInterface; |
7
|
|
|
use Audiens\AppnexusClient\entity\SegmentBilling; |
8
|
|
|
use Audiens\AppnexusClient\exceptions\RepositoryException; |
9
|
|
|
use Doctrine\Common\Cache\Cache; |
10
|
|
|
use GuzzleHttp\ClientInterface; |
11
|
|
|
|
12
|
|
|
class SegmentBillingRepository implements CacheableInterface |
13
|
|
|
{ |
14
|
|
|
use CachableTrait; |
15
|
|
|
|
16
|
|
|
public const BASE_URL = 'https://api.adnxs.com/segment-billing-category'; |
17
|
|
|
public const SANDBOX_BASE_URL = 'http://api-test.adnxs.com/segment-billing-category'; |
18
|
|
|
public const CACHE_NAMESPACE = 'appnexus_segment_billing_repository_find_all'; |
19
|
|
|
public const CACHE_EXPIRATION = 3600; |
20
|
|
|
|
21
|
|
|
/** @var ClientInterface */ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** @var int */ |
25
|
|
|
protected $memberId; |
26
|
|
|
|
27
|
|
|
/** @var Cache */ |
28
|
|
|
protected $cache; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $baseUrl; |
32
|
|
|
|
33
|
|
|
public function __construct(ClientInterface $client, Cache $cache, int $memberId) |
34
|
|
|
{ |
35
|
|
|
$this->client = $client; |
36
|
|
|
$this->cache = $cache; |
37
|
|
|
$this->memberId = $memberId; |
38
|
|
|
$this->baseUrl = self::BASE_URL; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getBaseUrl(): string |
42
|
|
|
{ |
43
|
|
|
return $this->baseUrl; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setBaseUrl($baseUrl) |
47
|
|
|
{ |
48
|
|
|
$this->baseUrl = $baseUrl; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function add(SegmentBilling $segmentBilling): RepositoryResponse |
52
|
|
|
{ |
53
|
|
|
$compiledUrl = $this->baseUrl.'?member_id='.$this->memberId; |
54
|
|
|
|
55
|
|
|
$payload = [ |
56
|
|
|
'segment-billing-category' => $segmentBilling->toArray(), |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$response = $this->client->request('POST', $compiledUrl, ['body' => json_encode($payload)]); |
60
|
|
|
|
61
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
62
|
|
|
|
63
|
|
|
if ($repositoryResponse->isSuccessful()) { |
64
|
|
|
$stream = $response->getBody(); |
65
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
66
|
|
|
|
67
|
|
|
$stream->rewind(); |
68
|
|
|
|
69
|
|
|
if (count($responseContent['response']['segment-billing-category']) == 0) { |
70
|
|
|
throw RepositoryException::missingSegmentBillingContent(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!(isset($responseContent['response']['segment-billing-category'][0]['id']))) { |
74
|
|
|
throw RepositoryException::wrongFormat(serialize($responseContent)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$segmentBilling->setId($responseContent['response']['segment-billing-category'][0]['id']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $repositoryResponse; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function update(SegmentBilling $segmentBilling): RepositoryResponse |
84
|
|
|
{ |
85
|
|
|
if (!$segmentBilling->getId()) { |
|
|
|
|
86
|
|
|
throw RepositoryException::missingSegmentBillingId($segmentBilling); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$compiledUrl = $this->baseUrl.'?member_id='.$this->memberId; |
90
|
|
|
|
91
|
|
|
$payload = [ |
92
|
|
|
'segment-billing-category' => $segmentBilling->toArray(), |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$response = $this->client->request('PUT', $compiledUrl, ['body' => json_encode($payload)]); |
96
|
|
|
|
97
|
|
|
return RepositoryResponse::fromResponse($response); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function findOneBySegmentId($segmentId): ?SegmentBilling |
101
|
|
|
{ |
102
|
|
|
$compiledUrl = $this->baseUrl.'?member_id='.$this->memberId.'&segment_id='.$segmentId; |
103
|
|
|
|
104
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
105
|
|
|
|
106
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
107
|
|
|
|
108
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
109
|
|
|
throw RepositoryException::failed($repositoryResponse); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$stream = $response->getBody(); |
113
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
114
|
|
|
$stream->rewind(); |
115
|
|
|
|
116
|
|
|
if (!$responseContent['response']['segment-billing-categories']) { |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (count($responseContent['response']['segment-billing-categories']) > 1) { |
121
|
|
|
throw RepositoryException::genericFailed('Expected only one results. Found '.count($responseContent['response']['segment-billing-categories'])); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return SegmentBilling::fromArray($responseContent['response']['segment-billing-categories'][0]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function findAll($start = 0, $maxResults = 100): ?array |
128
|
|
|
{ |
129
|
|
|
$cacheKey = self::CACHE_NAMESPACE.sha1($this->memberId.$start.$maxResults); |
130
|
|
|
|
131
|
|
|
if ($this->cache->contains($cacheKey)) { |
132
|
|
|
return $this->cache->fetch($cacheKey); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$compiledUrl = $this->baseUrl.'?member_id='.$this->memberId.'&start_element='.$start.'&num_elements='.$maxResults; |
136
|
|
|
|
137
|
|
|
$response = $this->client->request('GET', $compiledUrl); |
138
|
|
|
|
139
|
|
|
$repositoryResponse = RepositoryResponse::fromResponse($response); |
140
|
|
|
|
141
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
142
|
|
|
throw RepositoryException::failed($repositoryResponse); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$stream = $response->getBody(); |
146
|
|
|
$responseContent = json_decode($stream->getContents(), true); |
147
|
|
|
$stream->rewind(); |
148
|
|
|
|
149
|
|
|
$result = []; |
150
|
|
|
|
151
|
|
|
if (!$responseContent['response']['segment-billing-categories']) { |
152
|
|
|
$responseContent['response']['segment-billing-categories'] = []; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach ($responseContent['response']['segment-billing-categories'] as $segmentBillingArray) { |
156
|
|
|
$result[] = SegmentBilling::fromArray($segmentBillingArray); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION); |
160
|
|
|
|
161
|
|
|
return $result; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function remove($id) |
165
|
|
|
{ |
166
|
|
|
$compiledUrl = $this->baseUrl.'?member_id='.$this->memberId.'&id='.$id; |
167
|
|
|
|
168
|
|
|
$response = $this->client->request('DELETE', $compiledUrl); |
169
|
|
|
|
170
|
|
|
return RepositoryResponse::fromResponse($response); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: