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

Code::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
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
}