Completed
Push — master ( 1a1da2...265cbd )
by Andrii
17:18 queued 02:24
created

PagesList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 50
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5
ccs 0
cts 27
cp 0
rs 10
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