Completed
Push — master ( ae7486...e30bc0 )
by Francesco
16:00
created

CategoryRepository::setBaseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Audiens\AppnexusClient\repository;
4
5
use Audiens\AppnexusClient\CachableTrait;
6
use Audiens\AppnexusClient\CacheableInterface;
7
use Audiens\AppnexusClient\entity\Category;
8
use Audiens\AppnexusClient\exceptions\RepositoryException;
9
use Doctrine\Common\Cache\Cache;
10
use GuzzleHttp\Client;
11
use GuzzleHttp\ClientInterface;
12
13
/**
14
 * Class CategoryRepository
15
 */
16
class CategoryRepository implements CacheableInterface
17
{
18
19
    use CachableTrait;
20
21
    const BASE_URL = 'https://api.adnxs.com/content-category/';
22
23
    const SANDBOX_BASE_URL = 'http://api-test.adnxs.com/content-category/';
24
25
    /** @var Client */
26
    protected $client;
27
28
    /** @var  int */
29
    protected $memberId;
30
31
    /** @var  Cache */
32
    protected $cache;
33
34
    /** @var  string */
35
    protected $baseUrl;
36
37
    const CACHE_NAMESPACE = 'appnexus_category_repository_find_all';
38
39
    const CACHE_EXPIRATION = 3600;
40
41
    /**
42
     * SegmentRepository constructor.
43
     *
44
     * @param ClientInterface $client
45
     * @param Cache|null $cache
46
     */
47 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...
48
    {
49
        $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...
50
        $this->cache        = $cache;
51
        $this->cacheEnabled = $cache instanceof Cache;
52
        $this->baseUrl      = self::BASE_URL;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getBaseUrl()
59
    {
60
        return $this->baseUrl;
61
    }
62
63
    /**
64
     * @param string $baseUrl
65
     */
66
    public function setBaseUrl($baseUrl)
67
    {
68
        $this->baseUrl = $baseUrl;
69
    }
70
71
    /**
72
     * @param int $start
73
     * @param int $maxResults
74
     * @return Category[]|null
75
     * @throws RepositoryException
76
     */
77 View Code Duplication
    public function findAll($start = 0, $maxResults = 100)
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...
78
    {
79
80
        $cacheKey = self::CACHE_NAMESPACE . sha1($start . $maxResults);
81
82
        if ($this->isCacheEnabled()) {
83
            if ($this->cache->contains($cacheKey)) {
84
                return $this->cache->fetch($cacheKey);
85
            }
86
        }
87
88
        $compiledUrl = $this->baseUrl . "?start_element=$start&num_elements=$maxResults";
89
90
        $response = $this->client->request('GET', $compiledUrl);
91
92
        $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...
93
94
        if (!$repositoryResponse->isSuccessful()) {
95
            throw RepositoryException::failed($repositoryResponse);
96
        }
97
98
        $stream          = $response->getBody();
99
        $responseContent = json_decode($stream->getContents(), true);
100
        $stream->rewind();
101
102
        $result = [];
103
104
        if (!$responseContent['response']['content_categories']) {
105
            $responseContent['response']['content_categories'] = [];
106
        }
107
108
        foreach ($responseContent['response']['content_categories'] as $segmentArray) {
109
            $result[] = Category::fromArray($segmentArray);
110
        }
111
112
        if ($this->isCacheEnabled()) {
113
            $this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION);
114
        }
115
116
        return $result;
117
    }
118
}
119