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