1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client; |
6
|
|
|
|
7
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Purchase; |
8
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\PurchaseHistory; |
9
|
|
|
use LauLamanApps\IzettleApi\Client\Exception\NotFoundException; |
10
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\Exception\PurchaseNotFoundException; |
11
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\PurchaseBuilderInterface; |
12
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\PurchaseHistoryBuilderInterface; |
13
|
|
|
use LauLamanApps\IzettleApi\IzettleClientInterface; |
14
|
|
|
use Ramsey\Uuid\UuidInterface; |
15
|
|
|
|
16
|
|
|
final class PurchaseClient |
17
|
|
|
{ |
18
|
|
|
const BASE_URL = 'https://purchase.izettle.com'; |
19
|
|
|
|
20
|
|
|
const GET_PURCHASE = self::BASE_URL . '/purchase/v2/%s'; |
21
|
|
|
const GET_PURCHASES = self::BASE_URL . '/purchases/v2'; |
22
|
|
|
|
23
|
|
|
private $client; |
24
|
|
|
private $purchaseHistoryBuilder; |
25
|
|
|
private $purchaseBuilder; |
26
|
|
|
public $paramters = []; |
27
|
|
|
|
28
|
5 |
|
public function __construct( |
29
|
|
|
IzettleClientInterface $client, |
30
|
|
|
PurchaseHistoryBuilderInterface $purchaseHistoryBuilder, |
31
|
|
|
PurchaseBuilderInterface $purchaseBuilder |
32
|
|
|
) { |
33
|
5 |
|
$this->client = $client; |
34
|
5 |
|
$this->purchaseHistoryBuilder = $purchaseHistoryBuilder; |
35
|
5 |
|
$this->purchaseBuilder = $purchaseBuilder; |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
public function setParameter($key, $value) |
39
|
|
|
{ |
40
|
|
|
$this->paramters[$key] = $value; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function limit(int $limit) |
45
|
|
|
{ |
46
|
|
|
$this->setParameter('limit', $limit); |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function descending(bool $boolean) |
51
|
|
|
{ |
52
|
|
|
$this->setParameter('desending', $boolean ? 'true' : 'false'); |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function getPurchasesUrl() |
57
|
|
|
{ |
58
|
2 |
|
return self::GET_PURCHASES . '?' . http_build_query($this->paramters); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function getPurchaseHistory(): PurchaseHistory |
62
|
|
|
{ |
63
|
2 |
|
$json = $this->client->getJson($this->client->get($this->getPurchasesUrl())); |
64
|
|
|
|
65
|
1 |
|
return $this->purchaseHistoryBuilder->buildFromJson($json); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function getPurchase(UuidInterface $uuid): Purchase |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
3 |
|
$response = $this->client->get(sprintf(self::GET_PURCHASE, (string) $uuid)); |
72
|
1 |
|
} catch (NotFoundException $e) { |
73
|
1 |
|
throw new PurchaseNotFoundException($e->getMessage()); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$json = $this->client->getJson($response); |
77
|
|
|
|
78
|
2 |
|
return $this->purchaseBuilder->buildFromJson($json); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|