Passed
Pull Request — master (#23)
by Tom
04:47 queued 02:05
created

MakePageCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Filesystem\Filesystem;
0 ignored issues
show
introduced by
You should change the following
<fg=green>+use Illuminate\Contracts\Config\Repository as ConfigContract;
</><fg=green>+use Illuminate\Contracts\Filesystem\Factory as FilesystemManagerContract;
</>
Loading history...
7
use Illuminate\Support\Str;
8
use Spatie\Sheets\Facades\Sheets;
9
use Spatie\Sheets\Repositories\FilesystemRepository;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Yaml\Yaml;
12
use Illuminate\Contracts\Config\Repository as ConfigContract;
0 ignored issues
show
introduced by
You should change the following
<fg=red>-use Illuminate\Contracts\Config\Repository as ConfigContract;
</><fg=red>-use Illuminate\Contracts\Filesystem\Factory as FilesystemManagerContract;
</>
Loading history...
13
use Illuminate\Contracts\Filesystem\Factory as FilesystemManagerContract;
14
15
class MakePageCommand extends GeneratorCommand
16
{
17
    protected $name = 'make:page';
0 ignored issues
show
introduced by
Property \App\Console\Commands\MakePageCommand::$name does not have @var annotation.
Loading history...
18
19
    protected $description = 'Create a new page for stancy package.';
0 ignored issues
show
introduced by
Property \App\Console\Commands\MakePageCommand::$description does not have @var annotation.
Loading history...
20
21
    /** @var ConfigContract */
22
    protected $config;
23
24
    /** @var FilesystemManagerContract */
25
    protected $filesystemManager;
26
27
    public function __construct(Filesystem $files, ConfigContract $config, FilesystemManagerContract $filesystemManager)
28
    {
29
        parent::__construct($files);
30
31
        $this->config = $config;
32
        $this->filesystemManager = $filesystemManager;
33
    }
34
35
    public function handle()
0 ignored issues
show
introduced by
Method \App\Console\Commands\MakePageCommand::handle() does not have return type hint nor @return annotation for its return value.
Loading history...
36
    {
37
        $this->createSheet();
38
39
        return parent::handle();
40
    }
41
42
    protected function createSheet(): void
0 ignored issues
show
introduced by
You should change the following
<fg=green>+ public function getOptions()
</><fg=green>+
</><fg=green>+ return [
</><fg=green>+ ['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'],
</><fg=green>+ ];
</><fg=green>+
</><fg=green>+
</>
Loading history...
43
    {
44
        $name = $this->getNameInput();
45
        $collection = $this->option('collection');
46
47
        if (! $collection) {
48
            return;
49
        }
50
51
        $repository = Sheets::collection($collection);
52
53
        if (! $repository instanceof FilesystemRepository) {
54
            $this->warn('can not create a sheet if collection is not instance of `'.FilesystemRepository::class.'`');
55
56
            return;
57
        }
58
59
        $extension = $this->config->get('sheets.collections.'.$collection.'.extension', 'md');
0 ignored issues
show
Bug introduced by
Are you sure $collection of type string|string[]|true can be used in concatenation? ( Ignorable by Annotation )

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

59
        $extension = $this->config->get('sheets.collections.'./** @scrutinizer ignore-type */ $collection.'.extension', 'md');
Loading history...
60
        $disk = $this->config->get('sheets.collections.'.$collection.'.disk', $collection);
61
        $filesystem = $this->filesystemManager->disk($disk);
62
63
        $filename = Str::kebab(class_basename($name)).'.'.$extension;
64
65
        if (
66
            (! $this->hasOption('force') || ! $this->option('force'))
67
            && $filesystem->exists($filename)
68
        ) {
69
            $this->error('the sheet `'.$filename.'` already exists');
70
71
            return;
72
        }
73
74
        $filesystem->put($filename, $this->getDefaultSheetContent($extension));
75
    }
76
77
    protected function getDefaultSheetContent(string $extension): string
78
    {
79
        if (! in_array($extension, ['md', 'json', 'yaml', 'yml'])) {
80
            return '';
81
        }
82
83
        $data = [
84
            '_pageData' => '\\'.$this->qualifyClass($this->getNameInput()),
85
        ];
86
87
        if ($extension == 'json') {
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
88
            return json_encode($data);
89
        }
90
91
        if (in_array($extension, ['yaml', 'yml'])) {
92
            return Yaml::dump($data);
93
        }
94
95
        return implode(PHP_EOL, [
96
            '---',
97
            trim(Yaml::dump(array_merge($data, ['_view' => null]))),
98
            '---',
99
            '',
100
        ]);
101
    }
102
103
    protected function getStub(): string
104
    {
105
        return __DIR__.'/../../../../stancy/resources/stubs/page.stub';
106
    }
107
108
    protected function getDefaultNamespace($rootNamespace): string
0 ignored issues
show
introduced by
Method \App\Console\Commands\MakePageCommand::getDefaultNamespace() does not have parameter type hint nor @param annotation for its parameter $rootNamespace.
Loading history...
109
    {
110
        return rtrim($rootNamespace, '\\').'\Pages';
111
    }
112
113
    protected function replaceClass($stub, $name): string
0 ignored issues
show
introduced by
Possible useless method overriding detected
Loading history...
introduced by
Method \App\Console\Commands\MakePageCommand::replaceClass() does not have parameter type hint nor @param annotation for its parameter $stub.
Loading history...
introduced by
Method \App\Console\Commands\MakePageCommand::replaceClass() does not have parameter type hint nor @param annotation for its parameter $name.
Loading history...
114
    {
115
        return parent::replaceClass($stub, $name);
116
    }
0 ignored issues
show
introduced by
You should change the following
<fg=red>- }
</><fg=red>-
</><fg=red>- public function getOptions()
</><fg=red>- {
</><fg=red>- return [
</><fg=red>- ['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'],
</><fg=red>- ];
</>
Loading history...
117
118
    public function getOptions()
0 ignored issues
show
introduced by
Method \App\Console\Commands\MakePageCommand::getOptions() does not have return type hint nor @return annotation for its return value.
Loading history...
119
    {
120
        return [
121
            ['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'],
122
        ];
123
    }
124
}
125