Completed
Pull Request — master (#4)
by Klochok
14:56
created

PagesList::createFromDir()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 20
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 Yii;
14
use hiqdev\yii2\modules\pages\storage\FileSystemStorage;
15
use yii\data\ArrayDataProvider;
16
use yii\helpers\ArrayHelper;
17
18
class PagesList
19
{
20
    /** @var AbstractPage[]  */
21
    protected array $pages = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
22
23
    /**
24
     * PagesList constructor.
25
     * @param AbstractPage[] $pages
26
     */
27
    public function __construct(array $pages)
28
    {
29
        $this->pages = $pages;
30
    }
31
32
    /**
33
     * @param string $path
34
     * @param FileSystemStorage $storage
35
     * @return PagesList
36
     * @throws \yii\base\InvalidConfigException
37
     */
38
    public static function createFromDir(string $path, FileSystemStorage $storage): PagesList
39
    {
40
        $list = $storage->getFileSystem()->listContents($path);
41
        ArrayHelper::multisort($list, 'basename', SORT_DESC);
42
43
        $pages = [];
44
        foreach ($list as $file) {
45
            if ($file['type'] !== 'file' || $file['basename'][0] === '.') {
46
                continue;
47
            }
48
            $pages[] = AbstractPage::createFromFile($file['path'], $storage);
49
        }
50
        $index = new static($pages, $storage);
51
52
        return $index;
53
    }
54
55
    /**
56
     * @return ArrayDataProvider
57
     */
58
    public function getDataProvider(): ArrayDataProvider
59
    {
60
        return new ArrayDataProvider([
61
            'allModels' => $this->pages,
62
            'pagination' => [
63
                'pageSize' => Yii::$app->getModule('pages')->getPageSize(),
64
            ]
65
        ]);
66
    }
67
68
    public function render(array $params = []): string
69
    {
70
        return Yii::$app->getView()->renderFile('@hipanel/site/views/themes/dataserv/articles/category.php', array_merge($params, [
71
            'dataProvider' => $this->getDataProvider()
72
        ]));
73
    }
74
}
75