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

GhostModel::init()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 1
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma;
4
5
use Psr\Http\Message\UriInterface;
6
use Scriptotek\Alma\Exception\ResourceNotFound;
7
8
abstract class GhostModel extends Model
9
{
10
    /**
11
     * This class is a ghost object that lazy loads the full record only when needed.
12
     * If $initialized is false, it means we haven't yet loaded the full record.
13
     * We can still have incomplete data from a search response.
14
     */
15
    protected $initialized = false;
16
17
    public function __construct(Client $client)
18
    {
19
        parent::__construct($client);
20
    }
21
22
    /**
23
     * Check if we have the full representation of our data object.
24
     *
25
     * @param \stdClass $data
26
     * @return boolean
27
     */
28
    abstract protected function isInitialized($data);
29
30
    /**
31
     * Load data onto this object. Chainable method.
32
     *
33
     * @param \stdClass $data
34
     *
35
     * @return self
36
     */
37
    public function init($data = null)
38
    {
39
        if ($this->initialized) {
40
            return $this;
41
        }
42
43
        if (is_null($data)) {
44
            $data = $this->fetchData();
45
        }
46
47
        if (!is_null($data) && $this->isInitialized($data)) {
48
            $this->initialized = true;
49
        }
50
51
        $this->data = $data;
52
        if ($this->initialized) {
53
            $this->setData($data);
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * Get the model data.
61
     */
62
    protected function fetchData()
63
    {
64
        return $this->client->getJSON($this->url());
65
    }
66
67
    /**
68
     * Store data onto object. Can be overriden.
69
     *
70
     * @param \stdClass $data
71
     */
72
    protected function setData(\stdClass $data)
73
    {
74
    }
75
76
    /**
77
     * Get the raw data object.
78
     */
79
    public function getData()
80
    {
81
        return $this->init()->data;
82
    }
83
84
    /**
85
     * Check if the object exists.
86
     */
87
    public function exists()
88
    {
89
        try {
90
            $this->init();
91
        } catch (ResourceNotFound $ex) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
92
93
        return $this->initialized;
94
    }
95
96
    /**
97
     * Generate the base URL for this resource.
98
     *
99
     * @return string
100
     */
101
    abstract protected function urlBase();
102
103
    /**
104
     * Build a full URL for a resource.
105
     *
106
     * @param string $url
107
     * @param array $query
108
     * @return UriInterface
109
     */
110
    protected function url($url = '', $query = [])
111
    {
112
        return $this->client->buildUrl($this->urlBase() . $url, $query);
113
    }
114
}