Passed
Push — develop ( 2d7b42...b3143e )
by Brent
03:12
created

Page::removeAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Site;
4
5
use Brendt\Html\Meta\Meta;
6
use Brendt\Stitcher\Exception\TemplateNotFoundException;
7
use Brendt\Stitcher\Adapter\Adapter;
8
use Brendt\Stitcher\Site\Http\Header;
9
10
/**
11
 * A Page object represents a page entry configured in a YAML file located in the `src/sites/` directory.
12
 * Constructing a new Page requires a unique ID and an array of data. This array can hold several different arguments:
13
 *
14
 *      - `template`: the only required argument. This variable is a path to a template file.
15
 *              This path is relative to the `directories.src` or `directories.template` configuration entry
16
 * @see     \Brendt\Stitcher\Stitcher::loadTemplates
17
 *
18
 *
19
 *      - `data`: an optional array of variables which will be mapped onto the template.
20
 *              Each of these variables is parsed during compile time.
21
 * @see     \Brendt\Stitcher\Parser\AbstractParser
22
 *
23
 *      - `adapters`: an optional array of Adapters for this page. Adapters are used to adapt a page's configuration
24
 *              to another one.
25
 * @see     \Brendt\Stitcher\Adapter\Adapter
26
 *
27
 * @package Brendt\Stitcher\Site
28
 */
