Completed
Pull Request — master (#1)
by
unknown
04:06
created

FileSystemStorage::isDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\storage;
12
13
use creocoder\flysystem\Filesystem;
14
use hiqdev\yii2\collection\BaseObject;
15
use hiqdev\yii2\modules\pages\models\AbstractPage;
16
use Yii;
17
18
class FileSystemStorage extends BaseObject implements StorageInterface
19
{
20
    /** @var string|Filesystem */
21
    private $fileSystem;
22
23
    /** @var string */
24
    private $path;
25
26
    private $pageClasses = [
27
        ''      => \hiqdev\yii2\modules\pages\models\OtherPage::class,
28
        'md'    => \hiqdev\yii2\modules\pages\models\MarkdownPage::class,
29
        'php'   => \hiqdev\yii2\modules\pages\models\PhpPage::class,
30
        'twig'  => \hiqdev\yii2\modules\pages\models\TwigPage::class,
31
    ];
32
33
    /**
34
     * @param string $page
35
     * @return AbstractPage|null
36
     * @throws \yii\base\InvalidConfigException
37
     */
38
    public function getPage(string $page): ?AbstractPage
39
    {
40
        if ($this->isDir($page)) {
41
            foreach (['index', 'README'] as $name) {
42
                $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...
43
                if ($index) {
44
                    return $index;
45
                }
46
            }
47
        }
48
49
        foreach (array_keys($this->pageClasses) as $extension) {
50
            $path = $page . '.' . $extension;
51
            if ($this->getFileSystem()->has($path)) {
52
                return AbstractPage::createFromFile($path, $this);
53
            }
54
        }
55
56
        return null;
57
    }
58
59
    /**
60
     * @param string $page
61
     * @return bool|null
62
     * @throws \yii\base\InvalidConfigException
63
     */
64
    public function isDir(string $page): ?bool
65
    {
66
        if (!$this->getFileSystem()->has($page)) {
67
            return null;
68
        }
69
        $meta = $this->getMetadata($page);
70
71
        return $meta['type'] === 'dir';
72
    }
73
74
    /**
75
     * @param $page
76
     * @return array|false
77
     * @throws \yii\base\InvalidConfigException
78
     */
79
    public function getMetadata($page)
80
    {
81
        return $this->getFileSystem()->getMetadata($page);
82
    }
83
84
    /**
85
     * @param mixed $fileSystem
86
     */
87
    public function setFileSystem($fileSystem): void
88
    {
89
        $this->fileSystem = $fileSystem;
90
    }
91
92
    /**
93
     * @param string $path
94
     */
95
    public function setPath(string $path): void
96
    {
97
        $this->path = $path;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getPath(): string
104
    {
105
        return $this->path;
106
    }
107
108
    /**
109
     * @param string $path
110
     * @return null|string
111
     * @throws \yii\base\InvalidConfigException
112
     */
113
    public function read(string $path): ?string
114
    {
115
        return $this->getFileSystem()->read($path);
116
    }
117
118
    /**
119
     * @param string $extension
120
     * @return string
121
     */
122
    public function findPageClass(string $extension): string
123
    {
124
        if (empty($this->pageClasses[$extension])) {
125
            $extension = '';
126
        }
127
128
        return $this->pageClasses[$extension];
129
    }
130
131
    /**
132
     * Reads given path as array of already rtrimmed lines.
133
     * @param string $path
134
     * @return false|string[]
135
     */
136
    public function readArray(string $path)
137
    {
138
        return preg_split("/((\r?\n)|(\r\n?))/", $this->fileSystem->read($path));
139
    }
140
141
    /**
142
     * @param $filePath
143
     * @return string
144
     */
145
    public function getLocalPath($filePath): string
146
    {
147
        return $this->path . '/' . $filePath;
148
    }
149
150
    /**
151
     * @return Filesystem
152
     * @throws \yii\base\InvalidConfigException
153
     */
154
    private function getFileSystem(): Filesystem
155
    {
156
        if (!is_object($this->fileSystem)) {
157
            $this->fileSystem = Yii::createObject([
158
                'class' => $this->fileSystem,
159
                'path' => $this->path,
160
            ]);
161
        }
162
163
        return $this->fileSystem;
164
    }
165
}
166