ArticlesSearchRepository::transform()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.ru 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\Article;
10
11
use App\Models\Article;
12
use Illuminate\Support\Collection;
13
use Service\SearchService\SearchResult;
14
use Service\SearchService\Repository\SearchRepositoryInterface;
15
16
/**
17
 * Class ArticlesSearchRepository.
18
 */
19
class ArticlesSearchRepository implements SearchRepositoryInterface
20
{
21
    /**
22
     * @return string
23
     */
24
    public function getName(): string
25
    {
26
        return 'articles';
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
36
        return Article::query()
37
            ->where('content_source', 'LIKE', '%' . $query . '%')
38
            ->take($limit)
39
            ->get(['slug', 'title', 'content_rendered'])
40
            ->map(function (Article $model) {
41
                return $this->transform($model);
42
            });
43
    }
44
45
    /**
46
     * @param Article $article
47
     * @return SearchResult
48
     */
49 View Code Duplication
    private function transform(Article $article): SearchResult
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    {
51
        $dto = new SearchResult();
52
53
        $dto->title = $article->title;
54
        $dto->type = Article::class;
55
56
        $dto->url = route('articles.show', ['slug' => $article->slug]);
57
        $dto->slug = $article->slug;
58
        $dto->body = $article->content_rendered;
59
60
        return $dto;
61
    }
62
}
63