AppnexusFacade::disableCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Audiens\AppnexusClient\facade;
4
5
use Audiens\AppnexusClient\Auth;
6
use Audiens\AppnexusClient\authentication\AdnxStrategy;
7
use Audiens\AppnexusClient\CacheableInterface;
8
use Audiens\AppnexusClient\entity\Segment;
9
use Audiens\AppnexusClient\entity\UploadJobStatus;
10
use Audiens\AppnexusClient\entity\UploadTicket;
11
use Audiens\AppnexusClient\repository\RepositoryResponse;
12
use Audiens\AppnexusClient\repository\SegmentRepository;
13
use Audiens\AppnexusClient\service\Report;
14
use Audiens\AppnexusClient\service\UserUpload;
15
use Doctrine\Common\Cache\FilesystemCache;
16
use GuzzleHttp\Client;
17
18
class AppnexusFacade implements CacheableInterface
19
{
20
21
    /** @var string */
22
    protected $username;
23
24
    /** @var string */
25
    protected $password;
26
27
    /** @var int */
28
    protected $memberId;
29
30
    /** @var  SegmentRepository */
31
    private $segmentRepository;
32
33
    /** @var UserUpload */
34
    private $userUpload;
35
36
    /** @var Report */
37
    private $report;
38
39
    public function __construct(string $username, string $password, int $memberId)
40
    {
41
        $this->username = $username;
42
        $this->password = $password;
43
        $this->memberId = $memberId;
44
45
        $client = new Client();
46
        $cache  = new FilesystemCache('build');
47
48
        $authStrategy = new AdnxStrategy($client, $cache);
49
50
        $auth = new Auth($username, $password, $client, $authStrategy);
51
52
        $this->segmentRepository = new SegmentRepository($auth, $cache, $memberId);
53
        $this->userUpload        = new UserUpload($auth, $cache);
54
        $this->report            = new Report($auth, $cache);
55
    }
56
57
    public function add(Segment $segment)
58
    {
59
        return $this->segmentRepository->add($segment);
60
    }
61
62
    public function remove($id)
63
    {
64
        return $this->segmentRepository->remove($id);
65
    }
66
67
    public function update(Segment $segment): RepositoryResponse
68
    {
69
        return $this->segmentRepository->update($segment);
70
    }
71
72
    public function findOneById($id): ?Segment
73
    {
74
        return $this->segmentRepository->findOneById($id);
75
    }
76
77
    public function findAll(int $start = 0, int $maxResults = 100)
78
    {
79
        return $this->segmentRepository->findAll($start, $maxResults);
80
    }
81
82
    public function upload(string $fileAsString): UploadJobStatus
83
    {
84
        return $this->userUpload->upload($this->memberId, $fileAsString);
85
    }
86
87
    public function getUploadHistory(int $start = 0, int $maxResults = 100): array
88
    {
89
        return $this->userUpload->getUploadHistory($this->memberId, $start, $maxResults);
90
    }
91
92
    public function getUploadTicket(): UploadTicket
93
    {
94
        return $this->userUpload->getUploadTicket($this->memberId);
95
    }
96
97
    public function getJobStatus(UploadTicket $uploadTicket): UploadJobStatus
98
    {
99
        return $this->userUpload->getJobStatus($uploadTicket);
100
    }
101
102
    public function getReport($reportFormat = Report::REVENUE_REPORT): array
103
    {
104
        $reportStatus = $this->report->getReportStatus($this->report->getReportTicket($reportFormat));
105
106
        $maxSteps = 0;
107
108
        while (!$reportStatus->isReady() && $maxSteps < 10) {
109
            $reportStatus = $this->report->getReportStatus($reportStatus);
110
            $maxSteps++;
111
        }
112
113
        return $this->report->getReport($reportStatus);
114
    }
115
116
    public function isCacheEnabled(): bool
117
    {
118
        return $this->segmentRepository->isCacheEnabled() || $this->userUpload->isCacheEnabled();
119
    }
120
121
    public function disableCache()
122
    {
123
        $this->segmentRepository->disableCache();
124
        $this->userUpload->disableCache();
125
    }
126
127
    public function enableCache()
128
    {
129
        $this->segmentRepository->enableCache();
130
        $this->userUpload->enableCache();
131
    }
132
}
133