1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of laravel.su package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace App\Models\DocsPage; |
10
|
|
|
|
11
|
|
|
use App\Models\DocsPage; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Service\SearchService\SearchResult; |
14
|
|
|
use Service\SearchService\Repository\SearchRepositoryInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DocsPagesSearchRepository. |
18
|
|
|
*/ |
19
|
|
|
class DocsPagesSearchRepository implements SearchRepositoryInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function getName(): string |
25
|
|
|
{ |
26
|
|
|
return 'docs'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $query |
31
|
|
|
* @param int $limit |
32
|
|
|
* @return Collection|SearchResult[] |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function getSearchResults(string $query, int $limit = 10): Collection |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
return DocsPage::query() |
37
|
|
|
->with('docs') |
38
|
|
|
->where('content_source', 'LIKE', '%' . $query . '%') |
39
|
|
|
->take($limit) |
40
|
|
|
->get(['docs_id', 'slug', 'title', 'content_rendered']) |
41
|
|
|
->map(function (DocsPage $model) { |
42
|
|
|
return $this->transform($model); |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param DocsPage $page |
48
|
|
|
* @return SearchResult |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
private function transform(DocsPage $page): SearchResult |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$dto = new SearchResult(); |
53
|
|
|
|
54
|
|
|
$dto->title = $page->title; |
55
|
|
|
$dto->type = DocsPage::class; |
56
|
|
|
$dto->slug = $page->slug; |
57
|
|
|
$dto->body = $page->content_rendered; |
58
|
|
|
|
59
|
|
|
$dto->url = route('docs.show', [ |
60
|
|
|
'version' => $page->docs->version, |
61
|
|
|
'slug' => $page->slug, |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
return $dto; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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.