Completed
Pull Request — master (#12)
by ARCANEDEV
09:00
created

SeoMeta::misc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php namespace Arcanedev\SeoHelper;
2
3
use Arcanedev\Support\Traits\Configurable;
4
5
/**
6
 * Class     SeoMeta
7
 *
8
 * @package  Arcanedev\SeoHelper
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class SeoMeta implements Contracts\SeoMeta
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Traits
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    use Configurable;
18
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     * Current URL.
25
     *
26
     * @var string
27
     */
28
    protected $currentUrl = '';
29
30
    /**
31
     * The Title instance.
32
     *
33
     * @var \Arcanedev\SeoHelper\Contracts\Entities\TitleInterface
34
     */
35
    protected $title;
36
37
    /**
38
     * The Description instance.
39
     *
40
     * @var \Arcanedev\SeoHelper\Contracts\Entities\DescriptionInterface
41
     */
42
    protected $description;
43
44
    /**
45
     * The Keywords instance.
46
     *
47
     * @var \Arcanedev\SeoHelper\Contracts\Entities\KeywordsInterface
48
     */
49
    protected $keywords;
50
51
    /**
52
     * The MiscTags instance.
53
     *
54
     * @var \Arcanedev\SeoHelper\Contracts\Entities\MiscTagsInterface
55
     */
56
    protected $misc;
57
58
    /**
59
     * The Webmasters instance.
60
     *
61
     * @var \Arcanedev\SeoHelper\Contracts\Entities\WebmastersInterface
62
     */
63
    protected $webmasters;
64
65
    /**
66
     * The Analytics instance.
67
     *
68
     * @var \Arcanedev\SeoHelper\Contracts\Entities\AnalyticsInterface
69
     */
70
    protected $analytics;
71
72
    /* ------------------------------------------------------------------------------------------------
73
     |  Constructor
74
     | ------------------------------------------------------------------------------------------------
75
     */
76
    /**
77
     * Make SeoMeta instance.
78
     *
79
     * @param  array  $configs
80
     */
81 324
    public function __construct(array $configs)
82
    {
83 324
        $this->setConfigs($configs);
84 324
        $this->init();
85 324
    }
86
87
    /**
88
     * Start the engine.
89
     */
90 324
    private function init()
91
    {
92 324
        $this->title(
93 324
            new Entities\Title($this->getConfig('title', []))
94 243
        );
95 324
        $this->description(
96 324
            new Entities\Description($this->getConfig('description', []))
97 243
        );
98 324
        $this->keywords(
99 324
            new Entities\Keywords($this->getConfig('keywords', []))
100 243
        );
101 324
        $this->misc(
102 324
            new Entities\MiscTags($this->getConfig('misc', []))
103 243
        );
104 324
        $this->webmasters(
105 324
            new Entities\Webmasters($this->getConfig('webmasters', []))
106 243
        );
107 324
        $this->analytics(
108 324
            new Entities\Analytics($this->getConfig('analytics', []))
109 243
        );
110 324
    }
111
112
    /* ------------------------------------------------------------------------------------------------
113
     |  Getters & Setters
114
     | ------------------------------------------------------------------------------------------------
115
     */
116
    /**
117
     * Set the Title instance.
118
     *
119
     * @param  \Arcanedev\SeoHelper\Contracts\Entities\TitleInterface  $title
120
     *
121
     * @return self
122
     */
123 324
    public function title(Contracts\Entities\TitleInterface $title)
124
    {
125 324
        $this->title = $title;
126
127 324
        return $this;
128
    }
129
130
    /**
131
     * Set the Description instance.
132
     *
133
     * @param  \Arcanedev\SeoHelper\Contracts\Entities\DescriptionInterface  $description
134
     *
135
     * @return self
136
     */
137 324
    public function description(Contracts\Entities\DescriptionInterface $description) {
138 324
        $this->description = $description;
139
140 324
        return $this;
141
    }
142
143
    /**
144
     * Set the Keywords instance.
145
     *
146
     * @param  \Arcanedev\SeoHelper\Contracts\Entities\KeywordsInterface  $keywords
147
     *
148
     * @return self
149
     */
150 324
    public function keywords(Contracts\Entities\KeywordsInterface $keywords)
151
    {
152 324
        $this->keywords = $keywords;
153
154 324
        return $this;
155
    }
156
157
    /**
158
     * Set the MiscTags instance.
159
     *
160
     * @param  \Arcanedev\SeoHelper\Contracts\Entities\MiscTagsInterface  $misc
161
     *
162
     * @return self
163
     */
164 324
    public function misc(Contracts\Entities\MiscTagsInterface $misc)
165
    {
166 324
        $this->misc = $misc;
167
168 324
        return $this;
169
    }
170
171
    /**
172
     * Set the Webmasters instance.
173
     *
174
     * @param  \Arcanedev\SeoHelper\Contracts\Entities\WebmastersInterface  $webmasters
175
     *
176
     * @return self
177
     */
178 324
    public function webmasters(Contracts\Entities\WebmastersInterface $webmasters)
179
    {
180 324
        $this->webmasters = $webmasters;
181
182 324
        return $this;
183
    }
184
185
    /**
186
     * Set the Analytics instance.
187
     *
188
     * @param  Contracts\Entities\AnalyticsInterface  $analytics
189
     *
190
     * @return self
191
     */
192 324
    private function analytics(Contracts\Entities\AnalyticsInterface $analytics)
193
    {
194 324
        $this->analytics = $analytics;
195
196 324
        return $this;
197
    }
198
199
    /**
200
     * Set the title.
201
     *
202
     * @param  string  $title
203
     * @param  string  $siteName
204
     * @param  string  $separator
205
     *
206
     * @return self
207
     */
208 36
    public function setTitle($title, $siteName = null, $separator = null)
209
    {
210 36
        $this->title->set($title);
211
212 36
        if ( ! is_null($siteName)) {
213 36
            $this->title->setSiteName($siteName);
214 27
        }
215
216 36
        if ( ! is_null($separator)) {
217 36
            $this->title->setSeparator($separator);
218 27
        }
219
220 36
        return $this;
221
    }
222
223
    /**
224
     * Set the description content.
225
     *
226
     * @param  string  $content
227
     *
228
     * @return self
229
     */
230 36
    public function setDescription($content)
231
    {
232 36
        $this->description->set($content);
233
234 36
        return $this;
235
    }
236
237
    /**
238
     * Set the keywords content.
239
     *
240
     * @param  array|string  $content
241
     *
242
     * @return self
243
     */
244 60
    public function setKeywords($content)
245
    {
246 60
        $this->keywords->set($content);
247
248 60
        return $this;
249
    }
250
251
    /**
252
     * Add a keyword.
253
     *
254
     * @param  string  $keyword
255
     *
256
     * @return self
257
     */
258 12
    public function addKeyword($keyword)
259
    {
260 12
        $this->keywords->add($keyword);
261
262 12
        return $this;
263
    }
264
265
    /**
266
     * Add many keywords.
267
     *
268
     * @param  array  $keywords
269
     *
270
     * @return self
271
     */
272 12
    public function addKeywords(array $keywords)
273
    {
274 12
        $this->keywords->addMany($keywords);
275
276 12
        return $this;
277
    }
278
279
    /**
280
     * Add a webmaster tool site verifier.
281
     *
282
     * @param  string  $webmaster
283
     * @param  string  $content
284
     *
285
     * @return self
286
     */
287 12
    public function addWebmaster($webmaster, $content)
288
    {
289 12
        $this->webmasters->add($webmaster, $content);
290
291 12
        return $this;
292
    }
293
294
    /**
295
     * Set the current URL.
296
     *
297
     * @param  string  $url
298
     *
299
     * @return self
300
     */
301 120
    public function setUrl($url)
302
    {
303 120
        $this->currentUrl = $url;
304 120
        $this->misc->setUrl($url);
305
306 120
        return $this;
307
    }
308
309
    /**
310
     * Set the Google Analytics code.
311
     *
312
     * @param  string  $code
313
     *
314
     * @return self
315
     */
316 12
    public function setGoogleAnalytics($code)
317
    {
318 12
        $this->analytics->setGoogle($code);
319
320 12
        return $this;
321
    }
322
323
    /* ------------------------------------------------------------------------------------------------
324
     |  Main Functions
325
     | ------------------------------------------------------------------------------------------------
326
     */
327
    /**
328
     * Add a meta tag.
329
     *
330
     * @param  string  $name
331
     * @param  string  $content
332
     *
333
     * @return self
334
     */
335 12
    public function addMeta($name, $content)
336
    {
337 12
        $this->misc->add($name, $content);
338
339 12
        return $this;
340
    }
341
342
    /**
343
     * Add many meta tags.
344
     *
345
     * @param  array  $metas
346
     *
347
     * @return self
348
     */
349 12
    public function addMetas(array $metas)
350
    {
351 12
        $this->misc->addMany($metas);
352
353 12
        return $this;
354
    }
355
356
    /**
357
     * Remove a meta from the meta collection by key.
358
     *
359
     * @param  string|array  $names
360
     *
361
     * @return self
362
     */
363 12
    public function removeMeta($names)
364
    {
365 12
        $this->misc->remove($names);
366
367 12
        return $this;
368
    }
369
370
    /**
371
     * Reset the meta collection except the description and keywords metas.
372
     *
373
     * @return self
374
     */
375 12
    public function resetMetas()
376
    {
377 12
        $this->misc->reset();
378
379 12
        return $this;
380
    }
381
382
    /**
383
     * Reset all webmaster tool site verifier metas.
384
     *
385
     * @return self
386
     */
387 12
    public function resetWebmasters()
388
    {
389 12
        $this->webmasters->reset();
390
391 12
        return $this;
392
    }
393
394
    /**
395
     * Render all seo tags.
396
     *
397
     * @return string
398
     */
399 228
    public function render()
400
    {
401 228
        return implode(PHP_EOL, array_filter([
402 228
            $this->title->render(),
403 228
            $this->description->render(),
404 228
            $this->keywords->render(),
405 228
            $this->misc->render(),
406 228
            $this->webmasters->render(),
407 228
            $this->analytics->render(),
408 171
        ]));
409
    }
410
411
    /**
412
     * Render all seo tags.
413
     *
414
     * @return string
415
     */
416 96
    public function __toString()
417
    {
418 96
        return $this->render();
419
    }
420
}
421