Users::me()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Blackmine\Repository\Users;
6
7
use Blackmine\Model\CustomField;
8
use Blackmine\Model\User\Group;
9
use Blackmine\Model\User\Membership;
10
use Blackmine\Model\User\Role;
11
use Blackmine\Model\User\User;
12
use Blackmine\Repository\AbstractRepository;
13
use Blackmine\Repository\RepositoryInterface;
14
use JsonException;
15
use Blackmine\Exception\Api\AbstractApiException;
16
17
/**
18
 * @method User|null  get(mixed $id)
19
 */
20
class Users extends AbstractRepository
21
{
22
    public const API_ROOT = "users";
23
24
    public const USER_RELATION_MEMBERSHIPS = "memberships";
25
    public const USER_RELATION_GROUPS = "groups";
26
    public const USER_RELATION_ROLES = "roles";
27
    public const USER_RELATION_CUSTOM_FIELDS = "custom_fields";
28
29
    public const USER_FILTER_NAME = "name";
30
    public const USER_FILTER_GROUP_ID = "group_id";
31
32
    protected static array $relation_class_map = [
33
        self::USER_RELATION_MEMBERSHIPS => Membership::class,
34
        self::USER_RELATION_GROUPS => Group::class,
35
        self::USER_RELATION_ROLES => Role::class,
36
        self::USER_RELATION_CUSTOM_FIELDS => CustomField::class
37
    ];
38
39
    protected static array $allowed_filters = [
40
        self::USER_FILTER_NAME => RepositoryInterface::SEARCH_PARAM_TYPE_STRING,
41
        self::USER_FILTER_GROUP_ID => RepositoryInterface::SEARCH_PARAM_TYPE_INT
42
    ];
43
44
    public function getModelClass(): string
45
    {
46
        return User::class;
47
    }
48
49
    /**
50
     * @throws JsonException
51
     * @throws AbstractApiException
52
     */
53
    public function me(): ?User
54
    {
55
        $endpoint_url = "my/account." . $this->client->getFormat();
56
        $api_response = $this->client->get($this->constructEndpointUrl($endpoint_url, []));
57
58
        if ($api_response->isSuccess()) {
59
            $model_class = $this->getModelClass();
60
            $model = new $model_class();
61
            $model->fromArray($api_response->getData()[$model->getEntityName()]);
62
63
            return $model;
64
        }
65
66
        throw AbstractApiException::fromApiResponse($api_response);
67
    }
68
}
69