tsmsogn /
Pixela
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Pixela\Api; |
||
| 4 | |||
| 5 | class Pixel extends Api implements PixelInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var string |
||
| 9 | */ |
||
| 10 | private $graphID; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var \DateTimeInterface |
||
| 14 | */ |
||
| 15 | private $datetime; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | private $quantity; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $optionalData; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @return bool |
||
| 29 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 30 | */ |
||
| 31 | public function post() |
||
| 32 | { |
||
| 33 | $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getGraphID(); |
||
| 34 | |||
| 35 | $options = array( |
||
| 36 | 'headers' => array( |
||
| 37 | 'X-USER-TOKEN' => $this->getClient()->getToken(), |
||
| 38 | ), |
||
| 39 | 'body' => json_encode( |
||
| 40 | array( |
||
| 41 | 'quantity' => $this->getQuantity(), |
||
| 42 | 'date' => $this->getDateTime()->format('Ymd'), |
||
| 43 | 'optionalData' => $this->getOptionalData() |
||
| 44 | ) |
||
| 45 | ) |
||
| 46 | ); |
||
| 47 | |||
| 48 | $response = $this->getClient()->getHttpClient()->request('post', $uri, $options); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 49 | |||
| 50 | return true; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return \Pixela\Api\PixelInterface |
||
| 55 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 56 | */ |
||
| 57 | public function get() |
||
| 58 | { |
||
| 59 | $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getGraphID() . '/' . $this->getDatetime()->format('Ymd'); |
||
| 60 | |||
| 61 | $options = array( |
||
| 62 | 'headers' => array( |
||
| 63 | 'X-USER-TOKEN' => $this->getClient()->getToken(), |
||
| 64 | ) |
||
| 65 | ); |
||
| 66 | |||
| 67 | $response = $this->getClient()->getHttpClient()->request('get', $uri, $options); |
||
| 68 | $contents = array_merge( |
||
| 69 | array( |
||
| 70 | 'quantity' => null, |
||
| 71 | 'optionalData' => '' |
||
| 72 | ), |
||
| 73 | json_decode($response->getBody()->getContents(), true) |
||
| 74 | ); |
||
| 75 | |||
| 76 | $pixel = new \Pixela\Api\Pixel($this->getClient()); |
||
| 77 | $pixel->setGraphID($this->getGraphID()) |
||
| 78 | ->setDatetime($this->getDatetime()) |
||
| 79 | ->setQuantity($contents['quantity']) |
||
| 80 | ->setOptionalData($contents['optionalData']); |
||
| 81 | |||
| 82 | return $pixel; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return bool |
||
| 87 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 88 | */ |
||
| 89 | public function update() |
||
| 90 | { |
||
| 91 | $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getGraphId() . '/' . $this->getDateTime()->format('Ymd'); |
||
| 92 | |||
| 93 | $options = array( |
||
| 94 | 'headers' => array( |
||
| 95 | 'X-USER-TOKEN' => $this->getClient()->getToken(), |
||
| 96 | ), |
||
| 97 | 'body' => json_encode( |
||
| 98 | array( |
||
| 99 | 'quantity' => $this->getQuantity(), |
||
| 100 | 'optionalData' => $this->getOptionalData() |
||
| 101 | ) |
||
| 102 | ) |
||
| 103 | ); |
||
| 104 | |||
| 105 | $response = $this->getClient()->getHttpClient()->request('put', $uri, $options); |
||
|
0 ignored issues
–
show
|
|||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return bool |
||
| 112 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 113 | */ |
||
| 114 | public function increment() |
||
| 115 | { |
||
| 116 | $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getGraphId() . '/increment'; |
||
| 117 | |||
| 118 | $options = array( |
||
| 119 | 'headers' => array( |
||
| 120 | 'X-USER-TOKEN' => $this->getClient()->getToken(), |
||
| 121 | 'Content-Length' => 0 |
||
| 122 | ) |
||
| 123 | ); |
||
| 124 | |||
| 125 | $response = $this->getClient()->getHttpClient()->request('put', $uri, $options); |
||
|
0 ignored issues
–
show
|
|||
| 126 | |||
| 127 | $saved = $this->get(); |
||
| 128 | $this->setQuantity($saved->getQuantity()); |
||
| 129 | |||
| 130 | return true; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return bool |
||
| 135 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 136 | */ |
||
| 137 | public function decrement() |
||
| 138 | { |
||
| 139 | $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getGraphId() . '/decrement'; |
||
| 140 | |||
| 141 | $options = array( |
||
| 142 | 'headers' => array( |
||
| 143 | 'X-USER-TOKEN' => $this->getClient()->getToken(), |
||
| 144 | 'Content-Length' => 0 |
||
| 145 | ) |
||
| 146 | ); |
||
| 147 | |||
| 148 | $response = $this->getClient()->getHttpClient()->request('put', $uri, $options); |
||
|
0 ignored issues
–
show
|
|||
| 149 | |||
| 150 | $saved = $this->get(); |
||
| 151 | $this->setQuantity($saved->getQuantity()); |
||
| 152 | |||
| 153 | return true; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return bool |
||
| 158 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 159 | */ |
||
| 160 | public function delete() |
||
| 161 | { |
||
| 162 | $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/graphs/' . $this->getGraphID() . '/' . $this->getDateTime()->format('Ymd'); |
||
| 163 | |||
| 164 | $options = array( |
||
| 165 | 'headers' => array( |
||
| 166 | 'X-USER-TOKEN' => $this->getClient()->getToken(), |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | |||
| 170 | $response = $this->getClient()->getHttpClient()->request('delete', $uri, $options); |
||
|
0 ignored issues
–
show
|
|||
| 171 | |||
| 172 | return true; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function getGraphID() |
||
| 179 | { |
||
| 180 | return $this->graphID; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $graphID |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function setGraphID($graphID) |
||
| 188 | { |
||
| 189 | $this->graphID = $graphID; |
||
| 190 | |||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return \DateTimeInterface |
||
| 196 | */ |
||
| 197 | public function getDatetime() |
||
| 198 | { |
||
| 199 | return $this->datetime; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param \DateTimeInterface $datetime |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | public function setDatetime(\DateTimeInterface $datetime) |
||
| 207 | { |
||
| 208 | $this->datetime = $datetime; |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getQuantity() |
||
| 217 | { |
||
| 218 | return $this->quantity; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $quantity |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function setQuantity($quantity) |
||
| 226 | { |
||
| 227 | $this->quantity = (string)$quantity; |
||
| 228 | |||
| 229 | return $this; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getOptionalData() |
||
| 236 | { |
||
| 237 | return $this->optionalData; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $optionalData |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function setOptionalData($optionalData) |
||
| 245 | { |
||
| 246 | $this->optionalData = $optionalData; |
||
| 247 | |||
| 248 | return $this; |
||
| 249 | } |
||
| 250 | } |
||
| 251 |