Completed
Push — 2.0 ( fe8057...a5342d )
by Kirill
06:55
created

SearchQuery::queryArguments()   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 GraphQL\Type\Definition\Type;
12
use Illuminate\Support\Collection;
13
use Folklore\GraphQL\Support\Query;
14
use App\GraphQL\Types\SearchResultType;
15
use GraphQL\Type\Definition\ListOfType;
16
use Service\SearchService\SearchService;
17
use App\GraphQL\Serializers\SearchResultsSerializer;
18
19
/**
20
 * Class SearchQuery.
21
 */
22
class SearchQuery extends AbstractQuery
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $attributes = [
28
        'name'        => 'Article list query',
29
        'description' => 'Returns a list of available articles',
30
    ];
31
32
    /**
33
     * @return ListOfType
34
     */
35
    public function type(): ListOfType
36
    {
37
        return Type::listOf(\GraphQL::type('SearchResult'));
38
    }
39
40
    /**
41
     * @param $root
42
     * @param  array $args
43
     * @return Collection
44
     * @throws \InvalidArgumentException
45
     */
46
    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...
47
    {
48
        $search = app(SearchService::class);
49
50
        $repo = $search->findByName($args['type']);
51
52
        if ($repo === null) {
53
            $message = 'Invalid type %s. Available types: %s';
54
            $types = implode(', ', $search->getCategories());
55
56
            throw new \InvalidArgumentException(sprintf($message, $args['type'], $types));
57
        }
58
59
        $result = $repo->getSearchResults($args['query'], $this->formatLimit($args));
60
61
        return SearchResultsSerializer::collection($result);
62
    }
63
64
    /**
65
     * @param array $args
66
     * @return int
67
     */
68
    private function formatLimit(array $args = []): int
69
    {
70
        $limit = $args['_limit'] ?? 10;
71
72
        return max(10, min(100, $limit));
73
    }
74
75
    /**
76
     * @return array
77
     */
78 View Code Duplication
    public function queryArguments(): 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...
79
    {
80
        return [
81
            '_limit' => [
82
                'type'        => Type::int(),
83
                'description' => 'Search results limit',
84
            ],
85
            'type'  => [
86
                'type'        => Type::nonNull(Type::string()),
87
                'description' => 'Search category',
88
            ],
89
            'query' => [
90
                'type'        => Type::nonNull(Type::string()),
91
                'description' => 'Search query',
92
            ],
93
        ];
94
    }
95
}
96