Completed
Pull Request — master (#127)
by Alexandr
05:53
created

Profile::getAllowedFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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