|
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\UserUpload; |
|
14
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
|
15
|
|
|
use GuzzleHttp\Client; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class AppnexusFacade |
|
19
|
|
|
*/ |
|
20
|
|
|
class AppnexusFacade implements CacheableInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
protected $username; |
|
24
|
|
|
protected $password; |
|
25
|
|
|
protected $memberId; |
|
26
|
|
|
|
|
27
|
|
|
/** @var SegmentRepository */ |
|
28
|
|
|
private $segmentRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* AppnexusFacade constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param $username |
|
34
|
|
|
* @param $password |
|
35
|
|
|
* @param $memberId |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($username, $password, $memberId) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->username = $username; |
|
40
|
|
|
$this->password = $password; |
|
41
|
|
|
$this->memberId = $memberId; |
|
42
|
|
|
|
|
43
|
|
|
$client = new Client(); |
|
44
|
|
|
$cache = new FilesystemCache('build'); |
|
45
|
|
|
|
|
46
|
|
|
$authStrategy = new AdnxStrategy($client, $cache); |
|
47
|
|
|
|
|
48
|
|
|
$auth = new Auth($username, $password, $client, $authStrategy); |
|
49
|
|
|
|
|
50
|
|
|
$this->segmentRepository = new SegmentRepository($auth, $cache); |
|
51
|
|
|
$this->userUpload = new UserUpload($auth, $cache); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Segment $segment |
|
57
|
|
|
* |
|
58
|
|
|
* @return RepositoryResponse |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
public function add(Segment $segment) |
|
62
|
|
|
{ |
|
63
|
|
|
|
|
64
|
|
|
return $this->segmentRepository->add($segment); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $id |
|
70
|
|
|
* |
|
71
|
|
|
* @return RepositoryResponse |
|
72
|
|
|
*/ |
|
73
|
|
|
public function remove($id) |
|
74
|
|
|
{ |
|
75
|
|
|
|
|
76
|
|
|
return $this->segmentRepository->remove($this->memberId, $id); |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Segment $segment |
|
82
|
|
|
* |
|
83
|
|
|
* @return RepositoryResponse |
|
84
|
|
|
* @throws \Exception |
|
85
|
|
|
*/ |
|
86
|
|
|
public function update(Segment $segment) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->segmentRepository->update($segment); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $id |
|
93
|
|
|
* |
|
94
|
|
|
* @return Segment|null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function findOneById($id) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->segmentRepository->findOneById($this->memberId, $id); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param int $start |
|
103
|
|
|
* @param int $maxResults |
|
104
|
|
|
* |
|
105
|
|
|
* @return array|mixed|null |
|
106
|
|
|
* @throws \Exception |
|
107
|
|
|
*/ |
|
108
|
|
|
public function findAll($start = 0, $maxResults = 100) |
|
109
|
|
|
{ |
|
110
|
|
|
|
|
111
|
|
|
return $this->segmentRepository->findAll($this->memberId, $start, $maxResults); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param $fileAsString |
|
116
|
|
|
* |
|
117
|
|
|
* @return \Audiens\AppnexusClient\entity\UploadJobStatus |
|
118
|
|
|
* @throws \Exception |
|
119
|
|
|
*/ |
|
120
|
|
|
public function upload($fileAsString) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->userUpload->upload($this->memberId, $fileAsString); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return \Audiens\AppnexusClient\entity\UploadTicket |
|
127
|
|
|
* @throws \Exception |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getUploadTicket() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->userUpload->getUploadTicket($this->memberId); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param UploadTicket $uploadTicket |
|
136
|
|
|
* |
|
137
|
|
|
* @return UploadJobStatus $uploadJobStatus |
|
138
|
|
|
* @throws \Exception |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getJobStatus(UploadTicket $uploadTicket) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->userUpload->getJobStatus($uploadTicket); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function isCacheEnabled() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->segmentRepository->isCacheEnabled() || $this->userUpload->isCacheEnabled(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function disableCache() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->segmentRepository->disableCache(); |
|
156
|
|
|
$this->userUpload->disableCache(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function enableCache() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->segmentRepository->enableCache(); |
|
162
|
|
|
$this->userUpload->enableCache(); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: