Passed
Push — master ( 8b12f2...70edcf )
by Romain
46s
created

Profile   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
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 109
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
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