1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Search; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Apps\ActiveRecord\CommentPost; |
7
|
|
|
use Apps\Model\Front\Search\Interfaces\SearchContainer; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Arch\Model; |
10
|
|
|
use Ffcms\Core\Helper\Text; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SearchComments. Search instance for comments text. |
14
|
|
|
* @package Apps\Model\Front\Search |
15
|
|
|
*/ |
16
|
|
|
class SearchComments extends Model implements SearchContainer |
17
|
|
|
{ |
18
|
|
|
private $query; |
19
|
|
|
private $limit; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SearchContainer constructor. Pass string query inside |
23
|
|
|
* @param string $query |
24
|
|
|
* @param int $limit |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function __construct($query, $limit = 10) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->query = $query; |
29
|
|
|
$this->limit = (int)$limit; |
30
|
|
|
if ($this->limit < 1) { |
31
|
|
|
$this->limit = 1; |
32
|
|
|
} |
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Build search results. Should return array collection: [AbstractSearchResult] |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function getResult() |
41
|
|
|
{ |
42
|
|
|
// search in comments post |
43
|
|
|
$query = CommentPost::where('moderate', '=', 0) |
44
|
|
|
->search($this->query) |
45
|
|
|
->take($this->limit) |
46
|
|
|
->get(); |
47
|
|
|
|
48
|
|
|
// check if response is empty |
49
|
|
|
if ($query->count() < 1) { |
50
|
|
|
return []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// build output |
54
|
|
|
$result = []; |
55
|
|
|
foreach ($query as $item) { |
56
|
|
|
/** @var CommentPost $item */ |
57
|
|
|
$snippet = App::$Security->strip_tags($item->message); |
58
|
|
|
$snippet = Text::snippet($snippet); |
|
|
|
|
59
|
|
|
|
60
|
|
|
// make unique instance object |
61
|
|
|
$instance = new AbstractSearchResult(); |
62
|
|
|
$instance->setTitle(App::$Translate->get('Search', 'Comment on the page')); |
63
|
|
|
$instance->setSnippet($snippet); |
64
|
|
|
$instance->setUri($item->pathway . '#comments-list'); |
65
|
|
|
$instance->setDate($item->created_at); |
66
|
|
|
$instance->setRelevance((int)$item->relevance); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// add instance to result set |
69
|
|
|
$result[] = $instance; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
} |
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.