1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Audiens\DoubleclickClient\service; |
5
|
|
|
|
6
|
|
|
use Audiens\DoubleclickClient\Auth; |
7
|
|
|
use Audiens\DoubleclickClient\CachableTrait; |
8
|
|
|
use Audiens\DoubleclickClient\CacheableInterface; |
9
|
|
|
use Audiens\DoubleclickClient\entity\ApiResponse; |
10
|
|
|
use Audiens\DoubleclickClient\exceptions\ClientException; |
11
|
|
|
use Doctrine\Common\Cache\Cache; |
12
|
|
|
use GuzzleHttp\Client; |
13
|
|
|
|
14
|
|
|
use Audiens\DoubleclickClient\entity\UserListClient; |
15
|
|
|
use GuzzleHttp\Exception\RequestException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class UserListClientService |
19
|
|
|
*/ |
20
|
|
|
class UserListClientService implements CacheableInterface |
21
|
|
|
{ |
22
|
|
|
use CachableTrait; |
23
|
|
|
|
24
|
|
|
const API_VERSION = 'v201708'; |
25
|
|
|
|
26
|
|
|
const URL_LIST_CLIENT = 'https://ddp.googleapis.com/api/ddp/provider/v201708/UserListClientService?wsdl'; |
27
|
|
|
const USER_LIST_CLIENT_TPL = 'userListClient.xml.twig'; |
28
|
|
|
const GET_USER_CLIENT_LIST_TPL = 'getUserClientList.xml.twig'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Client|Auth |
32
|
|
|
*/ |
33
|
|
|
protected $client; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Cache |
37
|
|
|
*/ |
38
|
|
|
protected $cache; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TwigCompiler |
42
|
|
|
*/ |
43
|
|
|
protected $twigCompiler; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $clientCustomerId; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* UserListClient constructor. |
52
|
|
|
* @param Auth|Client $client |
53
|
|
|
* @param Cache $cache |
54
|
|
|
* @param TwigCompiler $twigCompiler |
55
|
|
|
* @param string $clientCustomerId |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function __construct($client, Cache $cache = null, TwigCompiler $twigCompiler, $clientCustomerId) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->client = $client; |
60
|
|
|
$this->cache = $cache; |
61
|
|
|
$this->twigCompiler = $twigCompiler; |
62
|
|
|
$this->clientCustomerId = $clientCustomerId; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param UserListClient $client |
67
|
|
|
* @param bool $updateIfExist |
68
|
|
|
* @return UserListClient |
69
|
|
|
* @throws ClientException |
70
|
|
|
*/ |
71
|
|
|
public function createUserClientList(UserListClient $client, $updateIfExist = false) |
72
|
|
|
{ |
73
|
|
|
$operator = 'ADD'; |
74
|
|
|
|
75
|
|
|
if ($updateIfExist) { |
76
|
|
|
$operator = 'SET'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$context = [ |
80
|
|
|
'status' => $client->getStatus(), |
81
|
|
|
'userlistid' => $client->getUserlistid(), |
82
|
|
|
'operator' => $operator, |
83
|
|
|
'clientCustomerId' => $this->clientCustomerId, |
84
|
|
|
'clientid' => $client->getClientid(), |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
if ($client->getPricingInfo()) { |
89
|
|
|
$context['pricingInfo'] = $client->getPricingInfo(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($client->getClientproduct()) { |
93
|
|
|
$context['clientproduct'] = $client->getClientproduct(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$requestBody = $this->twigCompiler->getTwig()->render( |
97
|
|
|
self::API_VERSION . '/' . self::USER_LIST_CLIENT_TPL, |
98
|
|
|
$context |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
$response = $this->client->request('POST', self::URL_LIST_CLIENT, ['body' => $requestBody]); |
104
|
|
|
} catch (RequestException $e) { |
105
|
|
|
$response = $e->getResponse(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$apiResponse = ApiResponse::fromResponse($response); |
109
|
|
|
|
110
|
|
|
if (!$apiResponse->isSuccessful()) { |
111
|
|
|
throw ClientException::failed($apiResponse); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!isset($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value'])) { |
115
|
|
|
throw ClientException::failed($apiResponse); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return UserListClient::fromArray($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param null $userListId |
124
|
|
|
* @param null $clientId |
125
|
|
|
* @return UserListClient[]|UserListClient |
126
|
|
|
* @throws ClientException |
127
|
|
|
*/ |
128
|
|
|
public function getUserClientList($userListId = null, $clientId = null) |
129
|
|
|
{ |
130
|
|
|
$compiledUrl = self::URL_LIST_CLIENT; |
131
|
|
|
|
132
|
|
|
$requestBody = $this->twigCompiler->getTwig()->render( |
133
|
|
|
self::API_VERSION . '/' . self::GET_USER_CLIENT_LIST_TPL, |
134
|
|
|
[ |
135
|
|
|
'clientCustomerId' => $this->clientCustomerId, |
136
|
|
|
'userlistid' => $userListId, |
137
|
|
|
'clientId' => $clientId |
138
|
|
|
] |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
$response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]); |
143
|
|
|
} catch (RequestException $e) { |
144
|
|
|
$response = $e->getResponse(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
$repositoryResponse = ApiResponse::fromResponse($response); |
149
|
|
|
|
150
|
|
|
if (!$repositoryResponse->isSuccessful()) { |
151
|
|
|
throw ClientException::failed($repositoryResponse); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$entries = $repositoryResponse |
155
|
|
|
->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'] |
156
|
|
|
?? []; |
157
|
|
|
|
158
|
|
|
if (is_array($entries) && isset($entries['userlistid'])) { |
159
|
|
|
return UserListClient::fromArray($entries); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$licenses = []; |
163
|
|
|
|
164
|
|
|
foreach ($entries as $entry) { |
165
|
|
|
$licenses[] = UserListClient::fromArray($entry); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $licenses; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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.