Issues (197)

src/Commands/PresenterCommand.php (2 issues)

1
<?php
2
namespace Salah3id\Domains\Commands;
3
4
use Illuminate\Console\Command;
5
use Salah3id\Domains\Repository\Generators\FileAlreadyExistsException;
6
use Salah3id\Domains\Repository\Generators\PresenterGenerator;
7
use Salah3id\Domains\Repository\Generators\TransformerGenerator;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
use Salah3id\Domains\Traits\DomainCommandTrait;
11
12
13
/**
14
 * Class PresenterCommand
15
 * @package Salah3id\Domains\Commands
16
 * @author Anderson Andrade <[email protected]>
17
 */
18
class PresenterCommand extends Command
19
{
20
    use DomainCommandTrait;
21
22
    /**
23
     * The name of command.
24
     *
25
     * @var string
26
     */
27
    protected $name = 'domain:presenter';
28
29
    /**
30
     * The description of command.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new presenter.';
35
36
    /**
37
     * The type of class being generated.
38
     *
39
     * @var string
40
     */
41
    protected $type = 'Presenter';
42
43
    /**
44
     * Execute the command.
45
     *
46
     * @see fire()
47
     * @return void
48
     */
49
    public function handle(){
50
        $this->laravel->call([$this, 'fire'], func_get_args());
51
    }
52
53
    /**
54
     * Execute the command.
55
     *
56
     * @return void
57
     */
58
    public function fire()
59
    {
60
61
        try {
62
            if($this->argument('domain') && $this->argument('domain-path')) {
63
                $domain = $this->argument('domain');
64
                $domainPath = $this->argument('domain-path');
65
            } else {
66
                $domain = $this->getDomainNameForRepo();
67
                $domainPath = $this->laravel['domains']->getDomainPath($domain);
68
            }
69
            
70
            (new PresenterGenerator([
71
                'name'  => $this->argument('name'),
72
                'force' => $this->option('force'),
73
            ],$domain,$domainPath))->run();
74
            $this->info("Presenter created successfully.");
75
76
            if (!\File::exists(app()->path() . '/Transformers/' . $this->argument('name') . 'Transformer.php')) {
0 ignored issues
show
The method path() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            if (!\File::exists(app()->/** @scrutinizer ignore-call */ path() . '/Transformers/' . $this->argument('name') . 'Transformer.php')) {
Loading history...
77
                if ($this->confirm('Would you like to create a Transformer? [y|N]')) {
78
                    (new TransformerGenerator([
79
                        'name'  => $this->argument('name'),
80
                        'force' => $this->option('force'),
81
                    ],$domain,$domainPath))->run();
82
                    $this->info("Transformer created successfully.");
83
                }
84
            }
85
        } catch (FileAlreadyExistsException $e) {
86
            $this->error($this->type . ' already exists!');
87
88
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
89
        }
90
    }
91
92
93
    /**
94
     * The array of command arguments.
95
     *
96
     * @return array
97
     */
98
    public function getArguments()
99
    {
100
        return [
101
            [
102
                'name',
103
                InputArgument::REQUIRED,
104
                'The name of model for which the presenter is being generated.',
105
                null
106
            ],
107
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
108
            ['domain-path', InputArgument::OPTIONAL, 'The path of domain will be used.'],
109
        ];
110
    }
111
112
113
    /**
114
     * The array of command options.
115
     *
116
     * @return array
117
     */
118
    public function getOptions()
119
    {
120
        return [
121
            [
122
                'force',
123
                'f',
124
                InputOption::VALUE_NONE,
125
                'Force the creation if file already exists.',
126
                null
127
            ]
128
        ];
129
    }
130
}
131