Passed
Pull Request — master (#91)
by Romain
02:54
created

User::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Api;
6
7
use GuzzleHttp\ClientInterface;
8
use Kerox\Messenger\Request\UserRequest;
9
use Kerox\Messenger\Response\UserResponse;
10
use Kerox\Messenger\UserInterface;
11
12
class User extends AbstractApi implements UserInterface
13
{
14
    /**
15
     * @param string     $userId
16
     * @param array|null $fields
17
     *
18
     * @throws \InvalidArgumentException
19
     *
20
     * @return \Kerox\Messenger\Response\UserResponse
21
     */
22
    public function profile(string $userId, array $fields = []): UserResponse
23 3
    {
24
        $allowedFields = $this->getAllowedFields();
25 3
        if (!empty($fields)) {
26 3
            foreach ($fields as $field) {
27
                if (!\in_array($field, $allowedFields, true)) {
28
                    throw new \InvalidArgumentException($field . ' is not a valid value. $fields must only contain ' . implode(', ', $allowedFields));
29
                }
30
            }
31
        } else {
32
            $fields = $allowedFields;
33
        }
34 1
35
        $request = new UserRequest($this->pageToken, $fields);
36 1
        $response = $this->client->get($userId, $request->build());
37 1
38
        return new UserResponse($response);
39
    }
40 1
41
    /**
42
     * @return array
43
     */
44
    private function getAllowedFields(): array
45
    {
46
        return [
47
            UserInterface::FIRST_NAME,
48
            UserInterface::LAST_NAME,
49 2
            UserInterface::PROFILE_PIC,
50
            UserInterface::LOCALE,
51 2
            UserInterface::TIMEZONE,
52 2
            UserInterface::GENDER,
53 1
            UserInterface::IS_PAYMENT_ENABLED,
54 1
        ];
55 1
    }
56
}
57