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