Completed
Push — master ( 4c1222...393226 )
by Romain
10s
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 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
    public function add(ProfileSettings $profileSettings): ProfileResponse
20
    {
21
        $request = new ProfileRequest($this->pageToken, $profileSettings);
22
        $response = $this->client->post('me/messenger_profile', $request->build());
23
24 4
        return new ProfileResponse($response);
25
    }
26 4
27 4
    /**
28
     * @param array $profileSettings
29
     *
30
     * @throws \InvalidArgumentException
31
     *
32
     * @return \Kerox\Messenger\Response\ProfileResponse
33
     */
34
    public function get(array $profileSettings): ProfileResponse
35 1
    {
36
        $this->isValidFields($profileSettings);
37 1
38 1
        $profileSettings = implode(',', $profileSettings);
39
40
        $request = new ProfileRequest($this->pageToken, $profileSettings);
41 1
        $response = $this->client->get('me/messenger_profile', $request->build());
42
43
        return new ProfileResponse($response);
44
    }
45
46
    /**
47
     * @param array $profileSettings
48
     *
49 1
     * @throws \InvalidArgumentException
50
     *
51 1
     * @return \Kerox\Messenger\Response\ProfileResponse
52 1
     */
53
    public function delete(array $profileSettings): ProfileResponse
54 1
    {
55
        $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 1
63
    /**
64 1
     * @param array $fields
65
     *
66 1
     * @throws \InvalidArgumentException
67
     */
68 1
    private function isValidFields(array $fields): void
69 1
    {
70
        $allowedFields = $this->getAllowedFields();
71 1
        foreach ($fields as $field) {
72
            if (!\in_array($field, $allowedFields, true)) {
73
                throw new \InvalidArgumentException($field . ' is not a valid value. $fields must only contain ' . implode(', ',
74
                        $allowedFields));
75
            }
76
        }
77
    }
78
79 1
    /**
80
     * @return array
81 1
     */
82
    private function getAllowedFields(): array
83
    {
84
        return [
85
            ProfileInterface::PERSISTENT_MENU,
86
            ProfileInterface::GET_STARTED,
87
            ProfileInterface::GREETING,
88
            ProfileInterface::DOMAIN_WHITELISTING,
89
            ProfileInterface::ACCOUNT_LINKING_URL,
90
            ProfileInterface::PAYMENT_SETTINGS,
91
            ProfileInterface::TARGET_AUDIENCE,
92
        ];
93
    }
94
}
95