Passed
Push — master ( 2e4f4d...b5d843 )
by
unknown
05:25
created

Page   F

Complexity

Total Complexity 92

Size/Duplication

Total Lines 678
Duplicated Lines 0 %

Test Coverage

Coverage 92.79%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 195
c 1
b 1
f 0
dl 0
loc 678
ccs 206
cts 222
cp 0.9279
rs 2
wmc 92

47 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A createIdFromFile() 0 21 5
A slugify() 0 7 2
A getIdWithoutLang() 0 8 3
B setFile() 0 53 7
A __toString() 0 3 1
A isVirtual() 0 3 1
A getType() 0 3 1
A getPath() 0 3 1
A setBodyHtml() 0 5 1
A getPagination() 0 3 1
A getBodyHtml() 0 3 1
A getFrontmatter() 0 3 1
A getFolder() 0 3 1
A setFolder() 0 5 1
A setVirtual() 0 5 1
A getPages() 0 3 1
A setId() 0 3 1
A getFmVariables() 0 3 1
A setVariables() 0 7 2
A getVariable() 0 7 2
A setTerms() 0 5 1
A parse() 0 8 1
A hasVariable() 0 3 1
A getRendered() 0 3 1
A setType() 0 5 1
A getBody() 0 3 1
A setFmVariables() 0 5 1
A addRendered() 0 5 1
A getFileName() 0 7 2
A unSection() 0 5 1
A getPaginator() 0 3 1
A setPaginator() 0 5 1
A setSection() 0 5 1
A getVariables() 0 3 1
A setPath() 0 35 6
A getSection() 0 3 2
A unVariable() 0 7 2
A setPages() 0 5 1
C setVariable() 0 50 17
A getFilePath() 0 7 3
A getSlug() 0 3 1
A setSlug() 0 12 4
A getContent() 0 3 1
A getTerms() 0 3 1
A filterBool() 0 5 2
A getPathname() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Page, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Collection\Page;
15
16
use Cecil\Collection\Item;
17
use Cecil\Exception\RuntimeException;
18
use Cecil\Util;
19
use Cocur\Slugify\Slugify;
20
use Symfony\Component\Finder\SplFileInfo;
21
22
/**
23
 * Class Page.
24
 */
