Passed
Push — master ( b534e3...17b30a )
by Mihail
19:51
created

ActionRead   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 68
rs 10
wmc 10
lcom 1
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
C read() 0 59 10
1
<?php
2
3
namespace Apps\Controller\Front\Content;
4
5
use Apps\ActiveRecord\ContentCategory;
6
use Apps\Model\Front\Content\EntityContentRead;
7
use Apps\Model\Front\Content\EntityContentSearch;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
use Apps\ActiveRecord\Content as ContentEntity;
14
15
/**
16
 * Trait ActionRead
17
 * @package Apps\Controller\Front\Content
18
 * @property View $view
19
 * @property Request $request
20
 * @property Response $response
21
 * @method array getConfigs
22
 */
23
trait ActionRead
24
{
25
    /**
26
     * Show content item
27
     * @throws NotFoundException
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     * @return string
30
     */
31
    public function read()
32
    {
33
        // get raw path without controller-action
34
        $rawPath = $this->request->getPathWithoutControllerAction();
35
        $arrayPath = explode('/', $rawPath);
36
        // get category and content item path as string
37
        $contentPath = array_pop($arrayPath);
38
        $categoryPath = implode('/', $arrayPath);
39
40
        // try to find category object by string pathway
41
        $categoryRecord = ContentCategory::getByPath($categoryPath);
42
43
        // if no categories are available for this path - throw exception
44
        if ($categoryRecord === null || $categoryRecord->count() < 1) {
45
            throw new NotFoundException(__('Page not found'));
46
        }
47
48
        // try to find content entity record
49
        $contentRecord = ContentEntity::where('path', '=', $contentPath)
50
            ->where('category_id', '=', $categoryRecord->id);
51
        $trash = false;
52
53
        // if no entity is founded for this path lets try to find on trashed
54
        if ($contentRecord === null || $contentRecord->count() !== 1) {
55
            // check if user can access to content list on admin panel
56
            if (!App::$User->isAuth() || !App::$User->identity()->role->can('Admin/Content/Index')) {
57
                throw new NotFoundException();
58
            }
59
            // lets try to find in trashed
60
            $contentRecord->withTrashed();
61
            // no way, lets throw exception
62
            if ($contentRecord->count() !== 1) {
63
                throw new NotFoundException();
64
            }
65
            // set trashed marker for this item
66
            $trash = true;
67
        }
68
69
        // lets init entity model for content transfer to view
70
        $model = new EntityContentRead($categoryRecord, $contentRecord->first());
0 ignored issues
show
Bug introduced by
It seems like $contentRecord->first() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
        $search = null;
72
        // check if similar search is enabled for item category
73
        if ((int)$model->getCategory()->getProperty('showSimilar') === 1 && $trash === false) {
74
            $search = new EntityContentSearch($model->title, $model->id, $model->getCategory()->id);
75
        }
76
77
        // define read event
78
        App::$Event->run(static::EVENT_CONTENT_READ, [
79
            'model' => $model
80
        ]);
81
82
        // render view output
83
        return $this->view->render('read', [
84
            'model' => $model,
85
            'search' => $search,
86
            'trash' => $trash,
87
            'configs' => $this->getConfigs()
0 ignored issues
show
Bug introduced by
It seems like getConfigs() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
        ]);
89
    }
90
}
91