Passed
Pull Request — master (#91)
by Romain
02:54
created

Profile::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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