|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kerox\Messenger\Api; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\ClientInterface; |
|
5
|
|
|
use Kerox\Messenger\Request\CodeRequest; |
|
6
|
|
|
use Kerox\Messenger\Response\CodeResponse; |
|
7
|
|
|
|
|
8
|
|
|
class Code extends AbstractApi |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
const CODE_TYPE_STANDARD = 'standard'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var null|\Kerox\Messenger\Api\Code |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $_instance; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Code constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $pageToken |
|
22
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
23
|
|
|
*/ |
|
24
|
5 |
|
public function __construct(string $pageToken, ClientInterface $client) |
|
25
|
|
|
{ |
|
26
|
5 |
|
parent::__construct($pageToken, $client); |
|
27
|
5 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $pageToken |
|
31
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
32
|
|
|
* @return \Kerox\Messenger\Api\Code |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public static function getInstance(string $pageToken, ClientInterface $client): Code |
|
35
|
|
|
{ |
|
36
|
1 |
|
if (self::$_instance === null) { |
|
37
|
1 |
|
self::$_instance = new Code($pageToken, $client); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
return self::$_instance; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int $imageSize |
|
45
|
|
|
* @param string $codeType |
|
46
|
|
|
* @return CodeResponse |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function request(int $imageSize = 1000, string $codeType = self::CODE_TYPE_STANDARD): CodeResponse |
|
49
|
|
|
{ |
|
50
|
4 |
|
$this->isValidCodeImageSize($imageSize); |
|
51
|
2 |
|
$this->isValidCodeType($codeType); |
|
52
|
|
|
|
|
53
|
1 |
|
$request = new CodeRequest($this->pageToken, $imageSize, $codeType); |
|
54
|
1 |
|
$response = $this->client->post('me/messenger_codes', $request->build()); |
|
55
|
|
|
|
|
56
|
1 |
|
return new CodeResponse($response); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param int $imageSize |
|
61
|
|
|
* @throws \InvalidArgumentException |
|
62
|
|
|
*/ |
|
63
|
4 |
|
private function isValidCodeImageSize(int $imageSize) |
|
64
|
|
|
{ |
|
65
|
4 |
|
if ($imageSize < 100 || $imageSize > 2000) { |
|
66
|
2 |
|
throw new \InvalidArgumentException('$imageSize must be between 100 and 2000'); |
|
67
|
|
|
} |
|
68
|
2 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $codeType |
|
72
|
|
|
*/ |
|
73
|
2 |
|
private function isValidCodeType(string $codeType) |
|
74
|
|
|
{ |
|
75
|
2 |
|
$allowedCodeType = $this->getAllowedCodeType(); |
|
76
|
2 |
|
if (!in_array($codeType, $allowedCodeType)) { |
|
77
|
1 |
|
throw new \InvalidArgumentException('$codeType must be either ' . implode(', ', $allowedCodeType)); |
|
78
|
|
|
} |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
2 |
|
private function getAllowedCodeType(): array |
|
85
|
|
|
{ |
|
86
|
|
|
return [ |
|
87
|
2 |
|
self::CODE_TYPE_STANDARD, |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|