FileSystemStorage   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 158
ccs 0
cts 78
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
B getPage() 0 20 6
A getList() 0 8 3
A isDir() 0 9 2
A getMetadata() 0 4 1
A setFileSystem() 0 4 1
A setPath() 0 4 1
A getPath() 0 4 1
A read() 0 4 1
A findPageClass() 0 8 2
A readArray() 0 4 1
A getLocalPath() 0 4 1
A getFileSystem() 0 11 2
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\storage;
12
13
use creocoder\flysystem\Filesystem;
14
use hiqdev\yii2\collection\BaseObject;
15
use hiqdev\yii2\modules\pages\interfaces\PageInterface;
16
use hiqdev\yii2\modules\pages\interfaces\StorageInterface;
17
use hiqdev\yii2\modules\pages\models\AbstractPage;
18
use hiqdev\yii2\modules\pages\models\PagesList;
19
use Yii;
20
21
class FileSystemStorage extends BaseObject implements StorageInterface
22
{
23
    /** @var string|Filesystem */
24
    private $fileSystem;
25
26
    /** @var string */
27
    private $path;
28
29
    private $pageClasses = [
30
        ''      => \hiqdev\yii2\modules\pages\models\OtherPage::class,
31
        'md'    => \hiqdev\yii2\modules\pages\models\MarkdownPage::class,
32
        'php'   => \hiqdev\yii2\modules\pages\models\PhpPage::class,
33
        'twig'  => \hiqdev\yii2\modules\pages\models\TwigPage::class,
34
        'feature' => \hiqdev\yii2\modules\pages\models\BehatPage::class,
35
    ];
36
37
    /**
38
     * @param string $page
39
     * @return AbstractPage|null
40
     * @throws \yii\base\InvalidConfigException
41
     */
42
    public function getPage(string $page): ?PageInterface
43
    {
44
        if ($this->isDir($page)) {
45
            foreach (['index', 'README'] as $name) {
46
                $index = $this->getPage($page . '/' . $name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $index is correct as $this->getPage($page . '/' . $name) (which targets hiqdev\yii2\modules\page...ystemStorage::getPage()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
                if ($index) {
48
                    return $index;
49
                }
50
            }
51
        }
52
53
        foreach (array_keys($this->pageClasses) as $extension) {
54
            $path = $page . '.' . $extension;
55
            if ($this->getFileSystem()->has($path)) {
56
                return AbstractPage::createFromFile($path, $this);
57
            }
58
        }
59
60
        return null;
61
    }
62
63
    public function getList(string $path = null): ?PagesList
64
    {
65
        if (!is_null($path) && $this->isDir($path)) {
66
            return PagesList::createFromDir($path, $this);
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * @param string $page
74
     * @return bool|null
75
     * @throws \yii\base\InvalidConfigException
76
     */
77
    public function isDir(string $page): ?bool
78
    {
79
        if (!$this->getFileSystem()->has($page)) {
80
            return null;
81
        }
82
        $meta = $this->getMetadata($page);
83
84
        return $meta['type'] === 'dir';
85
    }
86
87
    /**
88
     * @param $page
89
     * @return array|false
90
     * @throws \yii\base\InvalidConfigException
91
     */
92
    public function getMetadata($page)
93
    {
94
        return $this->getFileSystem()->getMetadata($page);
95
    }
96
97
    /**
98
     * @param mixed $fileSystem
99
     */
100
    public function setFileSystem($fileSystem): void
101
    {
102
        $this->fileSystem = $fileSystem;
103
    }
104
105
    /**
106
     * @param string $path
107
     */
108
    public function setPath(string $path): void
109
    {
110
        $this->path = $path;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getPath(): string
117
    {
118
        return $this->path;
119
    }
120
121
    /**
122
     * @param string $path
123
     * @return null|string
124
     * @throws \yii\base\InvalidConfigException
125
     */
126
    public function read(string $path): ?string
127
    {
128
        return $this->getFileSystem()->read($path);
129
    }
130
131
    /**
132
     * @param string $extension
133
     * @return string
134
     */
135
    public function findPageClass(string $extension): string
136
    {
137
        if (empty($this->pageClasses[$extension])) {
138
            $extension = '';
139
        }
140
141
        return $this->pageClasses[$extension];
142
    }
143
144
    /**
145
     * Reads given path as array of already rtrimmed lines.
146
     * @param string $path
147
     * @return false|string[]
148
     */
149
    public function readArray(string $path)
150
    {
151
        return preg_split("/((\r?\n)|(\r\n?))/", $this->fileSystem->read($path));
152
    }
153
154
    /**
155
     * @param $filePath
156
     * @return string
157
     */
158
    public function getLocalPath($filePath): string
159
    {
160
        return $this->path . '/' . $filePath;
161
    }
162
163
    /**
164
     * @return Filesystem
165
     * @throws \yii\base\InvalidConfigException
166
     */
167
    public function getFileSystem(): Filesystem
168
    {
169
        if (!is_object($this->fileSystem)) {
170
            $this->fileSystem = Yii::createObject([
171
                'class' => $this->fileSystem,
172
                'path' => $this->path,
173
            ]);
174
        }
175
176
        return $this->fileSystem;
177
    }
178
}
179