Passed
Push — master ( 340885...a41166 )
by Bruno
04:37 queued 01:25
created

PolicyGenerator::getGenerateFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Targets;
4
5
use GraphQL\Type\Definition\ObjectType;
6
use GraphQL\Type\Definition\Type;
7
use Illuminate\Support\Str;
8
use Modelarium\BaseGenerator;
9
use Modelarium\GeneratedCollection;
10
use Modelarium\GeneratedItem;
11
use Modelarium\Parser;
12
use Nette\PhpGenerator\ClassType;
13
use Nette\PhpGenerator\PhpNamespace;
14
15
class PolicyGenerator extends BaseGenerator
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $stubDir = __DIR__ . "/stubs/";
21
22
    /**
23
     * @var ObjectType
24
     */
25
    protected $type = null;
26
27
    /**
28
     * @var GeneratedCollection
29
     */
30
    protected $collection = null;
31
32
    /**
33
     *
34
     * @var ClassType[]
35
     */
36
    protected $policyClasses = [];
37
38
    /**
39
     * @param Parser $parser
40
     * @param string $name
41
     * @param Type|string $type
42
     * @phpstan-ignore-next-line
43
     */
44 2
    public function __construct(Parser $parser, string $name, $type = null)
45
    {
46 2
        parent::__construct($parser, '', $type);
47 2
        $this->collection = new GeneratedCollection();
48 2
    }
49
50 2
    public function generate(): GeneratedCollection
51
    {
52 2
        foreach ($this->type->getFields() as $field) {
53 1
            $directives = $field->astNode->directives;
54 1
            $this->processDirectives($field, $directives);
55
        }
56
57
58 2
        $printer = new \Nette\PhpGenerator\PsrPrinter;
59
60 2
        foreach ($this->policyClasses as $name => $c) {
61 1
            $namespace = new PhpNamespace('App\\Policies');
62 1
            $namespace->addUse('App\\Models\\User');
63 1
            $namespace->addUse('App\\Models\\' . $name);
64 1
            $namespace->add($c);
65
66 1
            $this->collection->push(
67 1
                new GeneratedItem(
68 1
                    GeneratedItem::TYPE_POLICY,
69 1
                    "<?php declare(strict_types=1);\n\n" . $printer->printNamespace($namespace),
70 1
                    $this->getGenerateFilename($name)
71
                )
72
            );
73
        }
74 2
        return $this->collection;
75
    }
76
77 1
    public function processDirectives(
78
        \GraphQL\Type\Definition\FieldDefinition $field,
79
        \GraphQL\Language\AST\NodeList $directives
80
    ): void {
81 1
        foreach ($directives as $directive) {
82 1
            $name = $directive->name->value;
83 1
            $className = $this->getDirectiveClass($name);
84 1
            if ($className) {
85 1
                $methodName = "$className::processPolicyFieldDirective";
86 1
                $methodName(
87 1
                    $this,
88
                    $field,
89
                    $directive
90
                );
91
            }
92
        }
93 1
    }
94
95 1
    public function getClass(string $name): ClassType
96
    {
97 1
        if (array_key_exists($name, $this->policyClasses)) {
98 1
            return $this->policyClasses[$name];
99
        }
100
101
        /**
102
         * @var ClassType $class
103
         */
104 1
        $class = new ClassType($name . 'Policy');
105
        $class
106 1
            ->addComment("This file was automatically generated by Modelarium.")
107 1
            ->setTraits(['Illuminate\Auth\Access\HandlesAuthorization']);
108 1
        $this->policyClasses[$name] = $class;
109 1
        return $class;
110
    }
111
112 1
    public function getGenerateFilename(string $name): string
113
    {
114 1
        return $this->getBasePath('app/Policies/'. Str::studly($name) . 'Policy.php');
115
    }
116
}
117