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