Passed
Pull Request — master (#109)
by Romain
03:30
created

Profile::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Api;
6
7
use Kerox\Messenger\Model\ProfileSettings;
8
use Kerox\Messenger\ProfileInterface;
9
use Kerox\Messenger\Request\ProfileRequest;
10
use Kerox\Messenger\Response\ProfileResponse;
11
12
class Profile extends AbstractApi implements ProfileInterface
13
{
14
    /**
15
     * @param \Kerox\Messenger\Model\ProfileSettings $profileSettings
16
     *
17
     * @return \Kerox\Messenger\Response\ProfileResponse
18
     */
19 1
    public function add(ProfileSettings $profileSettings): ProfileResponse
20
    {
21 1
        $request = new ProfileRequest($this->pageToken, $profileSettings);
22 1
        $response = $this->client->post('me/messenger_profile', $request->build());
23
24 1
        return new ProfileResponse($response);
25
    }
26
27
    /**
28
     * @param array $profileSettings
29
     *
30
     * @throws \InvalidArgumentException
31
     *
32
     * @return \Kerox\Messenger\Response\ProfileResponse
33
     */
34 1
    public function get(array $profileSettings): ProfileResponse
35
    {
36 1
        $this->isValidFields($profileSettings);
37
38 1
        $profileSettings = implode(',', $profileSettings);
39
40 1
        $request = new ProfileRequest($this->pageToken, $profileSettings);
41 1
        $response = $this->client->get('me/messenger_profile', $request->build());
42
43 1
        return new ProfileResponse($response);
44
    }
45
46
    /**
47
     * @param array $profileSettings
48
     *
49
     * @throws \InvalidArgumentException
50
     *
51
     * @return \Kerox\Messenger\Response\ProfileResponse
52
     */
53 1
    public function delete(array $profileSettings): ProfileResponse
54
    {
55 1
        $this->isValidFields($profileSettings);
56
57
        $request = new ProfileRequest($this->pageToken, $profileSettings);
58
        $response = $this->client->delete('me/messenger_profile', $request->build());
59
60
        return new ProfileResponse($response);
61
    }
62
63
    /**
64
     * @param array $fields
65
     *
66
     * @throws \InvalidArgumentException
67
     */
68 2
    private function isValidFields(array $fields): void
69
    {
70 2
        $allowedFields = $this->getAllowedFields();
71 2
        foreach ($fields as $field) {
72 2
            if (!\in_array($field, $allowedFields, true)) {
73 1
                throw new \InvalidArgumentException(
74 2
                    $field . ' is not a valid value. $fields must only contain ' . implode(', ', $allowedFields)
75
                );
76
            }
77
        }
78 1
    }
79
80
    /**
81
     * @return array
82
     */
83 2
    private function getAllowedFields(): array
84
    {
85
        return [
86 2
            self::PERSISTENT_MENU,
87 2
            self::GET_STARTED,
88 2
            self::GREETING,
89 2
            self::DOMAIN_WHITELISTING,
90 2
            self::ACCOUNT_LINKING_URL,
91 2
            self::PAYMENT_SETTINGS,
92 2
            self::TARGET_AUDIENCE,
93
        ];
94
    }
95
}
96