Completed
Push — 2.0 ( 43165f...27f708 )
by Kirill
03:01
created

DocsQuery   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 4 1
A resolve() 0 10 1
A checkProject() 0 6 1
A checkVersion() 0 11 2
A queryArguments() 0 13 1
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\GraphQL\Queries;
10
11
use App\Models\Docs;
12
use App\GraphQL\Types\DocsType;
13
use GraphQL\Type\Definition\Type;
14
use Illuminate\Database\Eloquent\Builder;
15
use Illuminate\Support\Collection;
16
use GraphQL\Type\Definition\ListOfType;
17
use App\GraphQL\Serializers\DocsSerializer;
18
19
/**
20
 * Class DocsQuery.
21
 */
22
class DocsQuery extends AbstractCollectionQuery
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $attributes = [
28
        'name'        => 'Docs list query',
29
        'description' => 'Returns a list of available documentation repositories',
30
    ];
31
32
    /**
33
     * @return ListOfType
34
     */
35
    public function type(): ListOfType
36
    {
37
        return Type::listOf(\GraphQL::type(DocsType::getName()));
38
    }
39
40
    /**
41
     * @param        $root
42
     * @param  array $args
43
     * @return Collection
44
     */
45
    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...
46
    {
47
        $query = $this->queryFor(Docs::class, $args)
48
            ->with('pages');
49
50
        $this->checkVersion($query, $args);
51
        $this->checkProject($query, $args);
52
53
        return DocsSerializer::collection($query->get());
54
    }
55
56
    /**
57
     * @param Builder $query
58
     * @param array $args
59
     * @return Builder|null
60
     */
61
    private function checkProject(Builder $query, array $args): ?Builder
62
    {
63
        return $this->whenExists($args, 'project', function(string $project) use ($query) {
64
            return $query->where('slug', $project);
65
        });
66
    }
67
68
    /**
69
     * @param Builder $query
70
     * @param array $args
71
     * @return Builder
72
     */
73
    private function checkVersion(Builder $query, array $args): ?Builder
74
    {
75
        return $this->whenExists($args, 'version', function (string $version) use ($query) {
76
            if ($version === 'latest') {
77
                return $query->orderBy('version', 'desc')
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
78
                    ->take(1);
79
            }
80
81
            return $query->where('version', $version);
82
        });
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    protected function queryArguments(): array
89
    {
90
        return [
91
            'version' => [
92
                'name'        => 'version',
93
                'type'        => Type::string(),
94
            ],
95
            'project' => [
96
                'name'        => 'project',
97
                'type'        => Type::string(),
98
            ],
99
        ];
100
    }
101
}
102