25
class Page extends Item
26
{
27
    public const SLUGIFY_PATTERN = '/(^\/|[^._a-z0-9\/]|-)+/'; // should be '/^\/|[^_a-z0-9\/]+/'
28
29
    /** @var bool True if page is not created from a file. */
30
    protected $virtual;
31
32
    /** @var SplFileInfo */
33
    protected $file;
34
35
    /** @var Type Type */
36
    protected $type;
37
38
    /** @var string */
39
    protected $folder;
40
41
    /** @var string */
42
    protected $slug;
43
44
    /** @var string path = folder + slug. */
45
    protected $path;
46
47
    /** @var string */
48
    protected $section;
49
50
    /** @var string */
51
    protected $frontmatter;
52
53
    /** @var array Front matter before conversion. */
54
    protected $fmVariables = [];
55
56
    /** @var string Body before conversion. */
57
    protected $body;
58
59
    /** @var string Body after conversion. */
60
    protected $html;
61
62
    /** @var array Output, by format */
63
    protected $rendered = [];
64
65
    /** @var Collection Subpages of a list page. */
66
    protected $subPages;
67
68
    /** @var array */
69
    protected $paginator = [];
70
71
    /** @var \Cecil\Collection\Taxonomy\Vocabulary Terms of a vocabulary. */
72
    protected $terms;
73
74
    /** @var Slugify */
75
    private static $slugifier;
76
77 1
    public function __construct(string $id)
78
    {
79 1
        parent::__construct($id);
80 1
        $this->setVirtual(true);
81 1
        $this->setType(Type::PAGE->value);
82
        // default variables
83 1
        $this->setVariables([
84 1
            'title'            => 'Page Title',
85 1
            'date'             => new \DateTime(),
86 1
            'updated'          => new \DateTime(),
87 1
            'weight'           => null,
88 1
            'filepath'         => null,
89 1
            'published'        => true,
90 1
            'content_template' => 'page.content.twig',
91 1
        ]);
92
    }
93
94
    /**
95
     * toString magic method to prevent Twig get_attribute fatal error.
96
     *
97
     * @return string
98
     */
99 1
    public function __toString()
100
    {
101 1
        return $this->getId();
102
    }
103
104
    /**
105
     * Turns a path (string) into a slug (URI).
106
     */
107 1
    public static function slugify(string $path): string
108
    {
109 1
        if (!self::$slugifier instanceof Slugify) {
110 1
            self::$slugifier = Slugify::create(['regexp' => self::SLUGIFY_PATTERN]);
111
        }
112
113 1
        return self::$slugifier->slugify($path);
114
    }
115
116
    /**
117
     * Creates the ID from the file (path).
118
     */
119 1
    public static function createIdFromFile(SplFileInfo $file): string
120
    {
121 1
        $relativePath = self::slugify(str_replace(DIRECTORY_SEPARATOR, '/', $file->getRelativePath()));
122 1
        $basename = self::slugify(PrefixSuffix::subPrefix($file->getBasename('.' . $file->getExtension())));
123
        // if file is "README.md", ID is "index"
124 1
        $basename = (string) str_ireplace('readme', 'index', $basename);
125
        // if file is section's index: "section/index.md", ID is "section"
126 1
        if (!empty($relativePath) && PrefixSuffix::sub($basename) == 'index') {
127
            // case of a localized section's index: "section/index.fr.md", ID is "fr/section"
128 1
            if (PrefixSuffix::hasSuffix($basename)) {
129 1
                return PrefixSuffix::getSuffix($basename) . '/' . $relativePath;
130
            }
131
132 1
            return $relativePath;
133
        }
134
        // localized page
135 1
        if (PrefixSuffix::hasSuffix($basename)) {
136 1
            return trim(Util::joinPath(PrefixSuffix::getSuffix($basename), $relativePath, PrefixSuffix::sub($basename)), '/');
137
        }
138
139 1
        return trim(Util::joinPath($relativePath, $basename), '/');
140
    }
141
142
    /**
143
     * Returns the ID of a page without language.
144
     */
145 1
    public function getIdWithoutLang(): string
146
    {
147 1
        $langPrefix = $this->getVariable('language') . '/';
148 1
        if ($this->hasVariable('language') && Util\Str::startsWith($this->getId(), $langPrefix)) {
149 1
            return substr($this->getId(), \strlen($langPrefix));
150
        }
151
152 1
        return $this->getId();
153
    }
154
155
    /**
156
     * Set file.
157
     */
158 1
    public function setFile(SplFileInfo $file): self
159
    {
160 1
        $this->file = $file;
161 1
        $this->setVirtual(false);
162
163
        /*
164
         * File path components
165
         */
166 1
        $fileRelativePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath());
167 1
        $fileExtension = $this->file->getExtension();
168 1
        $fileName = $this->file->getBasename('.' . $fileExtension);
169
        // renames "README" to "index"
170 1
        $fileName = (string) str_ireplace('readme', 'index', $fileName);
171
        // case of "index" = home page
172 1
        if (empty($this->file->getRelativePath()) && PrefixSuffix::sub($fileName) == 'index') {
173 1
            $this->setType(Type::HOMEPAGE->value);
174
        }
175
        /*
176
         * Set page properties and variables
177
         */
178 1
        $this->setFolder($fileRelativePath);
179 1
        $this->setSlug($fileName);
180 1
        $this->setPath($this->getFolder() . '/' . $this->getSlug());
181 1
        $this->setVariables([
182 1
            'title'    => PrefixSuffix::sub($fileName),
183 1
            'date'     => (new \DateTime())->setTimestamp($this->file->getMTime()),
184 1
            'updated'  => (new \DateTime())->setTimestamp($this->file->getMTime()),
185 1
            'filepath' => $this->file->getRelativePathname(),
186 1
        ]);
187
        /*
188
         * Set specific variables
189
         */
190
        // is file has a prefix?
191 1
        if (PrefixSuffix::hasPrefix($fileName)) {
192 1
            $prefix = PrefixSuffix::getPrefix($fileName);
193 1
            if ($prefix !== null) {
194
                // prefix is a valid date?
195 1
                if (Util\Date::isValid($prefix)) {
196 1
                    $this->setVariable('date', (string) $prefix);
197
                } else {
198
                    // prefix is an integer: used for sorting
199 1
                    $this->setVariable('weight', (int) $prefix);
200
                }
201
            }
202
        }
203
        // is file has a language suffix?
204 1
        if (PrefixSuffix::hasSuffix($fileName)) {
205 1
            $this->setVariable('language', PrefixSuffix::getSuffix($fileName));
206
        }
207
        // set reference between page's translations, even if it exist in only one language
208 1
        $this->setVariable('langref', $this->getPath());
209
210 1
        return $this;
211
    }
