Passed
Pull Request — master (#63)
by Romain
03:07
created

Profile::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.216

Importance

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