Passed
Push — master ( 1f7f34...948e88 )
by Thomas
04:12 queued 02:03
created

MakeEntity::loadServices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Commands;
4
5
use Illuminate\Console\Command;
6
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
7
use Webfactor\Laravel\Generators\Schemas\Schema;
8
use Webfactor\Laravel\Generators\Services\AddToGitService;
9
use Webfactor\Laravel\Generators\Services\OpenIdeService;
10
11
class MakeEntity extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'make:entity {entity} {--schema=name:string} {--migrate} {--git} {--ide=}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Make Entity';
26
27
    /**
28
     * Paths to files which should automatically be opened in IDE if the
29
     * option --ide is set (and IDE capable).
30
     *
31
     * @var array
32
     */
33
    public $filesToBeOpened = [];
34
35
    /**
36
     * The name of the entity being created.
37
     *
38
     * @var string
39
     */
40
    public $entity;
41
42
    /**
43
     * The Schema object.
44
     *
45
     * @var Schema
46
     */
47
    public $schema;
48
49
    /**
50
     * The naming schema object.
51
     *
52
     * @var array
53
     */
54
    public $naming = [];
55
56
    /**
57
     * Execute the console command.
58
     *
59
     * @return mixed
60
     */
61
    public function handle()
62
    {
63
        $this->entity = $this->argument('entity');
64
65
        $this->loadSchema();
66
        $this->loadNaming();
67
        $this->loadServices();
68
    }
69
70
    private function loadSchema()
71
    {
72
        $this->info('Load Schema');
73
        $this->schema = new Schema($this->option('schema'));
74
        $this->info('Schema loaded');
75
        $this->line('');
76
    }
77
78
    private function loadNaming()
79
    {
80
        $this->info('Load Naming Classes');
81
        $progressBar = $this->output->createProgressBar(count(config('webfactor.generators.naming')));
82
83
        foreach (config('webfactor.generators.naming') as $key => $naming) {
84
            $progressBar->advance();
85
            $this->info(' Naming Class: ' . $naming);
86
87
            $namingObject = new $naming($this->entity);
88
            $this->naming[$key] = $namingObject;
89
        }
90
91
        $progressBar->finish();
92
        $this->info(' Naming Classes loaded');
93
        $this->line('');
94
    }
95
96
    private function loadServices()
97
    {
98
        $services = $this->getServicesToBeExecuted();
99
        $progressBar = $this->output->createProgressBar(count($services));
100
101
        foreach ($services as $serviceClass) {
102
            $progressBar->advance();
103
            $this->info(' Call: ' . $serviceClass);
104
105
            $this->executeService(new $serviceClass($this));
106
        }
107
108
        $progressBar->finish();
109
        $this->info(' Service Classes loaded');
110
        $this->line('');
111
    }
112
113
    private function getServicesToBeExecuted(): array
114
    {
115
        $serviceClassesToBeExecuted = config('webfactor.generators.services', []);
116
        array_push($serviceClassesToBeExecuted, OpenIdeService::class);
117
        array_push($serviceClassesToBeExecuted, AddToGitService::class);
118
119
        return $serviceClassesToBeExecuted;
120
    }
121
122
    private function executeService(ServiceInterface $service)
123
    {
124
        $service->call();
125
    }
126
127
    /**
128
     * Adds file to $filesToBeOpened stack.
129
     *
130
     * @param $file
131
     * @return void
132
     */
133
    public function addFile(?\SplFileInfo $file): void
134
    {
135
        if ($file) {
136
            array_push($this->filesToBeOpened, $file);
137
        }
138
    }
139
}
140