Passed
Push — master ( 469104...8dd18e )
by Romain
38s
created

User   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 0
loc 85
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 8 2
A profile() 0 18 4
A getProfile() 0 4 1
A getAllowedFields() 0 12 1
1
<?php
2
namespace Kerox\Messenger\Api;
3
4
use GuzzleHttp\ClientInterface;
5
use Kerox\Messenger\Request\UserRequest;
6
use Kerox\Messenger\Response\UserResponse;
7
use Kerox\Messenger\UserInterface;
8
9
class User extends AbstractApi implements UserInterface
10
{
11
12
    /**
13
     * @var null|\Kerox\Messenger\Api\User
14
     */
15
    private static $_instance;
16
17
    /**
18
     * Send constructor.
19
     *
20
     * @param string $pageToken
21
     * @param \GuzzleHttp\ClientInterface $client
22
     */
23 3
    public function __construct(string $pageToken, ClientInterface $client)
24
    {
25 3
        parent::__construct($pageToken, $client);
26 3
    }
27
28
    /**
29
     * @param string $pageToken
30
     * @param \GuzzleHttp\ClientInterface $client
31
     * @return \Kerox\Messenger\Api\User
32
     */
33 1
    public static function getInstance(string $pageToken, ClientInterface $client): User
34
    {
35 1
        if (self::$_instance === null) {
36 1
            self::$_instance = new User($pageToken, $client);
37
        }
38
39 1
        return self::$_instance;
40
    }
41
42
    /**
43
     * @param string $userId
44
     * @param array|null $fields
45
     * @return \Kerox\Messenger\Response\UserResponse
46
     */
47 2
    public function profile(string $userId, array $fields = []): UserResponse
48
    {
49 2
        $allowedFields = $this->getAllowedFields();
50 2
        if (!empty($fields)) {
51 1
            foreach ($fields as $field) {
52 1
                if (!in_array($field, $allowedFields)) {
53 1
                    throw new \InvalidArgumentException($field . ' is not a valid value. $fields must only contain ' . implode(', ', $allowedFields));
54
                }
55
            }
56
        } else {
57 1
            $fields = $allowedFields;
58
        }
59
60 1
        $request = new UserRequest($this->pageToken, $fields);
61 1
        $response = $this->client->get($userId, $request->build());
62
63 1
        return new UserResponse($response);
64
    }
65
66
    /**
67
     * @deprecated since 1.2.0 and will be remove in 1.3.0.
68
     * @see profile()
69
     * @param string $userId
70
     * @param array $fields
71
     * @return \Kerox\Messenger\Response\UserResponse
72
     */
73
    public function getProfile(string $userId, array $fields = []): UserResponse
74
    {
75
        return $this->profile($userId, $fields);
76
    }
77
78
    /**
79
     * @return array
80
     */
81 2
    private function getAllowedFields(): array
82
    {
83
        return [
84 2
            UserInterface::FIRST_NAME,
85 2
            UserInterface::LAST_NAME,
86 2
            UserInterface::PROFILE_PIC,
87 2
            UserInterface::LOCALE,
88 2
            UserInterface::TIMEZONE,
89 2
            UserInterface::GENDER,
90 2
            UserInterface::IS_PAYMENT_ENABLED,
91
        ];
92
    }
93
}
94