Passed
Push — develop ( b01563...5b2a54 )
by Brent
02:41
created

MetaCompiler::compileTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
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', [$this, 'compileTitle']);
20
        $this->addCompiler('description', [$this, 'compileDescription']);
21
        $this->addCompiler('image', [$this, 'compileImage']);
22
        $this->addCompiler('pagination', [$this, 'compilePagination']);
23
        $this->addCompiler('meta', [$this, 'compileMeta']);
24
        $this->addCompiler('_name', [$this, 'compileNamedMeta']);
25
    }
26
27
    /**
28
     * @param string   $name
29
     * @param callable $callback
30
     *
31
     * @return MetaCompiler
32
     */
33
    public function addCompiler(string $name, callable $callback) : MetaCompiler {
34
        $this->compilers[$name] = $callback;
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param Page $page
41
     */
42
    public function compilePage(Page $page) : void {
43
        $variables = $page->getVariables();
44
45
        foreach ($variables as $name => $data) {
46
            if (!$page->isParsedVariable($name)) {
47
                continue;
48
            }
49
50
            $this->compilePageVariable($page, $name, $data);
51
        }
52
    }
53
54
    /**
55
     * @param Page   $page
56
     * @param string $name
57
     * @param mixed  $data
58
     * @param bool   $force
59
     */
60
    public function compilePageVariable(Page $page, string $name, $data, bool $force = false) : void {
61
        $isCustomCompiler = isset($this->compilers[$name]);
62
63
        if (!$isCustomCompiler && !$force) {
64
            return;
65
        } else if (!$isCustomCompiler) {
66
            $compileCallable = $this->compilers['_name'];
67
        } else {
68
            $compileCallable = $this->compilers[$name];
69
        }
70
71
        $compileCallable($page, $data, $name);
72
    }
73
74
    /**
75
     * @param Page   $page
76
     * @param string $data
77
     */
78
    private function compileTitle(Page $page, string $data) {
79
        $page->meta->title($data);
80
    }
81
82
    /**
83
     * @param Page   $page
84
     * @param string $data
85
     */
86
    private function compileDescription(Page $page, string $data) {
87
        $page->meta->description($data);
88
    }
89
90
    /**
91
     * @param Page $page
92
     * @param      $data
93
     */
94
    private function compileImage(Page $page, $data) {
95
        if (is_array($data) && isset($data['src'])) {
96
            $page->meta->image($data['src']);
97
        } else if (is_string($data)) {
98
            $page->meta->image($data);
99
        }
100
    }
101
102
    /**
103
     * @param Page  $page
104
     * @param array $pagination
105
     */
106
    private function compilePagination(Page $page, array $pagination) {
107 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...
108
            $page->meta->link('next', $pagination['next']['url']);
109
        }
110
111 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...
112
            $page->meta->link('prev', $pagination['prev']['url']);
113
        }
114
    }
115
116
    /**
117
     * @param Page  $page
118
     * @param array $data
119
     */
120
    private function compileMeta(Page $page, array $data) {
121
        foreach ($data as $name => $item) {
122
            $this->compilePageVariable($page, $name, $item, true);
123
        }
124
    }
125
126
    /**
127
     * @param Page   $page
128
     * @param        $data
129
     * @param string $name
130
     */
131
    private function compileNamedMeta(Page $page, $data, string $name) {
132
        $page->meta->name($name, $data);
133
    }
134
135
}
136