212
213
    /**
214
     * Returns file name, with extension.
215
     */
216
    public function getFileName(): ?string
217
    {
218
        if ($this->file === null) {
219
            return null;
220
        }
221
222
        return $this->file->getBasename();
223
    }
224
225
    /**
226
     * Returns file real path.
227
     */
228 1
    public function getFilePath(): ?string
229
    {
230 1
        if ($this->file === null) {
231
            return null;
232
        }
233
234 1
        return $this->file->getRealPath() === false ? null : $this->file->getRealPath();
235
    }
236
237
    /**
238
     * Parse file content.
239
     */
240 1
    public function parse(): self
241
    {
242 1
        $parser = new Parser($this->file);
243 1
        $parsed = $parser->parse();
244 1
        $this->frontmatter = $parsed->getFrontmatter();
245 1
        $this->body = $parsed->getBody();
246
247 1
        return $this;
248
    }
249
250
    /**
251
     * Get front matter.
252
     */
253 1
    public function getFrontmatter(): ?string
254
    {
255 1
        return $this->frontmatter;
256
    }
257
258
    /**
259
     * Get body as raw.
260
     */
261 1
    public function getBody(): ?string
262
    {
263 1
        return $this->body;
264
    }
265
266
    /**
267
     * Set virtual status.
268
     */
269 1
    public function setVirtual(bool $virtual): self
270
    {
271 1
        $this->virtual = $virtual;
272
273 1
        return $this;
274
    }
275
276
    /**
277
     * Is current page is virtual?
278
     */
279 1
    public function isVirtual(): bool
280
    {
281 1
        return $this->virtual;
282
    }
283
284
    /**
285
     * Set page type.
286
     */
287 1
    public function setType(string $type): self
288
    {
289 1
        $this->type = Type::from($type);
290
291 1
        return $this;
292
    }
293
294
    /**
295
     * Get page type.
296
     */
297 1
    public function getType(): string
298
    {
299 1
        return $this->type->value;
300
    }
301
302
    /**
303
     * Set path without slug.
304
     */
305 1
    public function setFolder(string $folder): self
306
    {
307 1
        $this->folder = self::slugify($folder);
308
309 1
        return $this;
310
    }
311
312
    /**
313
     * Get path without slug.
314
     */
315 1
    public function getFolder(): ?string
316
    {
317 1
        return $this->folder;
318
    }
319
320
    /**
321
     * Set slug.
322
     */
323 1
    public function setSlug(string $slug): self
324
    {
325 1
        if (!$this->slug) {
326 1
            $slug = self::slugify(PrefixSuffix::sub($slug));
327
        }
328
        // force slug and update path
329 1
        if ($this->slug && $this->slug != $slug) {
330 1
            $this->setPath($this->getFolder() . '/' . $slug);
331
        }
332 1
        $this->slug = $slug;
333
334 1
        return $this;
335
    }
336
337
    /**
338
     * Get slug.
339
     */
340 1
    public function getSlug(): string
341
    {
342 1
        return $this->slug;
343
    }
344
345
    /**
346
     * Set path.
347
     */
348 1
    public function setPath(string $path): self
349
    {
350 1
        $path = trim($path, '/');
351
352
        // case of homepage
353 1
        if ($path == 'index') {
354 1
            $this->path = '';
355
356 1
            return $this;
357
        }
358
359
        // case of custom sections' index (ie: section/index.md -> section)
360 1
        if (substr($path, -6) == '/index') {
361 1
            $path = substr($path, 0, \strlen($path) - 6);
362
        }
363 1
        $this->path = $path;
364
365 1
        $lastslash = strrpos($this->path, '/');
366
367
        // case of root/top-level pages
368 1
        if ($lastslash === false) {
369 1
            $this->slug = $this->path;
370
371 1
            return $this;
372
        }
373
374
        // case of sections' pages: set section
375 1
        if (!$this->virtual && $this->getSection() === null) {
376 1
            $this->section = explode('/', $this->path)[0];
377
        }
378
        // set/update folder and slug
379 1
        $this->folder = substr($this->path, 0, $lastslash);
380 1
        $this->slug = substr($this->path, -(\strlen($this->path) - $lastslash - 1));
381
382 1
        return $this;
383
    }
