Passed
Push — master ( 5cd32c...1a1aa9 )
by Rafael
05:46
created

ID::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Model;
12
13 1
trigger_error(
14 1
    'ID class: has been deprecated since v1.1 and will be removed in v2.0, use IDEncoder util instead',
15 1
    E_USER_DEPRECATED
16
);
17
18
/**
19
 * ID are formed with base64 representation of the Types and real database ID
20
 * in order to create a unique and global identifier for each resource
21
 *
22
 * @see https://facebook.github.io/relay/docs/graphql-object-identification.html
23
 *
24
 * @deprecated since v1.1, use IDEncoder instead to allow the use of custom encoders
25
 */
26
class ID
27
{
28
    private const DIVIDER = ':';
29
30
    /**
31
     * @var mixed
32
     */
33
    protected $databaseId;
34
35
    /**
36
     * @var string
37
     */
38
    protected $nodeType;
39
40
    /**
41
     * @var string
42
     */
43
    protected $class;
44
45
    /**
46
     * ID constructor.
47
     *
48
     * @param string $nodeType
49
     * @param mixed  $databaseId
50
     */
51
    public function __construct($nodeType, $databaseId)
52
    {
53
        $this->databaseId = $databaseId;
54
        $this->nodeType = $nodeType;
55
    }
56
57
    /**
58
     * @param string $nodeType
59
     * @param mixed  $databaseId
60
     *
61
     * @return ID
62
     *
63
     * @deprecated since v1.1 IDEncoder instead
64
     */
65
    public static function create($nodeType, $databaseId): ID
66
    {
67
        return new self($nodeType, $databaseId);
68
    }
69
70
    /**
71
     * @param string $globalIdentifier
72
     *
73
     * @return ID
74
     *
75
     * @deprecated since v1.1 IDEncoder::decode() instead
76
     */
77
    public static function createFromString($globalIdentifier)
78
    {
79
        trigger_error(
80
            'IDEncoder::createFromString has been deprecated since v1.1 and will be removed v2.0, use IDEncoder::decode() instead',
81
            E_USER_DEPRECATED
82
        );
83
84
        $typeAndUser = base64_decode($globalIdentifier);
85
        if (strpos($typeAndUser, self::DIVIDER) > 1) {
86
            list($nodeType, $databaseId) = explode(self::DIVIDER, $typeAndUser);
87
        } else {
88
            $nodeType = null;
89
            $databaseId = null;
90
        }
91
92
        return new self($nodeType, $databaseId);
93
    }
94
95
    /**
96
     * @param string $nodeType
97
     * @param mixed  $databaseId
98
     *
99
     * @return string
100
     */
101
    public static function encode($nodeType, $databaseId)
102
    {
103
        trigger_error(
104
            'IDEncoder::encode has been deprecated since v1.1 and will be removed in v2.0, use IDEncoder::encode() instead',
105
            E_USER_DEPRECATED
106
        );
107
108
        return base64_encode(sprintf('%s%s%s', $nodeType, self::DIVIDER, $databaseId));
109
    }
110
111
    /**
112
     * @return mixed
113
     */
114
    public function getDatabaseId()
115
    {
116
        return $this->databaseId;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getNodeType(): ?string
123
    {
124
        return $this->nodeType;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function __toString()
131
    {
132
        return self::encode($this->getNodeType(), $this->getDatabaseId());
133
    }
134
}
135