UserList   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 146
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B createUserList() 0 43 5
B getUserList() 0 45 7
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\Segment;
10
use Audiens\DoubleclickClient\entity\ApiResponse;
11
use Audiens\DoubleclickClient\exceptions\ClientException;
12
use Doctrine\Common\Cache\Cache;
13
use GuzzleHttp\Client;
14
use GuzzleHttp\ClientInterface;
15
use GuzzleHttp\Exception\RequestException;
16
17
class UserList implements CacheableInterface, ApiConfigurationInterface
18
{
19
    use CachableTrait;
20
21
    public const BASE_URL_USER     = 'https://ddp.googleapis.com/api/ddp/provider/'.self::API_VERSION.'/UserListService?wsdl';
22
    public const USER_LIST_TPL     = 'userList.xml.twig';
23
    public const GET_USER_LIST_TPL = 'getUserList.xml.twig';
24
25
    /**
26
     * @var Client|Auth
27
     */
28
    protected $client;
29
30
    /**
31
     * @var  int
32
     */
33
    protected $memberId;
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
     * Report constructor.
52
     *
53
     * @param ClientInterface $client
54
     * @param TwigCompiler    $twigCompiler
55
     * @param Cache|null      $cache
56
     * @param                 $clientCustomerId
57
     */
58
    public function __construct(ClientInterface $client, TwigCompiler $twigCompiler, Cache $cache = null, $clientCustomerId)
59
    {
60
        $this->client           = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<GuzzleHttp\ClientInterface> is incompatible with the declared type object<GuzzleHttp\Client...DoubleclickClient\Auth> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
        $this->cache            = $cache;
62
        $this->twigCompiler     = $twigCompiler;
63
        $this->cacheEnabled     = $cache instanceof Cache;
64
        $this->clientCustomerId = $clientCustomerId;
65
    }
66
67
    public function createUserList(Segment $segment, $updateIfExist = false): Segment
68
    {
69
        $operator = 'ADD';
70
71
        if ($updateIfExist) {
72
            $operator = 'SET';
73
        }
74
75
        $requestBody = $this->twigCompiler->getTwig()->render(
76
            self::API_VERSION.'/'.self::USER_LIST_TPL,
77
            [
78
                'id' => $segment->getSegmentId(),
79
                'name' => $segment->getSegmentName(),
80
                'status' => $segment->getSegmentStatus(),
81
                'description' => $segment->getDescription(),
82
                'integrationCode' => $segment->getIntegrationCode(),
83
                'accountUserListStatus' => $segment->getAccountUserListStatus(),
84
                'membershipLifeSpan' => $segment->getMembershipLifeSpan(),
85
                'accessReason' => $segment->getAccessReason(),
86
                'isEligibleForSearch' => $segment->getisEligibleForSearch(),
87
                'clientCustomerId' => $this->clientCustomerId,
88
                'operator' => $operator,
89
            ]
90
        );
91
92
        try {
93
            $response = $this->client->request('POST', self::BASE_URL_USER, ['body' => $requestBody]);
94
        } catch (RequestException $e) {
95
            $response = $e->getResponse();
96
        }
97
98
        $apiResponse = ApiResponse::fromResponse($response);
99
100
        if (!$apiResponse->isSuccessful()) {
101
            throw ClientException::failed($apiResponse);
102
        }
103
104
        if (!isset($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value'])) {
105
            throw ClientException::failed($apiResponse);
106
        }
107
108
        return Segment::fromArray($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value']);
109
    }
110
111
    /**
112
     * @param null|string $id
113
     *
114
     * @return array|Segment
115
     * @throws ClientException
116
     */
117
    public function getUserList($id = null)
118
    {
119
        $compiledUrl = self::BASE_URL_USER;
120
121
        $requestBody = $this->twigCompiler->getTwig()->render(
122
            self::API_VERSION.'/'.self::GET_USER_LIST_TPL,
123
            [
124
                'clientCustomerId' => $this->clientCustomerId,
125
                'id' => $id,
126
            ]
127
        );
128
129
        try {
130
            $response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]);
131
        } catch (RequestException $e) {
132
            $response = $e->getResponse();
133
        }
134
135
        $repositoryResponse = ApiResponse::fromResponse($response);
136
137
        if (!$repositoryResponse->isSuccessful()) {
138
            throw ClientException::failed($repositoryResponse);
139
        }
140
141
        if (!isset($repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'])
142
        ) {
143
            throw ClientException::failed($repositoryResponse);
144
        }
145
146
        $entries = $repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'];
147
148
        if (is_array($entries) && isset($entries['id'])) {
149
            //ok, this is the case when you search a specific user list. So we don't have an array of array in response but just a single array
150
151
            return Segment::fromArray($entries);
152
        }
153
154
        $segments = [];
155
156
        foreach ($entries as $entry) {
157
            $segments[] = Segment::fromArray($entry);
158
        }
159
160
        return $segments;
161
    }
162
}
163