Completed
Push — master ( c87e7f...53c2c8 )
by Marcel
07:32
created

GenerateDiagramCommand::getModelInstancesInDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\ErdGenerator;
4
5
use ReflectionClass;
6
use Illuminate\Console\Command;
7
use phpDocumentor\GraphViz\Graph;
8
use Illuminate\Support\Collection;
9
use BeyondCode\ErdGenerator\Model as GraphModel;
10
11
class GenerateDiagramCommand extends Command
12
{
13
    const FORMAT_TEXT = 'text';
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'generate:erd {filename=graph.png} {--format=png}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Generate ER diagram.';
28
29
    /** @var ModelFinder */
30
    protected $modelFinder;
31
32
    /** @var RelationFinder */
33
    protected $relationFinder;
34
35
    /** @var Graph */
36
    protected $graph;
37
38
    /** @var GraphBuilder */
39
    protected $graphBuilder;
40
41
    public function __construct(ModelFinder $modelFinder, RelationFinder $relationFinder, GraphBuilder $graphBuilder)
42
    {
43
        parent::__construct();
44
45
        $this->relationFinder = $relationFinder;
46
        $this->modelFinder = $modelFinder;
47
        $this->graphBuilder = $graphBuilder;
48
    }
49
50
    public function handle()
51
    {
52
        $models = $this->getModelsThatShouldBeInspected();
53
54
        $this->info("Found {$models->count()} models.");
55
        $this->info("Inspecing model relations.");
56
57
        $bar = $this->output->createProgressBar($models->count());
58
59
        $models->transform(function ($model) use ($bar) {
60
            $bar->advance();
61
            return new GraphModel(
62
                $model,
63
                (new ReflectionClass($model))->getShortName(),
64
                $this->relationFinder->getModelRelations($model)
65
            );
66
        });
67
68
        $graph = $this->graphBuilder->buildGraph($models);
69
70
        if ($this->option('format') === self::FORMAT_TEXT) {
71
            $this->info($graph->__toString());
72
            return;
73
        }
74
75
        $graph->export($this->option('format'), $this->argument('filename'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('format') targeting Illuminate\Console\Command::option() can also be of type array; however, phpDocumentor\GraphViz\Graph::export() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $this->argument('filename') targeting Illuminate\Console\Command::argument() can also be of type array; however, phpDocumentor\GraphViz\Graph::export() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
76
77
        $this->info(PHP_EOL);
78
        $this->info('Wrote diagram to '.$this->argument('filename'));
79
    }
80
81
    protected function getModelsThatShouldBeInspected(): Collection
82
    {
83
        $directories = config('erd-generator.directories');
84
85
        $modelsFromDirectories = $this->getAllModelsFromEachDirectory($directories);
86
87
        return $modelsFromDirectories;
88
    }
89
90
    protected function getAllModelsFromEachDirectory(array $directories): Collection
91
    {
92
        return collect($directories)
93
            ->map(function ($directory) {
94
                return $this->modelFinder->getModelsInDirectory($directory)->all();
95
            })
96
            ->flatten();
97
    }
98
}