Passed
Push — master ( 087cc0...37fbce )
by Mihail
04:19
created

Content::actionTag()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.439
cc 5
eloc 13
nc 4
nop 1
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\ActiveRecord\ContentCategory;
6
use Apps\ActiveRecord\Content as ContentEntity;
7
use Extend\Core\Arch\FrontAppController;
8
use Ffcms\Core\App;
9
use Apps\Model\Front\Content\EntityCategoryList;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Apps\Model\Front\Content\EntityContentRead;
12
use Ffcms\Core\Helper\HTML\SimplePagination;
13
use Ffcms\Core\Helper\Type\Str;
14
15
/**
16
 * Class Content. Controller of content app - content and categories.
17
 * @package Apps\Controller\Front
18
 */
19
class Content extends FrontAppController
20
{
21
    const TAG_PER_PAGE = 50;
22
23
    /**
24
     * Index is forbidden
25
     * @throws NotFoundException
26
     */
27
    public function actionIndex()
28
    {
29
        throw new NotFoundException();
30
    }
31
32
    /**
33
     * List category content
34
     * @throws NotFoundException
35
     * @throws \Ffcms\Core\Exception\SyntaxException
36
     * @throws \Ffcms\Core\Exception\NativeException
37
     * @return string
38
     */
39
    public function actionList()
40
    {
41
        $path = App::$Request->getPathWithoutControllerAction();
42
        $configs = $this->getConfigs();
43
        $page = (int)App::$Request->query->get('page');
44
        $itemCount = (int)$configs['itemPerCategory'];
45
46
        // build special model with content list and category list information
47
        $model = new EntityCategoryList($path, $configs, $page);
48
49
        // build pagination
50
        $pagination = new SimplePagination([
51
            'url' => ['content/list', $path],
52
            'page' => $page,
53
            'step' => $itemCount,
54
            'total' => $model->getContentCount()
55
        ]);
56
57
        // drow response view
58
        return App::$View->render('list', [
59
            'model' => $model,
60
            'pagination' => $pagination,
61
            'configs' => $configs,
62
        ]);
63
    }
64
65
    /**
66
     * Show content item
67
     * @throws NotFoundException
68
     * @throws \Ffcms\Core\Exception\SyntaxException
69
     * @throws \Ffcms\Core\Exception\NativeException
70
     * @return string
71
     */
72
    public function actionRead()
73
    {
74
        // get raw path without controller-action
75
        $rawPath = App::$Request->getPathWithoutControllerAction();
76
        $arrayPath = explode('/', $rawPath);
77
        // get category and content item path as string
78
        $contentPath = array_pop($arrayPath);
79
        $categoryPath = implode('/', $arrayPath);
80
81
        // try to find category object by string pathway
82
        $categoryRecord = ContentCategory::getByPath($categoryPath);
83
84
        // if no categories are available for this path - throw exception
85
        if ($categoryRecord === null || $categoryRecord->count() < 1) {
86
            throw new NotFoundException();
87
        }
88
89
        // try to find content entity record
90
        $contentRecord = ContentEntity::where('path', '=', $contentPath)->where('category_id', '=', $categoryRecord->id);
91
        $trash = false;
92
93
        // if no entity is founded for this path lets try to find on trashed
94
        if ($contentRecord === null || $contentRecord->count() !== 1) {
95
            // check if user can access to content list on admin panel
96 View Code Duplication
            if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('Admin/Content/Index')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
                throw new NotFoundException();
98
            }
99
            // lets try to find in trashed
100
            $contentRecord->withTrashed();
101
            // no way, lets throw exception
102
            if ($contentRecord->count() !== 1) {
103
                throw new NotFoundException();
104
            }
105
            // set trashed marker for this item
106
            $trash = true;
107
        }
108
109
        // lets init entity model for content transfer to view
110
        $model = new EntityContentRead($categoryRecord, $contentRecord->first());
111
112
        return App::$View->render('read', [
113
            'model' => $model,
114
            'trash' => $trash,
115
            'configs' => $this->getConfigs()
116
        ]);
117
    }
118
119
    /**
120
     * List latest by created_at content items contains tag name
121
     * @param string $tagName
122
     * @return string
123
     * @throws NotFoundException
124
     * @throws \Ffcms\Core\Exception\SyntaxException
125
     */
126
    public function actionTag($tagName)
127
    {
128
        $configs = $this->getConfigs();
129
        // check if tags is enabled
130
        if ((int)$configs['keywordsAsTags'] !== 1) {
131
            throw new NotFoundException(__('Tag system is disabled'));
132
        }
133
134
        // remove spaces and other shits
135
        $tagName = trim($tagName);
136
137
        // check if tag is not empty
138
        if (Str::likeEmpty($tagName) || Str::length($tagName) < 2) {
139
            throw new NotFoundException(__('Tag is empty or is too short!'));
140
        }
141
142
        // get equal rows order by creation date
143
        $records = ContentEntity::where('meta_keywords', 'like', '%' . $tagName . '%')->orderBy('created_at', 'DESC')->take(self::TAG_PER_PAGE);
144
        // check if result is not empty
145
        if ($records->count() < 1) {
146
            throw new NotFoundException(__('Nothing founded'));
147
        }
148
149
        // render response
150
        return App::$View->render('tag', [
151
            'records' => $records->get(),
152
            'tag' => App::$Security->strip_tags($tagName)
153
        ]);
154
    }
155
156
    public function actionRss()
157
    {
158
        $path = App::$Request->getPathWithoutControllerAction();
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
    }
160
}
161