Completed
Pull Request — master (#56)
by Romain
02:22 queued 14s
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
    /**
14
     * @var null|\Kerox\Messenger\Api\User
15
     */
16
    private static $_instance;
17
18
    /**
19
     * Send constructor.
20
     *
21
     * @param string $pageToken
22
     * @param \GuzzleHttp\ClientInterface $client
23
     */
24 3
    public function __construct(string $pageToken, ClientInterface $client)
25
    {
26 3
        parent::__construct($pageToken, $client);
27 3
    }
28
29
    /**
30
     * @param string $pageToken
31
     * @param \GuzzleHttp\ClientInterface $client
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 User($pageToken, $client);
38
        }
39
40 1
        return self::$_instance;
41
    }
42
43
    /**
44
     * @param string $userId
45
     * @param array|null $fields
46
     * @return \Kerox\Messenger\Response\UserResponse
47
     */
48 2
    public function profile(string $userId, array $fields = []): UserResponse
49
    {
50 2
        $allowedFields = $this->getAllowedFields();
51 2
        if (!empty($fields)) {
52 1
            foreach ($fields as $field) {
53 1
                if (!in_array($field, $allowedFields)) {
54 1
                    throw new \InvalidArgumentException($field . ' is not a valid value. $fields must only contain ' . implode(', ', $allowedFields));
55
                }
56
            }
57
        } else {
58 1
            $fields = $allowedFields;
59
        }
60
61 1
        $request = new UserRequest($this->pageToken, $fields);
62 1
        $response = $this->client->get($userId, $request->build());
63
64 1
        return new UserResponse($response);
65
    }
66
67
    /**
68
     * @return array
69
     */
70 2
    private function getAllowedFields(): array
71
    {
72
        return [
73 2
            UserInterface::FIRST_NAME,
74 2
            UserInterface::LAST_NAME,
75 2
            UserInterface::PROFILE_PIC,
76 2
            UserInterface::LOCALE,
77 2
            UserInterface::TIMEZONE,
78 2
            UserInterface::GENDER,
79 2
            UserInterface::IS_PAYMENT_ENABLED,
80
        ];
81
    }
82
}
83