Completed
Push — master ( 29aa5c...85c009 )
by Andrii
02:22
created

RenderController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 53
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewPath() 0 4 1
B actionIndex() 0 24 4
A renderPage() 0 14 3
1
<?php
2
3
namespace hiqdev\yii2\modules\pages\controllers;
4
5
use hiqdev\yii2\modules\pages\models\AbstractPage;
6
use hiqdev\yii2\modules\pages\models\PagesIndex;
7
use Yii;
8
use yii\helpers\Html;
9
use yii\web\NotFoundHttpException;
10
11
class RenderController extends \yii\web\Controller
12
{
13
    public function getViewPath()
14
    {
15
        return Yii::$app->getViewPath();
16
    }
17
18
    /**
19
     * Index action.
20
     * @param string $page
21
     * @return string rendered page
22
     */
23
    public function actionIndex($page = null)
24
    {
25
        if (!$page) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $page of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
26
            $page = 'posts';
27
        }
28
29
        $path = $this->module->find($page);
30
31
        if ($path === null) {
32
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
33
        }
34
35
        $meta = $this->module->getMetadata($path);
36
37
        if ($meta['type'] === 'dir') {
38
            $index = PagesIndex::createFromDir($path);
39
40
            return $this->render('@hiqdev/com/views/site/index', ['dataProvider' => $index->getDataProvider()]);
41
        } else {
42
            $page = AbstractPage::createFromFile($path);
43
44
            return $this->renderPage($page);
45
        }
46
    }
47
48
    public function renderPage($page, array $params = [])
49
    {
50
        if ($page->layout) {
51
            $this->layout = $page->layout;
52
        }
53
54
        if ($page->title) {
55
            $this->view->title = Html::encode($page->title);
56
        }
57
58
        $this->view->params = $page->getData();
59
60
        return $this->renderContent($page->render($params));
61
    }
62
63
}
64