Completed
Push — master ( 8c5cc3...01b1f4 )
by Francesco
02:33
created

AppnexusFacade   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 17
c 3
b 2
f 1
lcom 2
cbo 8
dl 0
loc 186
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A add() 0 6 1
A remove() 0 6 1
A update() 0 4 1
A findOneById() 0 4 1
A findAll() 0 5 1
A upload() 0 4 1
A getUploadHistory() 0 4 1
A getUploadTicket() 0 4 1
A getJobStatus() 0 4 1
A getReport() 0 15 3
A isCacheEnabled() 0 4 2
A disableCache() 0 5 1
A enableCache() 0 5 1
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
/**
19
 * Class AppnexusFacade
20
 */
21
class AppnexusFacade implements CacheableInterface
22
{
23
24
    protected $username;
25
    protected $password;
26
    protected $memberId;
27
28
    /** @var  SegmentRepository */
29
    private $segmentRepository;
30
31
    /** @var UserUpload */
32
    private $userUpload;
33
34
    /** @var Report */
35
    private $report;
36
37
    /**
38
     * AppnexusFacade constructor.
39
     *
40
     * @param $username
41
     * @param $password
42
     * @param $memberId
43
     */
44
    public function __construct($username, $password, $memberId)
45
    {
46
        $this->username = $username;
47
        $this->password = $password;
48
        $this->memberId = $memberId;
49
50
        $client = new Client();
51
        $cache = new FilesystemCache('build');
52
53
        $authStrategy = new AdnxStrategy($client, $cache);
54
55
        $auth = new Auth($username, $password, $client, $authStrategy);
56
57
        $this->segmentRepository = new SegmentRepository($auth, $cache);
58
        $this->userUpload = new UserUpload($auth, $cache);
59
        $this->report = new Report($auth, $cache);
60
61
    }
62
63
    /**
64
     * @param Segment $segment
65
     *
66
     * @return RepositoryResponse
67
     * @throws \Exception
68
     */
69
    public function add(Segment $segment)
70
    {
71
72
        return $this->segmentRepository->add($segment);
73
74
    }
75
76
    /**
77
     * @param $id
78
     *
79
     * @return RepositoryResponse
80
     */
81
    public function remove($id)
82
    {
83
84
        return $this->segmentRepository->remove($this->memberId, $id);
85
86
    }
87
88
    /**
89
     * @param Segment $segment
90
     *
91
     * @return RepositoryResponse
92
     * @throws \Exception
93
     */
94
    public function update(Segment $segment)
95
    {
96
        return $this->segmentRepository->update($segment);
97
    }
98
99
    /**
100
     * @param $id
101
     *
102
     * @return Segment|null
103
     */
104
    public function findOneById($id)
105
    {
106
        return $this->segmentRepository->findOneById($this->memberId, $id);
107
    }
108
109
    /**
110
     * @param int $start
111
     * @param int $maxResults
112
     *
113
     * @return array|mixed|null
114
     * @throws \Exception
115
     */
116
    public function findAll($start = 0, $maxResults = 100)
117
    {
118
119
        return $this->segmentRepository->findAll($this->memberId, $start, $maxResults);
120
    }
121
122
    /**
123
     * @param $fileAsString
124
     *
125
     * @return \Audiens\AppnexusClient\entity\UploadJobStatus
126
     * @throws \Exception
127
     */
128
    public function upload($fileAsString)
129
    {
130
        return $this->userUpload->upload($this->memberId, $fileAsString);
131
    }
132
133
    /**
134
     * @param int $start
135
     * @param int $maxResults
136
     *
137
     * @return \Audiens\AppnexusClient\entity\UploadJobStatus[]
138
     * @throws \Audiens\AppnexusClient\exceptions\RepositoryException
139
     */
140
    public function getUploadHistory($start = 0, $maxResults = 100)
141
    {
142
        return $this->userUpload->getUploadHistory($this->memberId, $start, $maxResults);
143
    }
144
145
    /**
146
     * @return \Audiens\AppnexusClient\entity\UploadTicket
147
     * @throws \Exception
148
     */
149
    public function getUploadTicket()
150
    {
151
        return $this->userUpload->getUploadTicket($this->memberId);
152
    }
153
154
    /**
155
     * @param UploadTicket $uploadTicket
156
     *
157
     * @return UploadJobStatus $uploadJobStatus
158
     * @throws \Exception
159
     */
160
    public function getJobStatus(UploadTicket $uploadTicket)
161
    {
162
        return $this->userUpload->getJobStatus($uploadTicket);
163
    }
164
165
    /**
166
     * @param array $reportFormat
167
     *
168
     * @return array
169
     * @throws \Audiens\AppnexusClient\exceptions\RepositoryException
170
     */
171
    public function getReport($reportFormat = Report::REVENUE_REPORT)
172
    {
173
174
        $reportStatus = $this->report->getReportStatus($this->report->getReportTicket($reportFormat));
175
176
        $maxSteps = 0;
177
178
        while (!$reportStatus->isReady() && $maxSteps < 10) {
179
            $reportStatus = $this->report->getReportStatus($reportStatus);
180
            $maxSteps++;
181
        }
182
183
        return $this->report->getReport($reportStatus);
184
185
    }
186
187
    /**
188
     * @return bool
189
     */
190
    public function isCacheEnabled()
191
    {
192
        return $this->segmentRepository->isCacheEnabled() || $this->userUpload->isCacheEnabled();
193
    }
194
195
    public function disableCache()
196
    {
197
        $this->segmentRepository->disableCache();
198
        $this->userUpload->disableCache();
199
    }
200
201
    public function enableCache()
202
    {
203
        $this->segmentRepository->enableCache();
204
        $this->userUpload->enableCache();
205
    }
206
}
207