Completed
Push — master ( b2db07...6ada9a )
by luca
08:41
created

UserList   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 159
Duplicated Lines 34.59 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 13
lcom 1
cbo 7
dl 55
loc 159
rs 10
c 3
b 1
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
B createUserList() 0 45 5
C getUserList() 47 47 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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
/**
18
 * Class UserList
19
 */
20
class UserList implements CacheableInterface
21
{
22
    use CachableTrait;
23
24
    const API_VERSION                  = 'v201708';
25
26
    const BASE_URL_USER = 'https://ddp.googleapis.com/api/ddp/provider/v201708/UserListService?wsdl';
27
    const USER_LIST_TPL = 'userList.xml.twig';
28
    const GET_USER_LIST_TPL = 'getUserList.xml.twig';
29
30
    /**
31
     * @var Client|Auth
32
     */
33
    protected $client;
34
35
    /**
36
     * @var  int
37
     */
38
    protected $memberId;
39
40
    /**
41
     * @var  Cache
42
     */
43
    protected $cache;
44
45
    /**
46
     * @var  TwigCompiler
47
     */
48
    protected $twigCompiler;
49
50
    /**
51
     * @var  string
52
     */
53
    protected $clientCustomerId;
54
55
56
    /**
57
     * Report constructor.
58
     *
59
     * @param ClientInterface $client
60
     * @param TwigCompiler $twigCompiler
61
     * @param Cache|null $cache
62
     * @param $clientCustomerId
63
     */
64 View Code Duplication
    public function __construct(ClientInterface $client, TwigCompiler $twigCompiler, Cache $cache = null, $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...
65
    {
66
        $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...
67
        $this->cache = $cache;
68
        $this->twigCompiler = $twigCompiler;
69
        $this->cacheEnabled = $cache instanceof Cache;
70
        $this->clientCustomerId = $clientCustomerId;
71
    }
72
73
74
    /**
75
     * @param Segment $segment
76
     * @param bool $updateIfExist
77
     * @return Segment
78
     * @throws ClientException
79
     */
80
    public function createUserList(Segment $segment, $updateIfExist = false)
81
    {
82
        $operator = 'ADD';
83
84
        if ($updateIfExist) {
85
            $operator = 'SET';
86
        }
87
88
        $requestBody = $this->twigCompiler->getTwig()->render(
89
            self::API_VERSION . '/' . self::USER_LIST_TPL,
90
            [
91
                'id' => $segment->getSegmentId(),
92
                'name' => $segment->getSegmentName(),
93
                'status' => $segment->getSegmentStatus(),
94
                'description' => $segment->getDescription(),
95
                'integrationCode' => $segment->getIntegrationCode(),
96
                'accountUserListStatus' => $segment->getAccountUserListStatus(),
97
                'membershipLifeSpan' => $segment->getMembershipLifeSpan(),
98
                'accessReason' => $segment->getAccessReason(),
99
                'isEligibleForSearch' => $segment->getisEligibleForSearch(),
100
                'clientCustomerId' => $this->clientCustomerId,
101
                'operator' => $operator
102
            ]
103
        );
104
105
106
        try {
107
            $response = $this->client->request('POST', self::BASE_URL_USER, ['body' => $requestBody]);
108
        } catch (RequestException $e) {
109
            $response = $e->getResponse();
110
        }
111
112
113
        $apiResponse = ApiResponse::fromResponse($response);
114
115
        if (!$apiResponse->isSuccessful()) {
116
            throw ClientException::failed($apiResponse);
117
        }
118
119
        if (!isset($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value'])) {
120
            throw ClientException::failed($apiResponse);
121
        }
122
123
        return Segment::fromArray($apiResponse->getResponseArray()['body']['envelope']['body']['mutateresponse']['rval']['value']);
124
    }
125
126
    /**
127
     * @param null $id
128
     * @return array|Segment
129
     * @throws ClientException
130
     */
131 View Code Duplication
    public function getUserList($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...
132
    {
133
        $compiledUrl = self::BASE_URL_USER;
134
135
        $requestBody = $this->twigCompiler->getTwig()->render(
136
            self::API_VERSION . '/' . self::GET_USER_LIST_TPL,
137
            [
138
                'clientCustomerId' => $this->clientCustomerId,
139
                'id' => $id
140
            ]
141
        );
142
143
        try {
144
            $response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]);
145
        } catch (RequestException $e) {
146
            $response = $e->getResponse();
147
        }
148
149
150
        $repositoryResponse = ApiResponse::fromResponse($response);
151
152
        if (!$repositoryResponse->isSuccessful()) {
153
            throw ClientException::failed($repositoryResponse);
154
        }
155
156
        if (!isset($repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'])
157
        ) {
158
            throw ClientException::failed($repositoryResponse);
159
        }
160
161
162
        $entries = $repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'];
163
164
        if (is_array($entries) && isset($entries['id'])) {
165
            //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
166
167
            return Segment::fromArray($entries);
168
        }
169
170
        $segments = [];
171
172
        foreach ($entries as $entry) {
173
            $segments[] = Segment::fromArray($entry);
174
        }
175
176
        return $segments;
177
    }
178
}
179