Completed
Push — master ( 32f37c...b12769 )
by Dan Michael O.
04:22
created

LazyResource::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Model;
4
5
use Psr\Http\Message\UriInterface;
6
use Scriptotek\Alma\Client;
7
use Scriptotek\Alma\Exception\ResourceNotFound;
8
9
/**
10
 * A LazyResource is anything that has its own URL and therefore can be lazy-loaded.
11
 * This class implements the basic ghost model functionality.
12
 *
13
 * The $initialized property indicates if the model is loaded or not.
14
 */
15
abstract class LazyResource extends Model
16
{
17
    /**
18
     * This class is a ghost object that lazy loads the full record only when needed.
19
     * If $initialized is false, it means we haven't yet loaded the full record.
20
     * We can still have incomplete data from a search response.
21
     */
22
    protected $initialized = false;
23
24
    /**
25
     * @var array Query string parameters
26
     */
27
    protected $params = [];
28
29
    /**
30
     * Set request query string parameters.
31
     * @param $params array
32
     * @return $this
33
     */
34
    public function setParams($params)
35
    {
36
        $this->params = $params;
37
        return $this;
38
    }
39
40
    /**
41
     * Get the request query string parameters.
42
     * @return array
43
     */
44
    public function getParams()
45
    {
46
        return $this->params;
47
    }
48
49
    /**
50
     * Check if we have the full representation of our data object.
51
     *
52
     * @param \stdClass $data
53
     * @return boolean
54
     */
55
    abstract protected function isInitialized($data);
56
57
    /**
58
     * Load data onto this object. Chainable method.
59
     *
60
     * @param \stdClass $data
61
     *
62
     * @return $this
63
     */
64
    public function init($data = null)
65
    {
66
        if ($this->initialized) {
67
            return $this;
68
        }
69
70
        if (is_null($data)) {
71
            $data = $this->fetchData();
72
        }
73
74
        if ($this->isInitialized($data)) {
75
            $this->initialized = true;
76
        }
77
78
        $this->data = $data;
79
        if ($this->initialized) {
80
            $this->onData($data);
81
        }
82
83
        return $this;
84
    }
85
86
    /**
87
     * Get and return the model data.
88
     * @return object
89
     */
90
    protected function fetchData()
91
    {
92
        return $this->client->getJSON($this->url());
93
    }
94
95
    /**
96
     * Called when data is available on the object.
97
     * The resource classes can use this method to process the data.
98
     *
99
     * @param mixed $data
100
     */
101
    protected function onData($data)
102
    {
103
    }
104
105
    /**
106
     * Get the raw data object.
107
     */
108
    public function getData()
109
    {
110
        return $this->init()->data;
111
    }
112
113
    /**
114
     * Check if the object exists.
115
     */
116
    public function exists()
117
    {
118
        try {
119
            $this->init();
120
        } catch (ResourceNotFound $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
121
        }
122
123
        return $this->initialized;
124
    }
125
126
    /**
127
     * Generate the base URL for this resource.
128
     *
129
     * @return string
130
     */
131
    abstract protected function urlBase();
132
133
    /**
134
     * Build a relative URL for a resource.
135
     *
136
     * @param string $path
137
     * @param array $query
138
     * @return string
139
     */
140
    protected function url($path = '', $query = [])
141
    {
142
        $path = $this->urlBase() . $path;
143
        $query = http_build_query(array_merge($this->params, $query));
144
145
        $url = $path;
146
        if (!empty($query)) {
147
            $url .= '?' . $query;
148
        }
149
150
        return $url;
151
    }
152
}
153