Completed
Push — master ( 6ada9a...5d3860 )
by luca
01:41
created

UserListClientService::createUserClientList()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 50
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 48
nop 2
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
89
        if ($client->getPricingInfo()) {
90
            $context['pricingInfo'] = $client->getPricingInfo();
91
        }
92
93
        if ($client->getClientproduct()) {
94
            $context['clientproduct'] = $client->getClientproduct();
95
        }
96
97
        $requestBody = $this->twigCompiler->getTwig()->render(
98
            self::API_VERSION . '/' . self::USER_LIST_CLIENT_TPL,
99
            $context
100
        );
101
102
103
        try {
104
            $response = $this->client->request('POST', self::URL_LIST_CLIENT, ['body' => $requestBody]);
105
        } catch (RequestException $e) {
106
            $response = $e->getResponse();
107
        }
108
109
        $apiResponse = ApiResponse::fromResponse($response);
110
111
        if (!$apiResponse->isSuccessful()) {
112
            throw ClientException::failed($apiResponse);
113
        }
114
115
        if (!isset($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value'])) {
116
            throw ClientException::failed($apiResponse);
117
        }
118
119
        return UserListClient::fromArray($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value']);
120
    }
121
122
123
    /**
124
     * @param int|null $id
125
     * @return UserListClient[]|UserListClient
126
     * @throws ClientException
127
     */
128 View Code Duplication
    public function getUserClientList($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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' => $id
137
            ]
138
        );
139
140
        try {
141
            $response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]);
142
        } catch (RequestException $e) {
143
            $response = $e->getResponse();
144
        }
145
146
147
        $repositoryResponse = ApiResponse::fromResponse($response);
148
149
        if (!$repositoryResponse->isSuccessful()) {
150
            throw ClientException::failed($repositoryResponse);
151
        }
152
153
        if (!isset($repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'])
154
        ) {
155
            throw ClientException::failed($repositoryResponse);
156
        }
157
158
159
        $entries = $repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'];
160
161
        if (is_array($entries) && isset($entries['id'])) {
162
            return UserListClient::fromArray($entries);
163
        }
164
165
        $licenses = [];
166
167
        foreach ($entries as $entry) {
168
            $licenses[] = UserListClient::fromArray($entry);
169
        }
170
171
        return $licenses;
172
    }
173
}
174