384
385
    /**
386
     * Get path.
387
     */
388 1
    public function getPath(): ?string
389
    {
390 1
        return $this->path;
391
    }
392
393
    /**
394
     * @see getPath()
395
     */
396
    public function getPathname(): ?string
397
    {
398
        return $this->getPath();
399
    }
400
401
    /**
402
     * Set section.
403
     */
404 1
    public function setSection(string $section): self
405
    {
406 1
        $this->section = $section;
407
408 1
        return $this;
409
    }
410
411
    /**
412
     * Get section.
413
     */
414 1
    public function getSection(): ?string
415
    {
416 1
        return !empty($this->section) ? $this->section : null;
417
    }
418
419
    /**
420
     * Unset section.
421
     */
422
    public function unSection(): self
423
    {
424
        $this->section = null;
425
426
        return $this;
427
    }
428
429
    /**
430
     * Set body as HTML.
431
     */
432 1
    public function setBodyHtml(string $html): self
433
    {
434 1
        $this->html = $html;
435
436 1
        return $this;
437
    }
438
439
    /**
440
     * Get body as HTML.
441
     */
442 1
    public function getBodyHtml(): ?string
443
    {
444 1
        return $this->html;
445
    }
446
447
    /**
448
     * @see getBodyHtml()
449
     */
450 1
    public function getContent(): ?string
451
    {
452 1
        return $this->getBodyHtml();
453
    }
454
455
    /**
456
     * Add rendered.
457
     */
458 1
    public function addRendered(array $rendered): self
459
    {
460 1
        $this->rendered += $rendered;
461
462 1
        return $this;
463
    }
464
465
    /**
466
     * Get rendered.
467
     */
468 1
    public function getRendered(): array
469
    {
470 1
        return $this->rendered;
471
    }
472
473
    /**
474
     * Set Subpages.
475
     */
476 1
    public function setPages(\Cecil\Collection\Page\Collection $subPages): self
477
    {
478 1
        $this->subPages = $subPages;
479
480 1
        return $this;
481
    }
482
483
    /**
484
     * Get Subpages.
485
     */
486 1
    public function getPages(): ?\Cecil\Collection\Page\Collection
487
    {
488 1
        return $this->subPages;
489
    }
490
491
    /**
492
     * Set paginator.
493
     */
494 1
    public function setPaginator(array $paginator): self
495
    {
496 1
        $this->paginator = $paginator;
497
498 1
        return $this;
499
    }
500
501
    /**
502
     * Get paginator.
503
     */
504 1
    public function getPaginator(): array
505
    {
506 1
        return $this->paginator;
507
    }
508
509
    /**
510
     * Paginator backward compatibility.
511
     */
512
    public function getPagination(): array
513
    {
514
        return $this->getPaginator();
515
    }
516
517
    /**
518
     * Set vocabulary terms.
519
     */
520 1
    public function setTerms(\Cecil\Collection\Taxonomy\Vocabulary $terms): self
521
    {
522 1
        $this->terms = $terms;
523
524 1
        return $this;
525
    }
526
527
    /**
528
     * Get vocabulary terms.
529
     */
530 1
    public function getTerms(): \Cecil\Collection\Taxonomy\Vocabulary
531
    {
532 1
        return $this->terms;
533
    }
534
535
    /*
536
     * Helpers to set and get variables.
537
     */
538
539
    /**
540
     * Set an array as variables.
541
     *
542
     * @throws RuntimeException
543
     */
544 1
    public function setVariables(array $variables): self
545
    {
546 1
        foreach ($variables as $key => $value) {
547 1
            $this->setVariable($key, $value);
548
        }
549
550 1
        return $this;
551
    }
552
553
    /**
554
     * Get all variables.
555
     */
556 1
    public function getVariables(): array
557
    {
558 1
        return $this->properties;
559
    }
