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) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->client = $client; |
|
|
|
|
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) |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.