Completed
Push — master ( 47778c...ba169d )
by Dan Michael O.
10:36
created

LazyResource::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
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
    public function __construct(Client $client)
25
    {
26
        parent::__construct($client);
27
    }
28
29
    /**
30
     * Check if we have the full representation of our data object.
31
     *
32
     * @param \stdClass $data
33
     * @return boolean
34
     */
35
    abstract protected function isInitialized($data);
36
37
    /**
38
     * Load data onto this object. Chainable method.
39
     *
40
     * @param \stdClass $data
41
     *
42
     * @return self
43
     */
44
    public function init($data = null)
45
    {
46
        if ($this->initialized) {
47
            return $this;
48
        }
49
50
        if (is_null($data)) {
51
            $data = $this->fetchData();
52
        }
53
54
        if (!is_null($data) && $this->isInitialized($data)) {
55
            $this->initialized = true;
56
        }
57
58
        $this->data = $data;
59
        if ($this->initialized) {
60
            $this->setData($data);
61
        }
62
63
        return $this;
64
    }
65
66
    /**
67
     * Get the model data.
68
     */
69
    protected function fetchData()
70
    {
71
        return $this->client->getJSON($this->url());
72
    }
73
74
    /**
75
     * Store data onto object. Can be overriden.
76
     *
77
     * @param mixed $data
78
     */
79
    protected function setData($data)
80
    {
81
    }
82
83
    /**
84
     * Get the raw data object.
85
     */
86
    public function getData()
87
    {
88
        return $this->init()->data;
89
    }
90
91
    /**
92
     * Check if the object exists.
93
     */
94
    public function exists()
95
    {
96
        try {
97
            $this->init();
98
        } catch (ResourceNotFound $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
99
        }
100
101
        return $this->initialized;
102
    }
103
104
    /**
105
     * Generate the base URL for this resource.
106
     *
107
     * @return string
108
     */
109
    abstract protected function urlBase();
110
111
    /**
112
     * Build a full URL for a resource.
113
     *
114
     * @param string $url
115
     * @param array $query
116
     * @return UriInterface
117
     */
118
    protected function url($url = '', $query = [])
119
    {
120
        return $this->client->buildUrl($this->urlBase() . $url, $query);
121
    }
122
}
123