Completed
Pull Request — master (#3)
by Romain
01:47
created

User::getProfile()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 2
1
<?php
2
namespace Kerox\Messenger\Api;
3
4
use GuzzleHttp\Client;
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
     * Send constructor.
14
     *
15
     * @param string $pageToken
16
     */
17
    public function __construct(string $pageToken)
18
    {
19
        parent::__construct($pageToken);
20
    }
21
22
    /**
23
     * @param string $userId
24
     * @param array|null $fields
25
     * @return \Kerox\Messenger\Response\UserResponse
26
     */
27
    public function getProfile(string $userId, array $fields = null): UserResponse
28
    {
29
        $allowedFields = $this->getAllowedFields();
30
        if ($fields !== null) {
31
            foreach ($fields as $field) {
32
                if (!in_array($field, $allowedFields)) {
33
                    throw new \InvalidArgumentException($field . ' is not a valid value. $userProfileFields must only contain ' . implode(', ', $allowedFields));
34
                }
35
            }
36
        } else {
37
            $fields = $allowedFields;
38
        }
39
40
        $request = new UserRequest($this->pageToken, $fields);
41
        $response = $this->client->get(sprintf('/%s', $userId), $request->build());
42
43
        return new UserResponse($response);
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    private function getAllowedFields(): array
50
    {
51
        return [
52
            UserInterface::FIRST_NAME,
53
            UserInterface::LAST_NAME,
54
            UserInterface::PROFILE_PIC,
55
            UserInterface::LOCALE,
56
            UserInterface::TIMEZONE,
57
            UserInterface::GENDER,
58
            UserInterface::IS_PAYMENT_ENABLED,
59
        ];
60
    }
61
}
62