Passed
Push — master ( 42cd76...fd00d5 )
by Bruno
04:07
created

CommandRenderable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
eloc 40
c 2
b 1
f 0
dl 0
loc 92
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 56 9
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Laravel\Commands;
4
5
use Formularium\Exception\Exception;
6
use Formularium\Factory\DatatypeFactory;
7
use Formularium\Factory\FrameworkFactory;
8
use Formularium\Factory\RenderableFactory;
9
use Formularium\Frontend\Blade\Framework;
10
use FormulariumTests\DatatypeTest;
11
use Illuminate\Console\Command;
12
13
class CommandRenderable extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'formularium:renderable
21
        {name : The datatype name}
22
        {--framework=* : The frameworks to use. You can use this options several times. Use "*" for all.}
23
        {--namespace=Formularium\\Frontend : base namespace. Defaults to Formularium\\Frontend }
24
        {--path= : path to save the file. Defaults to base_path("app/Frontend") }
25
    ';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Creates renderable scaffolding';
33
34
    /**
35
     * Create a new command instance.
36
     *
37
     * @return void
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * Execute the console command.
46
     *
47
     * @return mixed
48
     */
49
    public function handle()
50
    {
51
        $frameworkNames = $this->option('framework');
52
        if (is_string($frameworkNames)) {
0 ignored issues
show
introduced by
The condition is_string($frameworkNames) is always false.
Loading history...
53
            $frameworkNames = [$frameworkNames];
54
        }
55
        /**
56
         * @var array $frameworkNames
57
         */
58
        if (in_array('*', $frameworkNames)) {
59
            $frameworks = FrameworkFactory::factoryAll();
60
        } else {
61
            $frameworks = array_map(
62
                [FrameworkFactory::class, 'factory'],
63
                $frameworkNames
64
            );
65
        }
66
67
        $name = $this->argument('name');
68
        if (!is_string($name)) {
69
            $this->error('Name must be a string');
70
            return;
71
        }
72
        $datatype = DatatypeFactory::factory($name);
73
        $datatypeLower = mb_strtolower($datatype->getName());
74
        $baseNamespace = $this->option('namespace');
75
        if (!is_string($baseNamespace)) {
0 ignored issues
show
introduced by
The condition is_string($baseNamespace) is always true.
Loading history...
76
            $this->error('namespace must be a string');
77
            return;
78
        }
79
80
        $basePath = $this->option('path');
81
        if (!is_string($basePath)) {
0 ignored issues
show
introduced by
The condition is_string($basePath) is always true.
Loading history...
82
            $this->error('path must be a string');
83
            return;
84
        }
85
86
        $printer = new \Nette\PhpGenerator\PsrPrinter();
87
88
        foreach ($frameworks as $framework) {
89
            /**
90
             * @var Framework $framework
91
             */
92
            $phpns = $framework->generateRenderable($datatype, $baseNamespace);
93
            $code = "<?php declare(strict_types=1);\n" . $printer->printNamespace($phpns);
94
            $basepath = $basePath . '/' . $framework->getName() . '/Renderable/';
95
            if (!is_dir($basepath)) {
96
                \Safe\mkdir($basepath, 0777, true);
97
            }
98
    
99
            $filename = $basepath . 'Renderable_' . $datatypeLower . '.php';
100
            if (!file_exists($filename)) {
101
                $this->info("Created renderable at {$filename}.");
102
                file_put_contents($filename, $code);
103
            } else {
104
                $this->warn("Filename $filename already exists.");
105
            }
106
        }
107
    }
108
}
109