Completed
Push — master ( 7949b1...be02a5 )
by Nick
07:10
created

GenerateJavascript::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4286
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
namespace EloquentJs\Laravel\Console;
4
5
use EloquentJs\Generator\Generator;
6
use EloquentJs\Generator\ModelInputParser;
7
use Illuminate\Console\Command;
8
9
class GenerateJavascript extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'eloquentjs:generate
17
                            {--models= : Models to include in the generated javascript}
18
                            {--namespace=App : Namespace prefix to use with the --models option}
19
                            {--output=public/eloquent.js : Where to save the generated javascript file}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Generate a javascript file for an Eloquent-ish API in the browser';
27
28
    /**
29
     * @var ModelInputParser
30
     */
31
    protected $inputParser;
32
    /**
33
     * @var Generator
34
     */
35
    protected $generator;
36
37
    /**
38
     * Create a new command instance.
39
     *
40
     * @param ModelInputParser $inputParser
41
     * @param Generator $generator
42
     */
43
    public function __construct(ModelInputParser $inputParser, Generator $generator)
44
    {
45
        parent::__construct();
46
47
        $this->inputParser = $inputParser;
48
        $this->generator = $generator;
49
    }
50
51
    /**
52
     * Execute the console command.
53
     *
54
     * @return mixed
55
     */
56
    public function handle()
57
    {
58
        $models = $this->inputParser
59
            ->searchIn(app_path())
60
            ->getEndpointMapping($this->option('models'), $this->option('namespace'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('models') targeting Illuminate\Console\Command::option() can also be of type array; however, EloquentJs\Generator\Mod...r::getEndpointMapping() 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->option('namespace') targeting Illuminate\Console\Command::option() can also be of type array; however, EloquentJs\Generator\Mod...r::getEndpointMapping() does only seem to accept string|null, 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...
61
62
        $this->promptUserForMissingEndpoints($models);
63
64
        $this->printMapping($models);
65
66
        if ($this->confirm('Generate javascript for these models and associated endpoints?', true)) {
67
            $output = $this->option('output');
68
            $this->generator->saveTo($output)->build($models);
69
            $this->info("Javascript written to {$output}");
70
        }
71
    }
72
73
    /**
74
     * Print the inferred Model => Endpoint mapping.
75
     *
76
     * @param $models
77
     * @return void
78
     */
79
    protected function printMapping($models)
80
    {
81
        $rows = array_map(function($model, $endpoint) {
82
            return [$model, $endpoint];
83
        }, array_keys($models), $models);
84
85
        $this->table(['Model', 'Endpoint'], $rows);
86
    }
87
88
    /**
89
     * Ask user to specify an endpoint for those we failed to determine automatically.
90
     *
91
     * @param array $models
92
     */
93
    protected function promptUserForMissingEndpoints(&$models)
94
    {
95
        array_walk($models, function(&$endpoint, $model) {
96
            if (empty($endpoint)) {
97
                $endpoint = $this->ask("Enter the endpoint to use for the '{$model}' model");
98
            }
99
        });
100
    }
101
}
102