Getable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
eloc 14
c 3
b 1
f 0
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLoadPath() 0 3 1
A setLoadPath() 0 3 1
A getRequest() 0 13 3
A load() 0 7 1
1
<?php
2
3
namespace Lyal\Checkr\Traits;
4
5
trait Getable
6
{
7
    private $getPath;
8
9
    /**
10
     * Abstract functions to imppose requirements for the exhibiting class.
11
     */
12
    abstract public function getAttribute($key);
13
14
    abstract public function getResourceName($object = null);
15
16
    abstract public function getAttributes($sanitized = true);
17
18
    abstract public function processPath($path = null, array $values = null);
19
20
    abstract public function getClient();
21
22
    abstract public function setValues($values);
23
24
    abstract public function setAttributes(array $values);
25
26
    /**
27
     * Make a get request against the path.
28
     *
29
     * Note: options for Guzzle are set/handled on the Client
30
     *
31
     * @param string|null $path
32
     *
33
     * @return mixed
34
     */
35
    protected function getRequest($path = null, $parameters = null)
36
    {
37
        $parameters = $parameters ?? $this->getAttributes();
38
39
        if (method_exists($this, 'getEmbeddedResources')) {
40
            $parameters['include'] = $this->getEmbeddedResources();
41
        }
42
43
        if ($parameters) {
44
            $path .= '?'.http_build_query($parameters);
45
        }
46
47
        return $this->getClient()->request('get', $path);
48
    }
49
50
    /**
51
     * Load a resource via a get call.
52
     *
53
     * @param array|null $parameters
54
     *
55
     * @return $this
56
     */
57
    public function load(array $parameters = null)
58
    {
59
        $parameters = $parameters ?? ['id' => $this->getAttribute('id')];
60
        $path = $this->processPath($this->getLoadPath() ?? $this->getResourceName().'/'.$this->getAttribute('id'));
61
        $this->setValues($this->getRequest($path, $parameters));
62
63
        return $this;
64
    }
65
66
    public function getLoadPath()
67
    {
68
        return $this->getPath;
69
    }
70
71
    public function setLoadPath($path)
72
    {
73
        $this->getPath = $path;
74
    }
75
}
76