Passed
Push — master ( 488e94...db825e )
by Bruno
10:13
created

EventGenerator::processFieldDirectives()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 6
nop 3
dl 0
loc 22
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.7998
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Targets;
4
5
use GraphQL\Type\Definition\NonNull;
6
use GraphQL\Type\Definition\ObjectType;
7
use GraphQL\Type\Definition\Type;
8
use Modelarium\BaseGenerator;
9
use Modelarium\GeneratedCollection;
10
use Modelarium\GeneratedItem;
11
use Modelarium\Parser;
12
use Nette\PhpGenerator\ClassType;
13
14
class EventGenerator extends BaseGenerator
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $stubDir = __DIR__ . "/stubs/";
20
21
    /**
22
     * @var ObjectType
23
     */
24
    protected $type = null;
25
26
    /**
27
     * @var GeneratedCollection
28
     */
29
    protected $events;
30 2
31
    public function generate(): GeneratedCollection
32 2
    {
33
        $this->events = new GeneratedCollection();
34 2
35 1
        foreach ($this->type->getFields() as $field) {
36 1
            $directives = $field->astNode->directives;
37
            $this->processFieldDirectives($field, $directives, 'Event');
38 2
        }
39
        return $this->events;
40
    }
41 1
42
    protected function makeEventClass(string $name, string $model): GeneratedItem
43 1
    {
44
        list($eventNamespace, $eventClassName, $relativePath) = $this->splitClassName($name);
45 1
46
        $namespace = new \Nette\PhpGenerator\PhpNamespace($eventNamespace);
47
48
        /**
49
         * @var ClassType $class
50 1
         */
51
        $class = $namespace->addClass($eventClassName);
52 1
        $class
53 1
            ->addComment("This file was automatically generated by Modelarium.")
54
            ->setTraits(['Illuminate\Queue\SerializesModels']);
55 1
        
56 1
        $class->addProperty('target')
57 1
            ->setPublic()
58 1
            ->setComment("@var $model");
59
        $printer = new \Nette\PhpGenerator\PsrPrinter;
60 1
    
61 1
        $constructor = $class->addMethod('__construct');
62 1
        $constructor->setPublic()
63 1
            ->addParameter('target')
64
            ->setType('App\\Models\\' . $model);
65 1
66 1
        $constructor->addBody(
67
            '$this->target = $target;'
68
        );
69 1
70 1
        return new GeneratedItem(
71 1
            $name,
72 1
            "<?php declare(strict_types=1);\n\n" . $printer->printNamespace($namespace),
73
            $this->getGenerateFilename($relativePath, $eventClassName)
74
        );
75
    }
76 1
77
    public function processFieldDirectives(
78
        \GraphQL\Type\Definition\FieldDefinition $field,
79
        \GraphQL\Language\AST\NodeList $directives,
80
        string $type
81
    ): void {
82 1
        // TODO: there's probably no need to override this
83
        if ($field->type instanceof NonNull) {
84
            $type = $field->type->getWrappedType();
85 1
        } else {
86
            $type = $field->type;
87 1
        }
88
        $typeName = $type->name; /** @phpstan-ignore-line */
89 1
        
90 1
        foreach ($directives as $directive) {
91 1
            $name = $directive->name->value;
92 1
            switch ($name) {
93 1
            case 'event':
94
                $dispatch = Parser::getDirectiveArgumentByName($directive, 'dispatch');
95 1
                $e = $this->makeEventClass($dispatch, $typeName);
0 ignored issues
show
Bug introduced by
It seems like $dispatch can also be of type array and array and null; however, parameter $name of Modelarium\Laravel\Targe...rator::makeEventClass() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
                $e = $this->makeEventClass(/** @scrutinizer ignore-type */ $dispatch, $typeName);
Loading history...
96
                $this->events->push($e);
97
                break;
98
            default:
99
            }
100 1
        }
101
    }
102 1
103 1
    public function getGenerateFilename(string $relativePath, string $name): string
104 1
    {
105 1
        // fix App -> app
106
        if (mb_substr($relativePath, 0, 3) === 'App' && !is_dir($this->getBasePath($relativePath))) {
107
            $relativePath[0] = 'a';
108 1
        }
109 1
        return $this->getBasePath($relativePath . '/' . $name . '.php');
110 1
    }
111
}
112