JobMakeCommand::getTemplateContents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Illuminate\Support\Str;
6
use Salah3id\Domains\Support\Config\GenerateConfigReader;
7
use Salah3id\Domains\Support\Stub;
8
use Salah3id\Domains\Traits\DomainCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class JobMakeCommand extends GeneratorCommand
13
{
14
    use DomainCommandTrait;
15
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'domain:make-job';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Create a new job class for the specified domain';
29
30
    protected $argumentName = 'name';
31
32
    public function getDefaultNamespace(): string
33
    {
34
        $domain = $this->laravel['domains'];
35
36
        return $domain->config('paths.generator.jobs.namespace') ?: $domain->config('paths.generator.jobs.path', 'Jobs');
37
    }
38
39
    /**
40
     * Get the console command arguments.
41
     *
42
     * @return array
43
     */
44
    protected function getArguments()
45
    {
46
        return [
47
            ['name', InputArgument::REQUIRED, 'The name of the job.'],
48
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
49
        ];
50
    }
51
52
    /**
53
     * Get the console command options.
54
     *
55
     * @return array
56
     */
57
    protected function getOptions()
58
    {
59
        return [
60
            ['sync', null, InputOption::VALUE_NONE, 'Indicates that job should be synchronous.'],
61
        ];
62
    }
63
64
    /**
65
     * Get template contents.
66
     *
67
     * @return string
68
     */
69
    protected function getTemplateContents()
70
    {
71
        $domain = $this->laravel['domains']->findOrFail($this->getDomainName());
72
73
        return (new Stub($this->getStubName(), [
74
            'NAMESPACE' => $this->getClassNamespace($domain),
75
            'CLASS'     => $this->getClass(),
76
        ]))->render();
77
    }
78
79
    /**
80
     * Get the destination file path.
81
     *
82
     * @return string
83
     */
84
    protected function getDestinationFilePath()
85
    {
86
        $path = $this->laravel['domains']->getDomainPath($this->getDomainName());
87
88
        $jobPath = GenerateConfigReader::read('jobs');
89
90
        return $path . $jobPath->getPath() . '/' . $this->getFileName() . '.php';
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    private function getFileName()
97
    {
98
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type array; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

98
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    protected function getStubName(): string
105
    {
106
        if ($this->option('sync')) {
107
            return '/job.stub';
108
        }
109
110
        return '/job-queued.stub';
111
    }
112
}
113