Result::__set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace Akkroo;
3
4
use LogicException;
5
use JsonSerializable;
6
7
/**
8
 * A basic response result object
9
 *
10
 * @property string access_token    Authentication token for successful login results
11
 * @property int    expires_in      Authentication token duration for successful login results
12
 * @property string refresh_token   Refresh token for successful login results
13
 * @property string requestID       Identifier for an HTTP request
14
 */
15
class Result implements JsonSerializable
16
{
17
    /**
18
     * Result data payload
19
     * @var array
20
     */
21
    protected $data = [];
22
23
    /**
24
     * @var string|null
25
     */
26
    protected $requestID = null;
27
28 35
    public function __construct(array $data = [])
29
    {
30 35
        if (isset($data['requestID'])) {
31 1
            $this->requestID = $data['requestID'];
32 1
            unset($data['requestID']);
33
        }
34 35
        $this->data = $data;
35 35
    }
36
37 20
    public function __get(string $name)
38
    {
39 20
        if ('requestID' === $name) {
40 11
            return $this->requestID;
41
        }
42 19
        if (array_key_exists($name, $this->data)) {
43 18
            return $this->data[$name];
44
        }
45 2
        return null;
46
    }
47
48 3
    public function __isset(string $name)
49
    {
50 3
        if ('requestID' === $name) {
51 1
            return isset($this->requestID);
52
        }
53 2
        return array_key_exists($name, $this->data);
54
    }
55
56 1
    public function __set(string $name, $value)
57
    {
58 1
        throw new LogicException('Cannot add properties to a read-only result');
59
    }
60
61 1
    public function __unset(string $name)
62
    {
63 1
        throw new LogicException('Cannot remove properties from a read-only result');
64
    }
65
66
    /**
67
     * Customize JSON encode output
68
     *
69
     * @return array
70
     */
71 2
    public function jsonSerialize()
72
    {
73 2
        return $this->toArray();
74
    }
75
76
    /**
77
     * Link a result to an API request
78
     *
79
     * @param string $value A request ID to associate
80
     * @return Result
81
     */
82 16
    public function withRequestID(string $value)
83
    {
84 16
        $this->requestID = $value;
85 16
        return $this;
86
    }
87
88
    /**
89
     * Export internal data as array
90
     *
91
     * @return array
92
     */
93 3
    public function toArray()
94
    {
95 3
        return array_merge($this->data, ['requestID' => $this->requestID]);
96
    }
97
}
98