GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AudienceRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Audiens\AdobeClient\Repository\Target;
4
5
use Audiens\AdobeClient\CachableTrait;
6
use Audiens\AdobeClient\CacheableInterface;
7
use Audiens\AdobeClient\Entity\Target\Audience;
8
use Audiens\AdobeClient\Exceptions\RepositoryException;
9
use Audiens\AdobeClient\Repository\RepositoryResponse;
10
use Doctrine\Common\Cache\Cache;
11
use GuzzleHttp\Client;
12
use GuzzleHttp\ClientInterface;
13
use GuzzleHttp\RequestOptions;
14
15
/**
16
 * Class AudienceRepository
17
 */
18
class AudienceRepository implements CacheableInterface
19
{
20
    use CachableTrait;
21
22
    const BASE_URL = 'https://mc.adobe.io/%s/target/audiences/';
23
24
    /** @var Client */
25
    protected $client;
26
27
    /** @var  Cache */
28
    protected $cache;
29
30
    /** @var  string */
31
    protected $baseUrl;
32
33
    const CACHE_NAMESPACE = 'adobe_target_repository_find_all';
34
35
    const CACHE_EXPIRATION = 3600;
36
37
    /**
38
     * AudienceRepository constructor.
39
     *
40
     * @param ClientInterface $client
41
     * @param Cache|null      $cache
42
     */
43 View Code Duplication
    public function __construct(ClientInterface $client, Cache $cache = 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...
44
    {
45
        $this->client       = $client;
0 ignored issues
show
Documentation Bug introduced by
$client is of type object<GuzzleHttp\ClientInterface>, but the property $client was declared to be of type object<GuzzleHttp\Client>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
46
        $this->cache        = $cache;
47
        $this->cacheEnabled = $cache instanceof Cache;
48
        $this->baseUrl      = self::BASE_URL;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getBaseUrl()
55
    {
56
        return $this->baseUrl;
57
    }
58
59
    /**
60
     * @param string $baseUrl
61
     */
62
    public function setBaseUrl($baseUrl)
63
    {
64
        $this->baseUrl = $baseUrl;
65
    }
66
67
    public function create(Audience $audience, $tenant)
68
    {
69
        $compiledUrl = sprintf($this->baseUrl, $tenant);
70
71
        if (empty($audience->getName())) {
72
            return;
73
        }
74
75
        $response = $this->client->request(
76
            'POST',
77
            $compiledUrl,
78
            [
79
                                               RequestOptions::JSON => [$audience->toArray()],
80
81
            ]
82
        );
83
84
        $repositoryResponse = RepositoryResponse::fromResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
85
86
        if (!$repositoryResponse->isSuccessful()) {
87
            return null;
88
        }
89
90
        $stream          = $response->getBody();
91
        $responseContent = \json_decode($stream->getContents(), true);
92
        $stream->rewind();
93
94
        return Audience::fromArray($responseContent);
95
    }
96
97
    public function update(Audience $audience, $tenant)
98
    {
99
        $compiledUrl = sprintf($this->baseUrl, $tenant);
100
101
        $compiledUrl = $compiledUrl. $audience->getId();
102
103
        if (empty($audience->getId())) {
104
            return;
105
        }
106
107
        $response = $this->client->request(
108
            'PUT',
109
            $compiledUrl,
110
            [
111
                                               RequestOptions::JSON => [$audience->toArray()],
112
113
            ]
114
        );
115
116
        $repositoryResponse = RepositoryResponse::fromResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
117
118
        if (!$repositoryResponse->isSuccessful()) {
119
            return null;
120
        }
121
122
        $stream          = $response->getBody();
123
        $responseContent = \json_decode($stream->getContents(), true);
124
        $stream->rewind();
125
126
        return Audience::fromArray($responseContent);
127
    }
128
129
    public function findOneById($id, $tenant)
130
    {
131
        if (empty($tenant)) {
132
            RepositoryException::genericFailed('Missing tenant params');
133
        }
134
135
        $baseUrl = sprintf($this->baseUrl, $tenant);
136
137
        $compiledUrl = $baseUrl . '?id=' . $id;
138
139
        $response = $this->client->request('GET', $compiledUrl);
140
141
        $repositoryResponse = RepositoryResponse::fromResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
142
143
        if (!$repositoryResponse->isSuccessful()) {
144
            return null;
145
        }
146
147
        $stream          = $response->getBody();
148
        $responseContent = \json_decode($stream->getContents(), true);
149
        $stream->rewind();
150
151 View Code Duplication
        if (!isset($responseContent['audiences']) || count($responseContent['audiences']) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152
            return null;
153
        }
154
155
        if (count($responseContent['audiences']) > 1) {
156
            RepositoryException::genericFailed('Adobe Target returned more that one audiences...please check your id');
157
        }
158
159
        return Audience::fromArray($responseContent['audiences'][0]);
160
    }
161
162
    public function findAll($tenant)
163
    {
164
        if (empty($tenant)) {
165
            RepositoryException::genericFailed('Missing tenant params');
166
        }
167
168
        $date = date('Y_m_d_H');
169
170
        $cacheKey = self::CACHE_NAMESPACE . sha1($date);
171
172
        if ($this->isCacheEnabled()) {
173
            if ($this->cache->contains($cacheKey)) {
174
                return $this->cache->fetch($cacheKey);
175
            }
176
        }
177
178
        $compiledUrl = sprintf($this->baseUrl, $tenant);
179
180
        $response = $this->client->request('GET', $compiledUrl);
181
182
        $repositoryResponse = RepositoryResponse::fromResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
183
184
        if (!$repositoryResponse->isSuccessful()) {
185
            throw RepositoryException::genericFailed($repositoryResponse);
0 ignored issues
show
Documentation introduced by
$repositoryResponse is of type object<Audiens\AdobeClie...ory\RepositoryResponse>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
186
        }
187
188
        $stream          = $response->getBody();
189
        $responseContent = \json_decode($stream->getContents(), true);
190
        $stream->rewind();
191
192
        $result = [];
193
194 View Code Duplication
        if (!isset($responseContent['audiences']) || count($responseContent['audiences']) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
195
            return $result;
196
        }
197
198
        $audiences = $responseContent['audiences'];
199
200
        foreach ($audiences as $audience) {
201
            $result[] = Audience::fromArray($audience);
202
        }
203
204
        if ($this->isCacheEnabled()) {
205
            $this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION);
206
        }
207
208
        return $result;
209
    }
210
}
211