Completed
Push — master ( 2038e8...a5b302 )
by Andrii
01:52
created

Module   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 70
ccs 0
cts 34
cp 0
rs 10
c 3
b 1
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewPath() 0 4 1
A localPath() 0 6 1
A readArray() 0 5 1
A setStorage() 0 4 1
A getStorage() 0 8 2
A find() 0 15 4
A getMetadata() 0 4 1
1
<?php
2
3
namespace hiqdev\yii2\modules\pages;
4
5
use Yii;
6
7
class Module extends \yii\base\Module
8
{
9
    protected $_storage;
10
11
    public $handlers = [
12
        'md'    => \hiqdev\yii2\modules\pages\models\MarkdownPage::class,
13
        'php'   => \hiqdev\yii2\modules\pages\models\PhpPage::class,
14
        'twig'  => \hiqdev\yii2\modules\pages\models\TwigPage::class,
15
    ];
16
17
    /**
18
     * This to use standard app pathes for views and layouts.
19
     * @return string
20
     */
21
    public function getViewPath()
22
    {
23
        return Yii::$app->getViewPath();
24
    }
25
26
    public function find($page)
27
    {
28
        if ($this->getStorage()->has($page)) {
29
            return $page;
30
        }
31
32
        foreach (array_keys($this->handlers) as $extension) {
33
            $path = $page . '.' . $extension;
34
            if ($this->getStorage()->has($path)) {
35
                return $path;
36
            }
37
        }
38
39
        return null;
40
    }
41
42
    public function getMetadata($page)
43
    {
44
        return $this->getStorage()->getMetadata($page);
45
    }
46
47
    public function localPath($path)
48
    {
49
        /// XXX: works for Local Filesystem only
50
        /// TODO: for others copying to be implemented
51
        return $this->getStorage()->path . '/' . $path;
52
    }
53
54
    /**
55
     * Reads given path as array of already rtrimmed lines.
56
     */
57
    public function readArray($path)
58
    {
59
        /// XXX: performance
60
        return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path));
61
    }
62
63
    public function setStorage($value)
64
    {
65
        $this->_storage = $value;
66
    }
67
68
    public function getStorage()
69
    {
70
        if (!is_object($this->_storage)) {
71
            $this->_storage = Yii::createObject($this->_storage);
72
        }
73
74
        return $this->_storage;
75
    }
76
}
77