Completed
Pull Request — master (#129)
by
unknown
02:29
created

Profile   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 28
dl 0
loc 83
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidFields() 0 9 3
A add() 0 6 1
A getAllowedFields() 0 10 1
A get() 0 10 1
A delete() 0 8 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
     * @return \Kerox\Messenger\Response\ProfileResponse
19
     */
20 1
    public function add(ProfileSettings $profileSettings): ProfileResponse
21
    {
22 1
        $request = new ProfileRequest($this->pageToken, $profileSettings);
23 1
        $response = $this->client->post('me/messenger_profile', $request->build());
24
25 1
        return new ProfileResponse($response);
26
    }
27
28
    /**
29
     * @param array $profileSettings
30
     *
31
     * @throws \Kerox\Messenger\Exception\MessengerException
32
     *
33
     * @return \Kerox\Messenger\Response\ProfileResponse
34
     */
35 1
    public function get(array $profileSettings): ProfileResponse
36
    {
37 1
        $this->isValidFields($profileSettings);
38
39 1
        $profileSettings = implode(',', $profileSettings);
40
41 1
        $request = new ProfileRequest($this->pageToken, $profileSettings);
42 1
        $response = $this->client->get('me/messenger_profile', $request->build());
43
44 1
        return new ProfileResponse($response);
45
    }
46
47
    /**
48
     * @param array $profileSettings
49
     *
50
     * @throws \Kerox\Messenger\Exception\MessengerException
51
     *
52
     * @return \Kerox\Messenger\Response\ProfileResponse
53
     */
54 2
    public function delete(array $profileSettings): ProfileResponse
55
    {
56 2
        $this->isValidFields($profileSettings);
57
58 1
        $request = new ProfileRequest($this->pageToken, $profileSettings);
59 1
        $response = $this->client->delete('me/messenger_profile', $request->build());
60
61 1
        return new ProfileResponse($response);
62
    }
63
64
    /**
65
     * @param array $fields
66
     *
67
     * @throws \Kerox\Messenger\Exception\MessengerException
68
     */
69 3
    private function isValidFields(array $fields): void
70
    {
71 3
        $allowedFields = $this->getAllowedFields();
72 3
        foreach ($fields as $field) {
73 3
            if (!\in_array($field, $allowedFields, true)) {
74 1
                throw new InvalidKeyException(sprintf(
75 1
                    '%s is not a valid value. fields must only contain "%s".',
76 1
                    $field,
77 3
                    implode(', ', $allowedFields)
78
                ));
79
            }
80
        }
81 2
    }
82
83
    /**
84
     * @return array
85
     */
86 3
    private function getAllowedFields(): array
87
    {
88
        return [
89 3
            self::PERSISTENT_MENU,
90 3
            self::GET_STARTED,
91 3
            self::GREETING,
92 3
            self::DOMAIN_WHITELISTING,
93 3
            self::ACCOUNT_LINKING_URL,
94 3
            self::PAYMENT_SETTINGS,
95 3
            self::TARGET_AUDIENCE,
96
        ];
97
    }
98
}
99