Test Failed
Push — master ( a8b57d...533f3a )
by Dan Michael O.
03:50
created

User::init()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Users;
4
5
use Scriptotek\Alma\Client;
6
7
class User
8
{
9
    /**
10
     * This class is a ghost object that lazy loads the full record only when needed.
11
     * If $initialized is false, it means we haven't yet loaded the full record.
12
     * We can still have incomplete data from a search response.
13
     */
14
    protected $initialized = false;
15
16
    protected $client;
17
18
    protected $id;
19
20
    protected $_identifiers;
21
22
    /* @var \stdClass */
23
    protected $data;
24
25
    /**
26
     * Create a user from a search response containing an incomplete user record.
27
     *
28
     * @param Client    $client
29
     * @param \stdClass $data
30
     *
31
     * @return User
32
     */
33
    public static function fromSearchResponse(Client $client, \stdClass $data)
34
    {
35
        return (new self($client, $data->primary_id))
36
            ->init($data);
37
    }
38
39
    /**
40
     * User constructor.
41
     *
42
     * @param Client $client
43
     * @param string $id
44
     */
45
    public function __construct(Client $client, $id)
46
    {
47
        $this->client = $client;
48
        $this->id = $id;
49
    }
50
51
    /**
52
     * Load data on this User object. Chainable method.
53
     *
54
     * @param \stdClass $data
55
     *
56
     * @return User
57
     */
58
    public function init($data = null)
59
    {
60
        if ($this->initialized) {
61
            return $this;
62
        }
63
64
        if (is_null($data)) {
65
            $data = $this->client->getJSON('/users/' . $this->id);
66
        }
67
68
        if (isset($data->user_identifier)) {
69
            $this->_identifiers = new UserIdentifiers($data->primary_id, $data->user_identifier);
70
            $this->initialized = true;
71
        }
72
73
        $this->data = $data;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Get the complete user record.
80
     *
81
     * @return \stdClass
82
     */
83
    public function getData()
84
    {
85
        return $this->init()->data;
86
    }
87
88
    /**
89
     * Get the primary id. No need to load the full record for this.
90
     *
91
     * @return string|null
92
     */
93
    public function getPrimaryId()
94
    {
95
        return $this->primary_id;
0 ignored issues
show
Documentation introduced by
The property primary_id does not exist on object<Scriptotek\Alma\Users\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
    }
97
98
    /**
99
     * Get the user identifiers.
100
     *
101
     * @return UserIdentifier[]
102
     */
103
    public function getIdentifiers()
104
    {
105
        return $this->init()->_identifiers;
106
    }
107
108
    /**
109
     * Magic!
110
     */
111
    public function __get($key)
112
    {
113
        // If there's a getter method, call it.
114
        $method = 'get' . ucfirst($key);
115
        if (method_exists($this, $method)) {
116
            return $this->$method();
117
        }
118
119
        // If the property is defined in our data object, return it.
120
        if (isset($this->data->{$key})) {
121
            return $this->data->{$key};
122
        }
123
124
        // Load the full record if needed.
125
        $this->init();
126
127
        // If there's a getter method on the UserIdentifiers object
128
        // (getBarcode, getPrimaryId), call it.
129
        if (method_exists($this->identifiers, $method)) {
0 ignored issues
show
Bug introduced by
The property identifiers does not seem to exist. Did you mean _identifiers?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
130
            return $this->identifiers->$method();
0 ignored issues
show
Bug introduced by
The property identifiers does not seem to exist. Did you mean _identifiers?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
131
        }
132
133
        // Re-check if there's a property on our data object
134
        if (isset($this->data->{$key})) {
135
            return $this->data->{$key};
136
        }
137
    }
138
}
139