Completed
Push — master ( 7fa98e...01e874 )
by Dan Michael O.
02:23
created

User::__get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 18
rs 9.2
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
    protected $_user_id;
10
    protected $client;
11
    protected $data;
12
13
    public function __construct(Client $client = null, $user_id = null, $data = [])
14
    {
15
        $this->_user_id = $user_id;
16
        $this->client = $client;
17
        $this->data = $data;
18
        // $this->rows = new Rows($this->path, $this->client);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
19
    }
20
21
    public static function fromResponse(Client $client = null, $data)
22
    {
23
        return new User($client, $data->primary_id, $data);
24
    }
25
26
    public function hasFullRecord()
27
    {
28
        return isset($this->data->user_identifier);
29
    }
30
31
    public function fetch()
32
    {
33
        if ($this->hasFullRecord()) {
34
            return;
35
        }
36
        $data = $this->client->getJSON('/users/' . $this->_user_id);
37
        $this->data = $data;
38
    }
39
40
    public function getData()
41
    {
42
        return $this->data;
43
    }
44
45
    public function getIdOfType($id_type)
46
    {
47
        foreach ($this->user_identifier as $identifier) {
0 ignored issues
show
Documentation introduced by
The property user_identifier 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...
48
            if ($identifier->id_type->value == $id_type) {
49
                return $identifier->value;
50
            }
51
        }
52
        return null;
53
    }
54
55
    public function getBarcode()
56
    {
57
        return $this->getIdOfType('BARCODE');
58
    }
59
60
    public function getUniversityId()
61
    {
62
        return $this->getIdOfType('UNIV_ID');
63
    }
64
65
    public function getIds()
66
    {
67
        $ids = [$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...
68
        foreach ($this->user_identifier as $identifier) {
0 ignored issues
show
Documentation introduced by
The property user_identifier 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...
69
            $ids[] = $identifier->value;
70
        }
71
        return $ids;
72
    }
73
74
    public function __get($key)
75
    {
76
        $method = 'get' . ucfirst($key);
77
        if (method_exists($this, $method)) {
78
            return $this->$method();
79
        }
80
81
        if (isset($this->data->{$key})) {
82
            return $this->data->{$key};
83
        } else {
84
            // If initialized from a search, we don't have the full record.
85
            // Let's fetch it.
86
            $this->fetch();
87
            if (isset($this->data->{$key})) {
88
                return $this->data->{$key};
89
            }
90
        }
91
    }
92
}
93