Completed
Push — master ( e08fa1...2c7876 )
by
unknown
09:46
created

TranslationDirective   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 19.27 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 21
loc 109
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
A definition() 0 11 1
A manipulateFieldDefinition() 0 11 3
A resolveField() 0 32 5
A defineLocaleField() 10 10 1
A defineTranslationIdField() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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