Completed
Push — master ( a2f237...c267aa )
by Andrii
04:14
created

AbstractPage::getUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Yii2 Pages Module
4
 *
5
 * @link      https://github.com/hiqdev/yii2-module-pages
6
 * @package   yii2-module-pages
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\modules\pages\models;
12
13
use Symfony\Component\Yaml\Yaml;
14
use Yii;
15
16
abstract class AbstractPage extends \yii\base\Object
17
{
18
    public $layout;
19
20
    public $title;
21
22
    protected $path;
23
24
    protected $text;
25
26
    protected $data = [];
27
28
    protected $url;
29
30
    public function setData($data)
31
    {
32
        if (!is_array($data)) {
33
            return;
34
        }
35
        $this->data = $data;
36
        foreach (['title', 'layout', 'url'] as $key) {
37
            if (isset($data[$key])) {
38
                $this->{$key} = $data[$key];
39
            }
40
        }
41
    }
42
43
    public function getData()
44
    {
45
        return $this->data;
46
    }
47
48
    public function __construct($path)
49
    {
50
        list($data, $text) = $this->extractData($path);
51
52
        $this->path = $path;
53
        $this->text = $text;
54
        $this->setData($data);
55
    }
56
57
    public function getPath()
58
    {
59
        return $this->path;
60
    }
61
62
    public function getDate()
63
    {
64
        return $this->data['date'];
65
    }
66
67
    public function getUrl()
68
    {
69
        return $this->url ?: ['/pages/render/index', 'page' => $this->getPath()];
70
    }
71
72
    public static function getModule()
73
    {
74
        /// XXX think
75
        return Yii::$app->getModule('pages');
76
    }
77
78
    public static function createFromFile($path)
79
    {
80
        $extension = pathinfo($path)['extension'];
81
        $class = static::getModule()->findPageClass($extension);
82
83
        return new $class($path);
84
    }
85
86 View Code Duplication
    public function extractData($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
87
    {
88
        $lines = static::getModule()->readArray($path);
89
        $yaml = $this->readQuotedLines($lines, '/^---$/', '/^---$/');
90
        if (empty($yaml)) {
91
            $data = [];
92
            $text = $lines;
93
        } else {
94
            $data = $this->readYaml($yaml);
95
            $text = array_slice($lines, count($yaml));
96
        }
97
98
        return [$data, implode("\n", $text)];
99
    }
100
101
    public function readFrontMatter($lines)
102
    {
103
        $yaml = $this->readQuotedLines($lines, '/^---$/', '/^---$/');
104
        if (empty($yaml)) {
105
            return [];
106
        }
107
108
        return empty($yaml) ? [] : $this->readYaml($yaml);
109
    }
110
111
    public function readYaml($lines)
112
    {
113
        $data = Yaml::parse(implode("\n", $lines));
114
        if (is_int($data['date'])) {
115
            $data['date'] = date('c', $data['date']);
116
        }
117
118
        return $data;
119
    }
120
121
    public function readQuotedLines($lines, $headMarker, $tailMarker)
122
    {
123
        $line = array_shift($lines);
124
        if (!preg_match($headMarker, $line, $matches)) {
125
            return null;
126
        }
127
        $res[] = ltrim(substr($line, strlen($matches[0])));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
128
        while ($line) {
129
            $line = array_shift($lines);
130
            if (preg_match($tailMarker, $line, $matches)) {
131
                $res[] = rtrim(substr($line, 0, -strlen($matches[0])));
132
                break;
133
            }
134
            $res[] = $line;
135
        }
136
137
        return $res;
138
    }
139
140
    /**
141
     * Renders the page with given params.
142
     *
143
     * @param array $params
144
     * @abstract
145
     */
146
    abstract public function render(array $params = []);
147
}
148