Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

SearchComments::getResult()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 33
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Apps\Model\Front\Search;
4
5
use Apps\ActiveRecord\CommentPost;
6
use Apps\Model\Front\Search\Interfaces\SearchContainer;
0 ignored issues
show
Bug introduced by
The type Apps\Model\Front\Search\Interfaces\SearchContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Helper\Text;
10
11
/**
12
 * Class SearchComments. Search instance for comments text.
13
 * @package Apps\Model\Front\Search
14
 */
15
class SearchComments extends Model implements SearchContainer
16
{
17
    private $query;
18
    private $limit;
19
20
    /**
21
     * SearchContainer constructor. Pass string query inside
22
     * @param string $query
23
     * @param int $limit
24
     */
25
    public function __construct($query, $limit = 10)
26
    {
27
        $this->query = $query;
28
        $this->limit = (int)$limit;
29
        if ($this->limit < 1) {
30
            $this->limit = 1;
31
        }
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Build search results. Should return array collection: [AbstractSearchResult]
37
     * @return array
38
     */
39
    public function getResult()
40
    {
41
        // search in comments post
42
        $query = CommentPost::search($this->query)
43
            ->where('moderate', '=', 0)
44
            ->take($this->limit)
45
            ->get();
46
47
        // check if response is empty
48
        if ($query->count() < 1) {
49
            return [];
50
        }
51
52
        // build output
53
        $result = [];
54
        foreach ($query as $item) {
55
            /** @var CommentPost $item */
56
            $snippet = App::$Security->strip_tags($item->message);
57
            $snippet = Text::snippet($snippet);
58
59
            // make unique instance object
60
            $instance = new AbstractSearchResult();
61
            $instance->setTitle(App::$Translate->get('Search', 'Comment on the page'));
62
            $instance->setSnippet($snippet);
63
            $instance->setUri($item->pathway . '#comments-list');
0 ignored issues
show
Bug introduced by
The property pathway does not seem to exist on Apps\ActiveRecord\CommentPost. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
64
            $instance->setDate($item->created_at);
65
            $instance->setRelevance((int)$item->relevance);
0 ignored issues
show
Bug introduced by
The property relevance does not seem to exist on Apps\ActiveRecord\CommentPost. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
66
67
            // add instance to result set
68
            $result[] = $instance;
69
        }
70
71
        return $result;
72
    }
73
}
74