560
561
    /**
562
     * Set a variable.
563
     *
564
     * @param string $name  Name of the variable
565
     * @param mixed  $value Value of the variable
566
     *
567
     * @throws RuntimeException
568
     */
569 1
    public function setVariable(string $name, $value): self
570
    {
571 1
        $this->filterBool($value);
572
        switch ($name) {
573 1
            case 'date':
574 1
            case 'updated':
575 1
            case 'lastmod':
576
                try {
577 1
                    $date = Util\Date::toDatetime($value);
578
                } catch (\Exception) {
579
                    throw new \Exception(\sprintf('The value of "%s" is not a valid date: "%s".', $name, var_export($value, true)));
580
                }
581 1
                $this->offsetSet($name == 'lastmod' ? 'updated' : $name, $date);
582 1
                break;
583
584 1
            case 'schedule':
585
                /*
586
                 * publish: 2012-10-08
587
                 * expiry: 2012-10-09
588
                 */
589 1
                $this->offsetSet('published', false);
590 1
                if (\is_array($value)) {
591 1
                    if (\array_key_exists('publish', $value) && Util\Date::toDatetime($value['publish']) <= Util\Date::toDatetime('now')) {
592 1
                        $this->offsetSet('published', true);
593
                    }
594 1
                    if (\array_key_exists('expiry', $value) && Util\Date::toDatetime($value['expiry']) >= Util\Date::toDatetime('now')) {
595
                        $this->offsetSet('published', true);
596
                    }
597
                }
598 1
                break;
599 1
            case 'draft':
600
                // draft: true = published: false
601 1
                if ($value === true) {
602 1
                    $this->offsetSet('published', false);
603
                }
604 1
                break;
605 1
            case 'path':
606 1
            case 'slug':
607 1
                $slugify = self::slugify((string) $value);
608 1
                if ($value != $slugify) {
609
                    throw new RuntimeException(\sprintf('"%s" variable should be "%s" (not "%s") in "%s".', $name, $slugify, (string) $value, $this->getId()));
610
                }
611 1
                $method = 'set' . ucfirst($name);
612 1
                $this->$method($value);
613 1
                break;
614
            default:
615 1
                $this->offsetSet($name, $value);
616
        }
617
618 1
        return $this;
619
    }
620
621
    /**
622
     * Is variable exists?
623
     *
624
     * @param string $name Name of the variable
625
     */
626 1
    public function hasVariable(string $name): bool
627
    {
628 1
        return $this->offsetExists($name);
629
    }
630
631
    /**
632
     * Get a variable.
633
     *
634
     * @param string     $name    Name of the variable
635
     * @param mixed|null $default Default value
636
     *
637
     * @return mixed|null
638
     */
639 1
    public function getVariable(string $name, $default = null)
640
    {
641 1
        if ($this->offsetExists($name)) {
642 1
            return $this->offsetGet($name);
643
        }
644
645 1
        return $default;
646
    }
647
648
    /**
649
     * Unset a variable.
650
     *
651
     * @param string $name Name of the variable
652
     */
653 1
    public function unVariable(string $name): self
654
    {
655 1
        if ($this->offsetExists($name)) {
656 1
            $this->offsetUnset($name);
657
        }
658
659 1
        return $this;
660
    }
661
662
    /**
663
     * Set front matter (only) variables.
664
     */
665 1
    public function setFmVariables(array $variables): self
666
    {
667 1
        $this->fmVariables = $variables;
668
669 1
        return $this;
670
    }
671
672
    /**
673
     * Get front matter variables.
674
     */
675 1
    public function getFmVariables(): array
676
    {
677 1
        return $this->fmVariables;
678
    }
679
680
    /**
681
     * Cast "boolean" string (or array of strings) to boolean.
682
     *
683
     * @param mixed $value Value to filter
684
     *
685
     * @return bool|mixed
686
     *
687
     * @see strToBool()
688
     */
689 1
    private function filterBool(&$value)
690
    {
691 1
        \Cecil\Util\Str::strToBool($value);
692 1
        if (\is_array($value)) {
693 1
            array_walk_recursive($value, '\Cecil\Util\Str::strToBool');
694
        }
695
    }
696
697
    /**
698
     * {@inheritdoc}
699
     */
700 1
    public function setId(string $id): self
701
    {
702 1
        return parent::setId($id);
703
    }
704
}
705