Test Failed
Push — master ( c6b6d3...528954 )
by Dan Michael O.
03:21
created

User::__get()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Users;
4
5
use Scriptotek\Alma\Client;
6
use Scriptotek\Alma\GhostModel;
7
8
class User extends GhostModel
9
{
10
    /**
11
     * The primary id or some other id that can be used to fetch user information.
12
     * @var string
13
     */
14
    public $id;
15
16
    /**
17
     * @var UserIdentifiers
18
     */
19
    protected $_identifiers;
20
21
    /**
22
     * User constructor.
23
     *
24
     * @param Client $client
25
     * @param string $id
26
     */
27
    public function __construct(Client $client, $id)
28
    {
29
        parent::__construct($client);
30
        $this->id = $id;
31
    }
32
33
    /**
34
     * Get the user id the object was constructed with. This might or might not be the primary id.
35
     * The only usefulness of this method over getPrimaryId() is that it will not trigger loading of the full object.
36
     *
37
     * @return string
38
     */
39
    public function getUserId()
40
    {
41
        return $this->id;
42
    }
43
44
    /**
45
     * Get the primary id. No need to load the full record for this.
46
     *
47
     * @return string|null
48
     */
49
    public function getPrimaryId()
50
    {
51
        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...
52
    }
53
54
    /**
55
     * Get the user identifiers.
56
     *
57
     * @return UserIdentifiers
58
     */
59
    public function getIdentifiers()
60
    {
61
        return $this->init()->_identifiers;
62
    }
63
64
    /**
65
     * Check if we have the full representation of our data object.
66
     *
67
     * @param \stdClass $data
68
     * @return boolean
69
     */
70
    protected function isInitialized($data)
71
    {
72
        return isset($data->user_identifier);
73
    }
74
75
    /**
76
     * Store data onto object.
77
     *
78
     * @param \stdClass $data
79
     */
80
    protected function setData(\stdClass $data)
81
    {
82
        $this->_identifiers = UserIdentifiers::make($this->client, $data);
83
    }
84
85
86
    /**
87
     * Generate the base URL for this resource.
88
     *
89
     * @return string
90
     */
91
    protected function urlBase()
92
    {
93
        return "/users/{$this->id}";
94
    }
95
96
    public function __get($key)
97
    {
98
        // If there's a getter method, call it.
99
        $method = 'get' . ucfirst($key);
100
        if (method_exists($this, $method)) {
101
            return $this->$method();
102
        }
103
104
        // If the property is defined in our data object, return it.
105
        if (isset($this->data->{$key})) {
106
            return $this->data->{$key};
107
        }
108
109
        // Load the full record if needed.
110
        $this->init();
111
112
        // If there's a getter method on the UserIdentifiers object
113
        // (getBarcode, getPrimaryId), call it.
114
        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...
115
            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...
116
        }
117
118
        // Re-check if there's a property on our data object
119
        if (isset($this->data->{$key})) {
120
            return $this->data->{$key};
121
        }
122
123
        return null;
124
    }
125
}
126