RouteProviderMakeCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 30
dl 0
loc 113
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 4 1
A getWebRoutesPath() 0 3 1
A getDefaultNamespace() 0 5 2
A getControllerNameSpace() 0 5 2
A getOptions() 0 4 1
A getDestinationFilePath() 0 7 1
A getFileName() 0 3 1
A getApiRoutesPath() 0 3 1
A getTemplateContents() 0 14 1
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Salah3id\Domains\Support\Config\GenerateConfigReader;
6
use Salah3id\Domains\Support\Stub;
7
use Salah3id\Domains\Traits\DomainCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class RouteProviderMakeCommand extends GeneratorCommand
12
{
13
    use DomainCommandTrait;
14
15
    protected $argumentName = 'domain';
16
17
    /**
18
     * The command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'domain:route-provider';
23
24
    /**
25
     * The command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Create a new route service provider for the specified domain.';
30
31
    /**
32
     * The command arguments.
33
     *
34
     * @return array
35
     */
36
    protected function getArguments()
37
    {
38
        return [
39
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
40
        ];
41
    }
42
43
    protected function getOptions()
44
    {
45
        return [
46
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the file already exists.'],
47
        ];
48
    }
49
50
    /**
51
     * Get template contents.
52
     *
53
     * @return string
54
     */
55
    protected function getTemplateContents()
56
    {
57
        $domain = $this->laravel['domains']->findOrFail($this->getDomainName());
58
59
        return (new Stub('/route-provider.stub', [
60
            'NAMESPACE'            => $this->getClassNamespace($domain),
61
            'CLASS'                => $this->getFileName(),
62
            'DOMAIN_NAMESPACE'     => $this->laravel['domains']->config('namespace'),
63
            'DOMAIN'               => $this->getDomainName(),
64
            'CONTROLLER_NAMESPACE' => $this->getControllerNameSpace(),
65
            'WEB_ROUTES_PATH'      => $this->getWebRoutesPath(),
66
            'API_ROUTES_PATH'      => $this->getApiRoutesPath(),
67
            'LOWER_NAME'           => $domain->getLowerName(),
68
        ]))->render();
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    private function getFileName()
75
    {
76
        return 'RouteServiceProvider';
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
        $generatorPath = GenerateConfigReader::read('provider');
89
90
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    protected function getWebRoutesPath()
97
    {
98
        return '/' . $this->laravel['domains']->config('stubs.files.routes/web', 'Routes/web.php');
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    protected function getApiRoutesPath()
105
    {
106
        return '/' . $this->laravel['domains']->config('stubs.files.routes/api', 'Routes/api.php');
107
    }
108
109
    public function getDefaultNamespace(): string
110
    {
111
        $domain = $this->laravel['domains'];
112
113
        return $domain->config('paths.generator.provider.namespace') ?: $domain->config('paths.generator.provider.path', 'Providers');
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    private function getControllerNameSpace(): string
120
    {
121
        $domain = $this->laravel['domains'];
122
123
        return str_replace('/', '\\', $domain->config('paths.generator.controller.namespace') ?: $domain->config('paths.generator.controller.path', 'Controller'));
124
    }
125
}
126