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

User::getAllowedFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kerox\Messenger\Api;
4
5
use GuzzleHttp\ClientInterface;
6
use Kerox\Messenger\Request\UserRequest;
7
use Kerox\Messenger\Response\UserResponse;
8
use Kerox\Messenger\UserInterface;
9
10
class User extends AbstractApi implements UserInterface
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
     *
32
     * @return \Kerox\Messenger\Api\User
33
     */
34 1
    public static function getInstance(string $pageToken, ClientInterface $client): User
35
    {
36 1
        if (self::$_instance === null) {
37 1
            self::$_instance = new self($pageToken, $client);
38
        }
39
40 1
        return self::$_instance;
41
    }
42
43
    /**
44
     * @param string     $userId
45
     * @param array|null $fields
46
     *
47
     * @return \Kerox\Messenger\Response\UserResponse
48
     */
49 2
    public function profile(string $userId, array $fields = []): UserResponse
50
    {
51 2
        $allowedFields = $this->getAllowedFields();
52 2
        if (!empty($fields)) {
53 1
            foreach ($fields as $field) {
54 1
                if (!in_array($field, $allowedFields, true)) {
55 1
                    throw new \InvalidArgumentException($field . ' is not a valid value. $fields must only contain ' . implode(', ', $allowedFields));
56
                }
57
            }
58
        } else {
59 1
            $fields = $allowedFields;
60
        }
61
62 1
        $request = new UserRequest($this->pageToken, $fields);
63 1
        $response = $this->client->get($userId, $request->build());
64
65 1
        return new UserResponse($response);
66
    }
67
68
    /**
69
     * @return array
70
     */
71 2
    private function getAllowedFields(): array
72
    {
73
        return [
74 2
            UserInterface::FIRST_NAME,
75 2
            UserInterface::LAST_NAME,
76 2
            UserInterface::PROFILE_PIC,
77 2
            UserInterface::LOCALE,
78 2
            UserInterface::TIMEZONE,
79 2
            UserInterface::GENDER,
80 2
            UserInterface::IS_PAYMENT_ENABLED,
81
        ];
82
    }
83
}
84