|
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
|
|
|
* Code constructor. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $pageToken |
|
17
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
18
|
|
|
*/ |
|
19
|
5 |
|
public function __construct($pageToken, ClientInterface $client) |
|
20
|
|
|
{ |
|
21
|
5 |
|
parent::__construct($pageToken, $client); |
|
22
|
5 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param int $imageSize |
|
26
|
|
|
* @param string $codeType |
|
27
|
|
|
* @return CodeResponse |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function request(int $imageSize = 1000, string $codeType = self::CODE_TYPE_STANDARD): CodeResponse |
|
30
|
|
|
{ |
|
31
|
4 |
|
$this->isValidCodeImageSize($imageSize); |
|
32
|
2 |
|
$this->isValidCodeType($codeType); |
|
33
|
|
|
|
|
34
|
1 |
|
$request = new CodeRequest($this->pageToken, $imageSize, $codeType); |
|
35
|
1 |
|
$response = $this->client->post('me/messenger_codes', $request->build()); |
|
36
|
|
|
|
|
37
|
1 |
|
return new CodeResponse($response); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param int $imageSize |
|
42
|
|
|
* @throws \InvalidArgumentException |
|
43
|
|
|
*/ |
|
44
|
4 |
|
private function isValidCodeImageSize(int $imageSize) |
|
45
|
|
|
{ |
|
46
|
4 |
|
if ($imageSize < 100 || $imageSize > 2000) { |
|
47
|
2 |
|
throw new \InvalidArgumentException('$imageSize must be between 100 and 2000'); |
|
48
|
|
|
} |
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $codeType |
|
53
|
|
|
*/ |
|
54
|
2 |
|
private function isValidCodeType(string $codeType) |
|
55
|
|
|
{ |
|
56
|
2 |
|
$allowedCodeType = $this->getAllowedCodeType(); |
|
57
|
2 |
|
if (!in_array($codeType, $allowedCodeType)) { |
|
58
|
1 |
|
throw new \InvalidArgumentException('$codeType must be either ' . implode(', ', $allowedCodeType)); |
|
59
|
|
|
} |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
2 |
|
private function getAllowedCodeType(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
2 |
|
self::CODE_TYPE_STANDARD, |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
} |