Completed
Push — master ( eebe63...9dd416 )
by
unknown
09:41
created

TranslationDirective::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BBS\Nova\Translation\GraphQL\Directives;
4
5
use BBS\Nova\Translation\Models\Scopes\TranslatableScope;
6
use GraphQL\Language\AST\FieldDefinitionNode;
7
use GraphQL\Language\AST\NamedTypeNode;
8
use GraphQL\Language\AST\NameNode;
9
use GraphQL\Language\AST\NodeList;
10
use GraphQL\Language\AST\NonNullTypeNode;
11
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
12
use GraphQL\Language\AST\StringValueNode;
13
use GraphQL\Type\Definition\ResolveInfo;
14
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
15
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
16
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
17
use Nuwave\Lighthouse\Schema\Values\FieldValue;
18
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective;
19
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
20
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
21
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
22
23
class TranslationDirective extends BaseDirective implements FieldResolver, FieldManipulator, DefinedDirective
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function name(): string
29
    {
30
        return 'translation';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public static function definition(): string
37
    {
38
        return /* @lang GraphQL */ <<<'SDL'
39
directive @translation(
40
  "Specify the class name of the model to use."
41
  model: String
42
  "Specify the GraphQL type to add the 'locale' field."
43
  type: String
44
) on ARGUMENT_DEFINITION
45
SDL;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode &$parentType): void
52
    {
53
        $typeToAddLocaleField = $this->directiveArgValue('type', class_basename($this->getModelClass()));
54
55
        foreach ($documentAST->types as &$type) {
56
            if ($type->name->value === $typeToAddLocaleField) {
57
                /** @var \GraphQL\Language\AST\ObjectTypeDefinitionNode $type */
58
                $type->fields = ASTHelper::mergeNodeList($type->fields, [$this->defineLocaleField()]);
59
            }
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function resolveField(FieldValue $fieldValue): FieldValue
67
    {
68
        return $fieldValue->setResolver(
69
            function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) {
0 ignored issues
show
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...
70
                $modelClass = $this->getModelClass();
71
72
                $isos = [];
73
                if (isset($args['localeFilters'])) {
74
                    if (isset($args['localeFilters']['locale'])) {
75
                        $isos[] = $args['localeFilters']['locale'];
76
                    }
77
                    if (isset($args['localeFilters']['locales'])) {
78
                        $isos = array_merge($isos, $args['localeFilters']['locales']);
79
                    }
80
                }
81
82
                $table = (new $modelClass)->getTable();
83
                $query = $modelClass::query()
84
                    ->withoutGlobalScope(TranslatableScope::class)
85
                    ->select($table . '.type', $table . '.key', $table . '.value', 'locales.iso AS locale', $table . '.created_at', $table . '.updated_at')
86
                    ->join('translations', $table . '.id', '=', 'translations.translatable_id')
87
                    ->join('locales', 'translations.locale_id', '=', 'locales.id')
88
                    ->where('translations.translatable_type', '=', $modelClass)
89
                    ->where('locales.available_in_api', '=', true);
90
91
                if (! empty($isos)) {
92
                    $query = $query->whereIn('locales.iso', $isos);
93
                }
94
95
                return $query->get();
96
            }
97
        );
98
    }
99
100
    /**
101
     * Setup "locale" field definition.
102
     *
103
     * @return \GraphQL\Language\AST\FieldDefinitionNode
104
     */
105
    protected function defineLocaleField()
106
    {
107
        return new FieldDefinitionNode([
108
            'name' => new NameNode(['value' => 'locale']),
109
            'type' => new NonNullTypeNode(['type' => new NamedTypeNode(['name' => new NameNode(['value' => 'String'])])]),
110
            'arguments' => new NodeList([]),
111
            'directives' => new NodeList([]),
112
            'description' => new StringValueNode(['value' => 'Locale ISO', 'block' => false]),
113
        ]);
114
    }
115
}
116