Passed
Push — master ( fcf8a0...ba2801 )
by Mihail
04:49
created

Newcontent::init()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 16
Code Lines 8

Duplication

Lines 6
Ratio 37.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 16
rs 8.8571
cc 6
eloc 8
nc 8
nop 0
1
<?php
2
namespace Widgets\Front\Newcontent;
3
4
use Ffcms\Core\App;
5
use Extend\Core\Arch\FrontWidget as Widget;
6
use Ffcms\Core\Helper\Serialize;
7
use Ffcms\Core\Helper\Type\Obj;
8
use Ffcms\Core\Traits\OopTools;
9
use Apps\ActiveRecord\Content;
10
11
class Newcontent extends Widget
12
{
13
    use OopTools;
14
    
15
    public $categories;
16
    public $cache;
17
    public $count;
18
    
19
    public $tpl = 'widgets/newcontent/default';
20
    
21
    /**
22
     * Prepare widget. Set default configs if not defined on initialization
23
     * {@inheritDoc}
24
     * @see \Ffcms\Core\Arch\Widget::init()
25
     */
26
    public function init()
27
    {
28
        $cfg = $this->getConfigs();
29
        // check if categories is empty
30
        if ($this->categories === null) {
31
            $this->categories = Serialize::decode($cfg['categories']);
32
        }
33
        // check cache is defined
34 View Code Duplication
        if ($this->cache === null || !Obj::isLikeInt($this->cache)) {
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...
35
            $this->cache = (int)$cfg['cache'];
36
        }
37
        // check item count is defined
38 View Code Duplication
        if ($this->count === null || !Obj::isLikeInt($this->count)) {
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...
39
            $this->count = (int)$cfg['count'];
40
        }
41
    }
42
    
43
    /**
44
     * Display new content widget logic
45
     * {@inheritDoc}
46
     * @see \Ffcms\Core\Arch\Widget::display()
47
     */
48
    public function display()
49
    {
50
        // get unique instance hash with current params
51
        $cacheHash = $this->createStringClassSnapshotHash();
52
        
53
        $query = null;
0 ignored issues
show
Unused Code introduced by
$query 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...
54
        // cache is disabled, get result directly
55
        if ($this->cache === 0) {
56
            $query = $this->makeQuery();
57
        } else {
58
            // try get query result from cache
59
            $query = App::$Cache->get('widget.newcontent.' . $cacheHash);
60
            if ($query === null) {
61
                // if query is not cached make it
62
                $query = $this->makeQuery();
63
                // and save result to cache
64
                App::$Cache->set('widget.newcontent.' . $cacheHash, $query, $this->cache);
0 ignored issues
show
Documentation introduced by
$query is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
            }
66
        }
67
        
68
        // check if response is not empty
69
        if ($query->count() < 1) {
70
            return __('Content is not founded');
71
        }
72
        
73
        // render view
74
        return App::$View->render($this->tpl, [
75
           'records' => $query 
76
        ]);
77
    }
78
    
79
    /**
80
     * Make query to database
81
     * @return object
82
     */
83
    private function makeQuery()
84
    {
85
        return Content::select(['contents.*', 'content_categories.path as cpath', 'content_categories.title as ctitle'])
0 ignored issues
show
Bug introduced by
The method select() does not exist on Apps\ActiveRecord\Content. Did you maybe mean addSelectsToQuery()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86
                ->whereIn('contents.category_id', $this->categories)
87
                ->join('content_categories', 'content_categories.id', '=', 'contents.category_id', 'left outer')
88
                ->orderBy('contents.created_at', 'DESC')
89
                ->take($this->count)->get();
90
    }
91
}