Completed
Push — master ( 283304...d289a1 )
by
unknown
08:46
created

ByLocale   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
filterLocales() 0 1 ?
A resolve() 0 27 3
1
<?php
2
3
namespace BBS\Nova\Translation\GraphQL\Queries\Translation;
4
5
use BBS\Nova\Translation\Models\Label;
6
use BBS\Nova\Translation\Models\Locale;
7
use BBS\Nova\Translation\Models\Scopes\TranslatableScope;
8
use GraphQL\Type\Definition\ResolveInfo;
9
use Illuminate\Database\Eloquent\Builder;
10
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
11
12
abstract class ByLocale
13
{
14
    /**
15
     * Filter locales Eloquent Builder depending of given arguments.
16
     *
17
     * @param  \Illuminate\Database\Eloquent\Builder  $query
18
     * @param  array  $args
19
     * @return \Illuminate\Database\Eloquent\Builder
20
     */
21
    abstract protected function filterLocales(Builder $query, array $args);
22
23
    /**
24
     * Return a value for the field.
25
     *
26
     * @param  mixed  $rootValue
27
     * @param  array  $args
28
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context
29
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo
30
     * @return array
31
     */
32
    public function resolve($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
0 ignored issues
show
Unused Code introduced by
The parameter $rootValue 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...
Unused Code introduced by
The parameter $context 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...
Unused Code introduced by
The parameter $resolveInfo 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...
33
    {
34
        $query = Locale::query()->availableInApi()->select('id', 'iso');
35
        $query = $this->filterLocales($query, $args);
36
        $locales = $query->get()->pluck('iso', 'id')->toArray();
37
38
        $labels = Label::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

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...
39
            ->withoutGlobalScope(TranslatableScope::class)
40
            ->select('labels.key', 'labels.value', 'translations.locale_id')
41
            ->join('translations', 'labels.id', '=', 'translations.translatable_id')
42
            ->where('translations.translatable_type', '=', Label::class)
43
            ->whereIn('translations.locale_id', array_keys($locales))
44
            ->get();
45
46
        $json = [];
47
        foreach ($labels as $label) {
48
            $iso = $locales[$label->locale_id];
49
            if (! isset($json[$iso])) {
50
                $json[$iso] = [];
51
            }
52
            $json[$iso][$label->key] = $label->value;
53
        }
54
55
        return [
56
            'json' => $json,
57
        ];
58
    }
59
}
60