Completed
Push — master ( f49a8d...3fb39c )
by Romain
10s
created

User   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 44
rs 10
c 0
b 0
f 0
ccs 12
cts 12
cp 1

2 Methods

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