29
class Page
30
{
31
    /**
32
     * @var Meta
33
     */
34
    public $meta;
35
36
    /**
37
     * The page's ID.
38
     *
39
     * @var string
40
     */
41
    protected $id;
42
43
    /**
44
     * The template path of this page.
45
     *
46
     * @var string
47
     */
48
    protected $templatePath;
49
50
    /**
51
     * The variables of this page, which will be available in the rendered template.
52
     *
53
     * @var array
54
     */
55
    protected $variables = [];
56
57
    /**
58
     * The adapters of this page.
59
     * Adapters will transform a page's variables and/or the page itself into one or more pages.
60
     *
61
     * @var array
62
     */
63
    protected $adapters;
64
65
    /**
66
     * An array containing a list of parsed variables.
67
     *
68
     * @see setVariableIsParsed
69
     *
70
     * @var array
71
     */
72
    protected $parsedVariables = [];
73
74
    /**
75
     * @var Header[]
76
     */
77
    private $headers = [];
78
79
    /**
80
     * Construct a new page
81
     *
82
     * @param string    $id
83
     * @param array     $data
84
     *
85
     * @param Meta|null $meta
86
     *
87
     * @throws TemplateNotFoundException
88
     */
89
    public function __construct($id, array $data = [], Meta $meta = null) {
90
        if (!isset($data['template'])) {
91
            throw new TemplateNotFoundException("No template was set for page {$id}");
92
        }
93
94
        $this->id = $id;
95
        $this->templatePath = $data['template'];
96
        $this->meta = $meta ?? new Meta();
97
98
        if (isset($data['variables'])) {
99
            $this->variables += $data['variables'];
100
        }
101
102
        if (isset($data['adapters'])) {
103
            foreach ($data['adapters'] as $type => $adapterConfig) {
104
                $this->adapters[$type] = $adapterConfig;
105
            }
106
        }
107
    }
108
109
    /**
110
     * Defines a variable as parsed.
111
     * Parsed variables will be ignored by Stitcher when compiling the website.
112
     * Adapters can define parsed variables to indicate Stitcher it should skip parsing that variable during compile
113
     * time.
114
     *
115
     * @param $name
116
     *
117
     * @return Page
118
     *
119
     * @see \Brendt\Stitcher\Stitcher::parseVariables
120
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
121
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
122
     */
123
    public function setVariableIsParsed($name) {
124
        $this->parsedVariables[$name] = true;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Check whether a variable is parsed or not.
131
     * Parsed variables will be ignored by Stitcher during compile time.
132
     *
133
     * @param $name
134
     *
135
     * @return bool
136
     *
137
     * @see \Brendt\Stitcher\Stitcher::parseVariables
138
     */
139
    public function isParsedVariable($name) {
140
        return isset($this->parsedVariables[$name]);
141
    }
142
143
    /**
144
     * Get the ID of this page
145
     *
146
     * @return string
147
     *
148
     * @see \Brendt\Stitcher\Stitcher::stitch
149
     */
150
    public function getId() {
151
        return $this->id;
152
    }
153
154
    /**
155
     * Get the template path of this page.
156
     *
157
     * @return string
158
     *
159
     * @see \Brendt\Stitcher\Stitcher::stitch
160
     */
161
    public function getTemplatePath() {
162
        return $this->templatePath;
163
    }
164
165
    /**
166
     * Get the variables of this page.
167
     *
168
     * @return array
169
     *
170
     * @see \Brendt\Stitcher\Stitcher::stitch
171
     * @see \Brendt\Stitcher\Stitcher::parseVariables
172
     */
173
    public function getVariables() {
174
        return $this->variables;
175
    }
176
177
    /**
178
     * Get the adapters of this page.
179
     *
180
     * @return array
181
     *
182
     * @see \Brendt\Stitcher\Stitcher::parseAdapters
183
     */
184
    public function getAdapters() {
185
        return $this->adapters;
186
    }
187
188
    /**
189
     * Get an adapter configuration by name.
190
     *
191
     * @param $name
192
     *
193
     * @return array
194
     *
195
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
196
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
197
     * @see \Brendt\Stitcher\controller\DevController::run
198
     */
199
    public function getAdapterConfig($name) {
200
        if (!isset($this->adapters[$name])) {
201
            return [];
202
        }
203
204
        return $this->adapters[$name];
205
    }
206
207
    /**
208
     * Get a variable by name.
209
     *
210
     * @param $name
211
     *
212
     * @return mixed|null
213
     *
214
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
215
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
216
     */
217
    public function getVariable($name) {
218
        if (!isset($this->variables[$name])) {
219
            return null;
220
        }
221
222
        return $this->variables[$name];
223
    }
224
225
    /**
226
     * Set the value of a variable.
227
     *
228
     * @param $name
229
     * @param $value
230
     *
231
     * @return Page
232
     *
233
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
234
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
235
     * @see \Brendt\Stitcher\Stitcher::parseVariables
236
     */
237
    public function setVariableValue($name, $value) {
238
        $this->variables[$name] = $value;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Remove an adapter.
245
     *
246
     * @param $name
247
     *
248
     * @return Page
249
     *
250
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
251
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
252
     */
253
    public function removeAdapter($name) {
254
        if (isset($this->adapters[$name])) {
255
            unset($this->adapters[$name]);
256
        }
257
258
        return $this;
259
    }
260
261
    /**
262
     * Set the ID of this page.
263
     * An page's ID can be re-set after constructing when an adapter is creating other pages based on an existing page.
264
     *
265
     * @param string $id
266
     *
267
     * @return Page
268
     *
269
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
270
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
271
     */
272
    public function setId($id) {
273
        $this->id = $id;
274
275
        return $this;
276
    }
277
278
    /**
279
     * @param Header $header
280
     */
281
    public function addHeader(Header $header) {
282
        $this->headers[] = $header;
283
    }
284
285
    /**
286
     * @return Header[]
287
     */
288
    public function getHeaders() : array {
289
        return $this->headers;
290
    }
291
292
    /**
293
     * @param array $data
294
     *
295
     * @todo Create separate meta crawlers
296
     */
297
    public function parseMeta(array $data) {
298
        if (isset($data['meta'])) {
299
            foreach ($data['meta'] as $name => $value) {
300
                $value = $data[$value] ?? $value;
301
302
                $this->meta->name($name, $value);
303
            }
304
        }
305
306
        if (isset($data['title'])) {
307
            $this->meta->title($data['title']);
308
        }
309
310
        if (isset($data['image']['src'])) {
311
            $this->meta->image($data['image']['src']);
312
        }
313
314
        if (isset($data['description'])) {
315
            $this->meta->description($data['description']);
316
        }
317
318 View Code Duplication
        if (isset($data['pagination']['next'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
            $this->meta->link('next', $data['pagination']['next']['url']);
320
        }
321
322 View Code Duplication
        if (isset($data['pagination']['prev'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
323
            $this->meta->link('prev', $data['pagination']['prev']['url']);
324
        }
325
    }
326
327
}
328