Test Failed
Push — master ( 7c4720...fd955c )
by Lyal
04:00 queued 14s
created

AbstractEntity::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
namespace Lyal\Checkr\Entities;
3
4
5
use Lyal\Checkr\Client;
6
use Lyal\Checkr\Exceptions\UnknownResourceException;
7
use Lyal\Checkr\Traits\Getable;
8
use Lyal\Checkr\Traits\HasAttributes;
9
10
abstract class AbstractEntity
11
{
12
    use HasAttributes, Getable;
13
14
    /**
15
     * Define the field allowed for each resource
16
     * Overridden in individual children
17
     *
18
     * @var array
19
     */
20
21
    protected $fields = [];
22
23
    /**
24
     * @var \Lyal\Checkr\Client $client
25
     */
26
    private $client;
27
28
    /**
29
     * AbstractEntity constructor.
30
     * @param string|NULL $values
31
     * @param Client|NULL $client
32
     */
33
34
    public function __construct($values = NULL, $client = NULL)
35
    {
36
        $this->setValues($values);
37
        $this->setClient($client);
0 ignored issues
show
Bug introduced by
It seems like $client can also be of type null; however, parameter $client of Lyal\Checkr\Entities\AbstractEntity::setClient() does only seem to accept Lyal\Checkr\Client, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $this->setClient(/** @scrutinizer ignore-type */ $client);
Loading history...
38
    }
39
40
    /**
41
     * Attach the previously queried resource as classname_id
42
     * Allows for magic querying
43
     *
44
     * @param AbstractEntity $object
45
     * @return void
46
     */
47
48
    public function setPreviousObject(AbstractEntity $object) : void
49
    {
50
        $objectId = strtolower((new \ReflectionClass($object))->getShortName()) . '_id';
51
        if (null !== $object->getAttribute('id') && $this->checkField($objectId)) {
52
            $this->setAttribute($objectId, $object->getAttribute('id'));
53
        }
54
    }
55
56
57
    /**
58
     * @param \StdClass|string $values
59
     *
60
     * return void
61
     */
62
63
    public function setValues($values) : void
64
    {
65
        /**
66
         * If we get a string, we assume that it's an ID here
67
         * to allow for loading objects easily
68
         */
69
        if (is_string($values)) {
70
            $this->setAttribute('id', $values);
71
            return;
72
        }
73
74
        foreach ((array)$values as $key => $value) {
75
            if (isset($value->object)) {
76
                $className = checkrEntityClassName($value->object);
77
                $value = new $className($value, $this->getClient());
78
                $value->setPreviousObject($this);
79
            }
80
81
            if (is_array($value)) {
82
                $list = collect($value);
83
                $value = $list;
84
85
            }
86
            $this->{$key} = $value;
87
        }
88
    }
89
90
91
    /**
92
     * @return mixed
93
     * @throws UnknownResourceException
94
     */
95
96
    public function __call($name, $args)
97
    {
98
        return $this->getClient()->api($name, $args, $this);
99
    }
100
101
    /**
102
     * Get the client object; Client also handles routing between resources
103
     *
104
     * @return \Lyal\Checkr\Client
105
     */
106
107
    public function getClient() : \Lyal\Checkr\Client
108
    {
109
        return $this->client;
110
    }
111
112
    /**
113
     * Set the client object; Client also handles routing between resources
114
     *
115
     * @param Client $client
116
     * @return void;
117
     */
118
119
    public function setClient(Client $client) : void
120
    {
121
        $this->client = $client;
122
    }
123
124
    /**
125
     * Get the resource name of an object
126
     *
127
     * AdverseAction becomes adverse_actions
128
     *
129
     * @param AbstractEntity $object
130
     * @return string
131
     */
132
133
    public function getResourceName($object = null) : string
134
    {
135
        $object = $object ?? $this;
136
        return snake_case(str_plural((new \ReflectionClass($object))->getShortName()));
137
    }
138
139
    /**
140
     * @param string|null $path
141
     * @param array|null $values
142
     * @return string
143
     */
144
    public function processPath($path = NULL, array $values = null) : string
145
    {
146
        $path = $path ?? $this->getResourceName();
147
        return str_replace_tokens($path, $values ?? $this->getAttributes(false));
148
    }
149
150
    /**
151
     * Set the fields for an object
152
     * @param array $fields
153
     */
154
155
156
    public function setFields($fields = [])
157
    {
158
        $this->fields = $fields;
159
    }
160
161
    /**
162
     * Return the fields for a resource
163
     *
164
     * @return array
165
     */
166
167
168
    public function getFields()
169
    {
170
        return $this->fields;
171
    }
172
173
174
}