EnumsSupport   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bootEnumsSupport() 0 10 2
A configs() 0 8 2
A isGraphQLType() 0 4 1
A isEnumType() 0 4 2
A wrapType() 0 8 2
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\Feature;
10
11
use GraphQL\Type\Definition\Type;
12
use CommerceGuys\Enum\AbstractEnum;
13
use App\GraphQL\Feature\Enum\EnumTransfer;
14
use App\GraphQL\Feature\Kernel\FeaturesSupport;
15
16
/**
17
 * Class EnumsSupport
18
 * @package App\GraphQL\Feature
19
 * @mixin FeaturesSupport
20
 */
21
trait EnumsSupport
22
{
23
    /**
24
     * @var EnumTransfer
25
     */
26
    private $transfer;
27
28
    /**
29
     * @return void
30
     * @throws \ReflectionException
31
     */
32
    protected function bootEnumsSupport(): void
33
    {
34
        $this->transfer = new EnumTransfer();
35
36
        $this->addFieldsWrapper(function (array $arguments) {
0 ignored issues
show
Bug introduced by
It seems like addFieldsWrapper() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
            foreach ($arguments as $key => $data) {
38
                yield $key => $this->configs($data);
39
            }
40
        });
41
    }
42
43
    /**
44
     * @param array $config
45
     * @return array
46
     * @throws \ReflectionException
47
     */
48
    private function configs(array $config): array
49
    {
50
        if (! $this->isGraphQLType($config)) {
51
            $config['type'] = $this->wrapType($config['type']);
52
        }
53
54
        return $config;
55
    }
56
57
    /**
58
     * @param array $config
59
     * @return bool
60
     */
61
    private function isGraphQLType(array $config): bool
62
    {
63
        return $config['type'] instanceof Type;
64
    }
65
66
    /**
67
     * @param AbstractEnum|string|mixed $type
68
     * @return bool
69
     */
70
    private function isEnumType($type): bool
71
    {
72
        return is_string($type) && is_subclass_of($type, AbstractEnum::class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \CommerceGuys\Enum\AbstractEnum::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
73
    }
74
75
    /**
76
     * @param AbstractEnum|string|mixed $type
77
     * @return \GraphQL\Type\Definition\EnumType|mixed
78
     * @throws \ReflectionException
79
     */
80
    private function wrapType($type)
81
    {
82
        if ($this->isEnumType($type)) {
83
            return $this->transfer->toGraphQL($type);
84
        }
85
86
        return $type;
87
    }
88
}