Passed
Push — master ( 37fbce...eb9638 )
by Mihail
09:07
created

EntityContentSearch::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Apps\Model\Front\Content;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Exception\NotFoundException;
8
use Ffcms\Core\Helper\Serialize;
9
use Ffcms\Core\Helper\Text;
10
use Ffcms\Core\Helper\Type\Obj;
11
use Ffcms\Core\Helper\Type\Str;
12
use Apps\ActiveRecord\Content as ContentEntity;
13
14
class EntityContentSearch extends Model
15
{
16
    const MAX_ITEMS = 5;
17
    const MIN_QUERY_LENGTH = 2;
18
19
    public $items;
20
21
    private $_terms;
22
    private $_skip = [0];
23
    private $_records;
24
25
    /**
26
     * EntityContentSearch constructor. Pass search terms (query string) to model and used items to skip it by id.
27
     * @param $terms
28
     * @param int|array $skipIds
29
     */
30
    public function __construct($terms, $skipIds = 0)
31
    {
32
        $this->_terms = App::$Security->strip_tags(trim($terms, ' '));
33
        if (Obj::isLikeInt($skipIds)) {
34
            $this->_skip = [$skipIds];
35
        } elseif (Obj::isArray($skipIds)) {
36
            $this->_skip = $skipIds;
0 ignored issues
show
Documentation Bug introduced by
It seems like $skipIds of type integer is incompatible with the declared type array of property $_skip.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        }
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Prepare conditions to build content list
43
     * @throws NotFoundException
44
     */
45
    public function before()
46
    {
47
        // check length of passed terms
48
        if (!Obj::isString($this->_terms) || Str::length($this->_terms) < self::MIN_QUERY_LENGTH) {
0 ignored issues
show
Bug introduced by
It seems like $this->_terms can also be of type array; however, Ffcms\Core\Helper\Type\Str::length() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
49
            throw new NotFoundException(__('Search terms is too short'));
50
        }
51
52
        // lets make active record building
53
        $this->_records = ContentEntity::whereNotIn('id', $this->_skip)
54
            ->search($this->_terms)
55
            ->take(self::MAX_ITEMS)
56
            ->get();
57
        $this->buildContent();
58
        parent::before();
59
    }
60
61
    /**
62
     * Build content items as array
63
     */
64
    private function buildContent()
65
    {
66
        if ($this->_records->count() < 1) {
67
            return;
68
        }
69
70
        foreach ($this->_records as $item) {
71
            /** @var \Apps\ActiveRecord\Content $item */
72
            // full text
73
            $text = Serialize::getDecodeLocale($item->text);
74
            // remove html
75
            $text = App::$Security->strip_tags($text);
0 ignored issues
show
Bug introduced by
It seems like $text can also be of type null; however, Ffcms\Core\Helper\Security::strip_tags() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
            // build items
77
            $this->items[] = [
78
                'title' => $item->getLocaled('title'),
79
                'snippet' => Text::snippet($text),
80
                'uri' => '/content/read/' . $item->getPath(),
81
                'thumb' => $item->getPosterThumbUri()
82
            ];
83
        }
84
    }
85
86
87
88
}