UsersResolvers::resolveEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace ByTIC\Hello\Models\Users\Resolvers;
4
5
use ByTIC\Hello\Models\Users\Traits\UserTrait;
6
use League\OAuth2\Server\Entities\UserEntityInterface;
7
use Nip\Records\Locator\ModelLocator;
8
use Nip\Records\Record;
9
10
/**
11
 * Class UsersResolvers
12
 * @package ByTIC\Hello\Models\Users\Resolvers
13
 */
14
class UsersResolvers
15
{
16
    public const SEPARATOR = '|';
17
18
    /**
19
     * @param Record|UserEntityInterface|UserTrait $entity
20
     * @return string
21
     */
22 1
    public static function identifier(Record $entity)
23
    {
24 1
        return $entity->getManager()->getTable() . static::SEPARATOR . $entity->getPrimaryKey();
25
    }
26
27
    /**
28
     * @param string $identifier
29
     * @return string
30
     */
31
    public static function resolve(string $identifier)
32
    {
33
        list($userTable, $userIdentifier) = static::parseIdentifier($identifier);
34
        return static::resolveEntity($userTable, $userIdentifier);
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::resolveEn...Table, $userIdentifier) returns the type Nip\Records\AbstractModels\Record which is incompatible with the documented return type string.
Loading history...
35
    }
36
37
38
    /**
39
     * @param $userTable
40
     * @param $userIdentifier
41
     * @return \Nip\Records\AbstractModels\Record
42
     */
43
    protected static function resolveEntity($userTable, $userIdentifier)
44
    {
45
        $userRepository = ModelLocator::get($userTable);
46
        return $userRepository->findOne($userIdentifier);
47
    }
48
49
    /**
50
     * @param string $identifier
51
     * @return array
52
     */
53
    protected static function parseIdentifier($identifier)
54
    {
55
        if (strpos($identifier, static::class)) {
56
            $identifier = static::class . $identifier;
57
        }
58
        list($userTable, $userIdentifier) = explode(static::SEPARATOR, $identifier);
59
        $userTable = empty($userTable) ? 'users' : $userTable;
60
        return [$userTable, $userIdentifier];
61
    }
62
}
63