Passed
Push — master ( 195aeb...469104 )
by Romain
36s
created

Profile   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 3
dl 0
loc 105
ccs 33
cts 36
cp 0.9167
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 8 2
A add() 0 7 1
A get() 0 11 1
A delete() 0 9 1
A isValidFields() 0 9 3
A getAllowedFields() 0 12 1
1
<?php
2
namespace Kerox\Messenger\Api;
3
4
use GuzzleHttp\ClientInterface;
5
use Kerox\Messenger\Model\ProfileSettings;
6
use Kerox\Messenger\ProfileInterface;
7
use Kerox\Messenger\Request\ProfileRequest;
8
use Kerox\Messenger\Response\ProfileResponse;
9
10
class Profile extends AbstractApi implements ProfileInterface
11
{
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
     * @return \Kerox\Messenger\Api\Profile
33
     */
34 1
    public static function getInstance(string $pageToken, ClientInterface $client): Profile
35
    {
36 1
        if (self::$_instance === null) {
37 1
            self::$_instance = new Profile($pageToken, $client);
38
        }
39
40 1
        return self::$_instance;
41
    }
42
43
    /**
44
     * @param \Kerox\Messenger\Model\ProfileSettings $profileSettings
45
     * @return \Kerox\Messenger\Response\ProfileResponse
46
     */
47 1
    public function add(ProfileSettings $profileSettings): ProfileResponse
48
    {
49 1
        $request = new ProfileRequest($this->pageToken, $profileSettings);
50 1
        $response = $this->client->post('me/messenger_profile', $request->build());
51
52 1
        return new ProfileResponse($response);
53
    }
54
55
    /**
56
     * @param array $profileSettings
57
     * @return \Kerox\Messenger\Response\ProfileResponse
58
     */
59 1
    public function get(array $profileSettings): ProfileResponse
60
    {
61 1
        $this->isValidFields($profileSettings);
62
63 1
        $profileSettings = implode(',', $profileSettings);
64
65 1
        $request = new ProfileRequest($this->pageToken, $profileSettings);
66 1
        $response = $this->client->get('me/messenger_profile', $request->build());
67
68 1
        return new ProfileResponse($response);
69
    }
70
71
    /**
72
     * @param array $profileSettings
73
     * @return \Kerox\Messenger\Response\ProfileResponse
74
     */
75 1
    public function delete(array $profileSettings): ProfileResponse
76
    {
77 1
        $this->isValidFields($profileSettings);
78
79
        $request = new ProfileRequest($this->pageToken, $profileSettings);
80
        $response = $this->client->delete('me/messenger_profile', $request->build());
81
82
        return new ProfileResponse($response);
83
    }
84
85
    /**
86
     * @param array $fields
87
     * @throws \InvalidArgumentException
88
     */
89 2
    private function isValidFields(array $fields)
90
    {
91 2
        $allowedFields = $this->getAllowedFields();
92 2
        foreach ($fields as $field) {
93 2
            if (!in_array($field, $allowedFields)) {
94 2
                throw new \InvalidArgumentException($field . ' is not a valid value. $fields must only contain ' . implode(', ', $allowedFields));
95
            }
96
        }
97 1
    }
98
99
    /**
100
     * @return array
101
     */
102 2
    private function getAllowedFields(): array
103
    {
104
        return [
105 2
            ProfileInterface::PERSISTENT_MENU,
106 2
            ProfileInterface::GET_STARTED,
107 2
            ProfileInterface::GREETING,
108 2
            ProfileInterface::DOMAIN_WHITELISTING,
109 2
            ProfileInterface::ACCOUNT_LINKING_URL,
110 2
            ProfileInterface::PAYMENT_SETTINGS,
111 2
            ProfileInterface::TARGET_AUDIENCE,
112
        ];
113
    }
114
}
115