Completed
Push — 2.0 ( d39b3c...f2914f )
by Kirill
02:52
created

EnumsSupport::bootEnumsSupport()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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 CommerceGuys\Enum\AbstractEnum;
12
use GraphQL\Type\Definition\Type;
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
     */
31
    protected function bootEnumsSupport(): void
32
    {
33
        $this->transfer = new EnumTransfer();
34
35
        $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...
36
            foreach ($arguments as $key => $data) {
37
                yield $key => $this->configs($data);
38
            }
39
        });
40
    }
41
42
    /**
43
     * @param array $config
44
     * @return array
45
     */
46
    private function configs(array $config): array
47
    {
48
        if ($config['type'] instanceof Type) {
49
            return $config;
50
        }
51
52
        $config['type'] = $this->wrapType($config['type']);
53
54
        return $config;
55
    }
56
57
    /**
58
     * @param AbstractEnum|string|mixed $type
59
     * @return \GraphQL\Type\Definition\EnumType|mixed
60
     * @throws \ReflectionException
61
     */
62
    private function wrapType($type)
63
    {
64
        if (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...
65
            return $this->transfer->toGraphQL($type);
66
        }
67
68
        return $type;
69
    }
70
}