Passed
Push — 2.0 ( d68ee5...a984b1 )
by Kirill
04:24
created

SearchQuery::args()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 17
loc 17
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\GraphQL\Queries;
10
11
use App\GraphQL\Serializers\SearchResultsSerializer;
12
use GraphQL\Type\Definition\Type;
13
use Illuminate\Support\Collection;
14
use Folklore\GraphQL\Support\Query;
15
use App\GraphQL\Types\SearchResultType;
16
use GraphQL\Type\Definition\ListOfType;
17
use Service\SearchService\SearchService;
18
19
/**
20
 * Class SearchQuery
21
 * @package App\GraphQL\Queries
22
 */
23
class SearchQuery extends Query
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $attributes = [
29
        'name'        => 'Article list query',
30
        'description' => 'Returns a list of available articles',
31
    ];
32
33
    /**
34
     * @return ListOfType
35
     */
36
    public function type(): ListOfType
37
    {
38
        return Type::listOf(\GraphQL::type(SearchResultType::getName()));
39
    }
40
41
    /**
42
     * @param $root
43
     * @param  array $args
44
     * @return Collection
45
     * @throws \InvalidArgumentException
46
     */
47
    public function resolve($root, array $args = []): Collection
0 ignored issues
show
Unused Code introduced by
The parameter $root is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        $search = app(SearchService::class);
50
51
        $repo = $search->findByName($args['type']);
52
53
        if ($repo === null) {
54
            $message = 'Invalid type %s. Available types: %s';
55
            $types   = implode(', ', $search->getCategories());
56
57
            throw new \InvalidArgumentException(sprintf($message, $args['type'], $types));
58
        }
59
60
61
        $result = $repo->getSearchResults($args['query'], $this->formatLimit($args));
62
63
        return SearchResultsSerializer::collection($result);
64
    }
65
66
    /**
67
     * @param array $args
68
     * @return int
69
     */
70
    private function formatLimit(array $args = []): int
71
    {
72
        $limit = $args['_limit'] ?? 10;
73
74
        return max(10, min(100, $limit));
75
    }
76
77
    /**
78
     * @return array
79
     */
80 View Code Duplication
    public function args(): array
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...
81
    {
82
        return [
83
            '_limit' => [
84
                'type'        => Type::int(),
85
                'description' => 'Search results limit',
86
            ],
87
            'type'  => [
88
                'type'        => Type::nonNull(Type::string()),
89
                'description' => 'Search category',
90
            ],
91
            'query' => [
92
                'type'        => Type::nonNull(Type::string()),
93
                'description' => 'Search query',
94
            ],
95
        ];
96
    }
97
}