Completed
Pull Request — master (#49)
by Romain
02:29
created

Code::isValidRef()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Kerox\Messenger\Api;
4
5
use GuzzleHttp\ClientInterface;
6
use Kerox\Messenger\Request\CodeRequest;
7
use Kerox\Messenger\Response\CodeResponse;
8
9
class Code extends AbstractApi
10
{
11
12
    const CODE_TYPE_STANDARD = 'standard';
13
14
    /**
15
     * @var null|\Kerox\Messenger\Api\Code
16
     */
17
    private static $_instance;
18
19
    /**
20
     * Code constructor.
21
     *
22
     * @param string $pageToken
23
     * @param \GuzzleHttp\ClientInterface $client
24
     */
25 6
    public function __construct(string $pageToken, ClientInterface $client)
26
    {
27 6
        parent::__construct($pageToken, $client);
28 6
    }
29
30
    /**
31
     * @param string $pageToken
32
     * @param \GuzzleHttp\ClientInterface $client
33
     * @return \Kerox\Messenger\Api\Code
34
     */
35 1
    public static function getInstance(string $pageToken, ClientInterface $client): Code
36
    {
37 1
        if (self::$_instance === null) {
38 1
            self::$_instance = new Code($pageToken, $client);
39
        }
40
41 1
        return self::$_instance;
42
    }
43
44
    /**
45
     * @param int $imageSize
46
     * @param string $codeType
47
     * @param string|null $ref
48
     * @return \Kerox\Messenger\Response\CodeResponse
49
     */
50 5
    public function request(int $imageSize = 1000, string $codeType = self::CODE_TYPE_STANDARD, string $ref = null): CodeResponse
51
    {
52 5
        $this->isValidCodeImageSize($imageSize);
53 3
        $this->isValidCodeType($codeType);
54
55 2
        if ($ref !== null) {
56 2
            $this->isValidRef($ref);
57
        }
58
59 1
        $request = new CodeRequest($this->pageToken, $imageSize, $codeType, $ref);
60 1
        $response = $this->client->post('me/messenger_codes', $request->build());
61
62 1
        return new CodeResponse($response);
63
    }
64
65
    /**
66
     * @param int $imageSize
67
     * @throws \InvalidArgumentException
68
     */
69 5
    private function isValidCodeImageSize(int $imageSize)
70
    {
71 5
        if ($imageSize < 100 || $imageSize > 2000) {
72 2
            throw new \InvalidArgumentException('$imageSize must be between 100 and 2000');
73
        }
74 3
    }
75
76
    /**
77
     * @param string $codeType
78
     * @throws \InvalidArgumentException
79
     */
80 3
    private function isValidCodeType(string $codeType)
81
    {
82 3
        $allowedCodeType = $this->getAllowedCodeType();
83 3
        if (!in_array($codeType, $allowedCodeType)) {
84 1
            throw new \InvalidArgumentException('$codeType must be either ' . implode(', ', $allowedCodeType));
85
        }
86 2
    }
87
88
    /**
89
     * @param string $ref
90
     * @throws \InvalidArgumentException
91
     */
92 2
    private function isValidRef(string $ref)
93
    {
94 2
        if (!preg_match('/^[a-zA-Z0-9\+\/=\-.:_ ]{1,250}$/', $ref)) {
95 1
            throw new \InvalidArgumentException('$ref must be a string of max 250 characters. Valid characters are a-z A-Z 0-9 +/=-.:_');
96
        }
97 1
    }
98
99
    /**
100
     * @return array
101
     */
102 3
    private function getAllowedCodeType(): array
103
    {
104
        return [
105 3
            self::CODE_TYPE_STANDARD,
106
        ];
107
    }
108
}
109