Passed
Push — develop ( 7fa55c...bce364 )
by Brent
02:48
created

MetaCompiler::compilePageVariable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 4
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Site\Meta;
4
5
use Brendt\Stitcher\Site\Page;
6
7
class MetaCompiler
8
{
9
10
    /**
11
     * @var callable[]
12
     */
13
    private $compilers = [];
14
15
    /**
16
     * MetaCompiler constructor.
17
     */
18
    public function __construct() {
19
        $this->addCompiler('title', function (Page $page, string $data) {
20
            $page->meta->title($data);
21
        });
22
23
        $this->addCompiler('description', function (Page $page, string $data) {
24
            $page->meta->description($data);
25
        });
26
27
        $this->addCompiler('image', function (Page $page, $data) {
28
            if (is_array($data) && isset($data['src'])) {
29
                $page->meta->image($data['src']);
30
            } else if (is_string($data)) {
31
                $page->meta->image($data);
32
            }
33
        });
34
35
        $this->addCompiler('pagination', function (Page $page, array $pagination) {
36 View Code Duplication
            if (isset($pagination['next']['url'])) {
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...
37
                $page->meta->link('next', $pagination['next']['url']);
38
            }
39
40 View Code Duplication
            if (isset($pagination['prev']['url'])) {
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...
41
                $page->meta->link('prev', $pagination['prev']['url']);
42
            }
43
        });
44
45
        $this->addCompiler('meta', function (Page $page, array $data) {
46
            foreach ($data as $name => $item) {
47
                $this->compilePageVariable($page, $name, $item, true);
48
            }
49
        });
50
51
        $this->addCompiler('_name', function (Page $page, $data, string $name) {
52
            $page->meta->name($name, $data);
53
        });
54
    }
55
56
    /**
57
     * @param string   $name
58
     * @param callable $callback
59
     *
60
     * @return MetaCompiler
61
     */
62
    public function addCompiler(string $name, callable $callback) : MetaCompiler {
63
        $this->compilers[$name] = $callback;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param Page $page
70
     */
71
    public function compilePage(Page $page) : void {
72
        $variables = $page->getVariables();
73
74
        foreach ($variables as $name => $data) {
75
            if (!$page->isParsedVariable($name)) {
76
                continue;
77
            }
78
79
            $this->compilePageVariable($page, $name, $data);
80
        }
81
    }
82
83
    /**
84
     * @param Page   $page
85
     * @param string $name
86
     * @param mixed  $data
87
     * @param bool   $force
88
     */
89
    public function compilePageVariable(Page $page, string $name, $data, bool $force = false) : void {
90
        $isCustomCompiler = isset($this->compilers[$name]);
91
92
        if (!$isCustomCompiler && !$force) {
93
            return;
94
        } else if (!$isCustomCompiler) {
95
            $compileCallable = $this->compilers['_name'];
96
        } else {
97
            $compileCallable = $this->compilers[$name];
98
        }
99
100
        $compileCallable($page, $data, $name);
101
    }
102
103
}
104