DataProviderAudienceManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 32
dl 0
loc 78
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B get() 0 50 8
1
<?php declare(strict_types=1);
2
3
namespace Audiens\AdForm\Manager;
4
5
use Audiens\AdForm\Cache\CacheInterface;
6
use Audiens\AdForm\Entity\DataProviderAudience;
7
use Audiens\AdForm\Entity\DataProviderAudienceHydrator;
8
use Audiens\AdForm\Exception\ApiException;
9
use Audiens\AdForm\HttpClient;
10
use DateTime;
11
use GuzzleHttp\Exception\ClientException;
12
13
class DataProviderAudienceManager
14
{
15
    /** @var HttpClient */
16
    protected $httpClient;
17
18
    /** @var CacheInterface|null */
19
    protected $cache;
20
21
    /** @var string */
22
    protected $cachePrefix = 'data_provider_audience';
23
24
    public function __construct(HttpClient $httpClient, CacheInterface $cache = null)
25
    {
26
        $this->httpClient = $httpClient;
27
        $this->cache = $cache;
28
    }
29
30
    /**
31
     * Returns an array of audience objects
32
     *
33
     * @param int      $dataProviderId
34
     * @param DateTime $from
35
     * @param DateTime $to
36
     * @param array    $groupBy
37
     *
38
     * @return DataProviderAudience[]
39
     * @throws ApiException if the API call fails
40
     */
41
    public function get(int $dataProviderId, DateTime $from, DateTime $to, array $groupBy): array
42
    {
43
        // Endpoint URI
44
        $uri = sprintf('v1/dataproviders/%d/audience', $dataProviderId);
45
46
        $options = [
47
            'query' => [
48
                'from' => $from->format('c'),
49
                'to' => $to->format('c'),
50
                'groupBy' => $groupBy,
51
            ],
52
        ];
53
54
        $dataProviderAudiences = [];
55
56
        try {
57
            $data = null;
58
59
            // try to get from cache
60
            if ($this->cache) {
61
                $data = $this->cache->get($this->cachePrefix, $uri, $options);
62
            }
63
64
            // load from API
65
            if (!$data) {
66
                $data = $this->httpClient->get($uri, $options)->getBody()->getContents();
67
68
                if ($this->cache && $data) {
69
                    $this->cache->put($this->cachePrefix, $uri, $options, $data);
70
                }
71
            }
72
73
            $classArray = \json_decode($data);
74
75
            foreach ($classArray as $class) {
76
                $dataProviderAudiences[] = DataProviderAudienceHydrator::fromStdClass($class);
77
            }
78
        } catch (ClientException $e) {
79
            $response = $e->getResponse();
80
            if ($response === null) {
81
                throw $e;
82
            }
83
84
            $responseBody = $response->getBody()->getContents();
85
            $responseCode = $response->getStatusCode();
86
87
            throw new ApiException($responseBody, $responseCode);
88
        }
89
90
        return $dataProviderAudiences;
91
    }
92
}
93