Passed
Push — master ( bdb2b6...42b5cd )
by Tom
01:58 queued 11s
created

MakePageCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
eloc 46
c 1
b 1
f 0
dl 0
loc 108
ccs 46
cts 46
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getStub() 0 3 1
A handle() 0 5 1
A getDefaultSheetContent() 0 23 4
A getDefaultNamespace() 0 3 1
A replaceClass() 0 3 1
A getOptions() 0 4 1
A createSheet() 0 33 6
1
<?php
2
3
namespace Astrotomic\Stancy\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Contracts\Config\Repository as ConfigContract;
7
use Illuminate\Contracts\Filesystem\Factory as FilesystemManagerContract;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Str;
10
use Spatie\Sheets\Facades\Sheets;
11
use Spatie\Sheets\Repositories\FilesystemRepository;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Yaml\Yaml;
14
15
class MakePageCommand extends GeneratorCommand
16
{
17
    protected $name = 'make:page';
0 ignored issues
show
introduced by Gummibeer
Property \Astrotomic\Stancy\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 Gummibeer
Property \Astrotomic\Stancy\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 7
    public function __construct(Filesystem $files, ConfigContract $config, FilesystemManagerContract $filesystemManager)
28
    {
29 7
        parent::__construct($files);
30
31 7
        $this->config = $config;
32 7
        $this->filesystemManager = $filesystemManager;
33 7
    }
34
35 7
    public function getOptions()
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Commands\MakePageCommand::getOptions() does not have return type hint nor @return annotation for its return value.
Loading history...
36
    {
37
        return [
38 7
            ['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'],
39
        ];
40
    }
41
42 7
    public function handle()
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Commands\MakePageCommand::handle() does not have return type hint nor @return annotation for its return value.
Loading history...
43
    {
44 7
        $this->createSheet();
45
46 7
        return parent::handle();
47
    }
48
49 7
    protected function createSheet(): void
50
    {
51 7
        $name = $this->getNameInput();
52 7
        $collection = $this->option('collection');
53
54 7
        if (! $collection) {
55 2
            return;
56
        }
57
58 5
        $repository = Sheets::collection($collection);
59
60 5
        if (! $repository instanceof FilesystemRepository) {
61
            $this->warn('can not create a sheet if collection is not instance of `'.FilesystemRepository::class.'`'); // @codeCoverageIgnore
62
63
            return; // @codeCoverageIgnore
64
        }
65
66 5
        $extension = $this->config->get('sheets.collections.'.$collection.'.extension', 'md');
0 ignored issues
show
Bug introduced by Gummibeer
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

66
        $extension = $this->config->get('sheets.collections.'./** @scrutinizer ignore-type */ $collection.'.extension', 'md');
Loading history...
67 5
        $disk = $this->config->get('sheets.collections.'.$collection.'.disk', $collection);
68 5
        $filesystem = $this->filesystemManager->disk($disk);
69
70 5
        $filename = Str::kebab(class_basename($name)).'.'.$extension;
71
72
        if (
73 5
            (! $this->hasOption('force') || ! $this->option('force'))
74 5
            && $filesystem->exists($filename)
75
        ) {
76 1
            $this->error('the sheet `'.$filename.'` already exists');
77
78 1
            return;
79
        }
80
81 5
        $filesystem->put($filename, $this->getDefaultSheetContent($extension));
82 5
    }
83
84 5
    protected function getDefaultSheetContent(string $extension): string
85
    {
86 5
        if (! in_array($extension, ['md', 'json', 'yaml', 'yml'])) {
87 1
            return '';
88
        }
89
90
        $data = [
91 4
            '_pageData' => '\\'.$this->qualifyClass($this->getNameInput()),
92
        ];
93
94 4
        if ($extension === 'json') {
95 1
            return json_encode($data);
96
        }
97
98 3
        if (in_array($extension, ['yaml', 'yml'])) {
99 1
            return Yaml::dump($data);
100
        }
101
102 2
        return implode(PHP_EOL, [
103 2
            '---',
104 2
            trim(Yaml::dump(array_merge($data, ['_view' => null]))),
105 2
            '---',
106 2
            '',
107
        ]);
108
    }
109
110 7
    protected function getStub(): string
111
    {
112 7
        return __DIR__.'/../../resources/stubs/page.stub';
113
    }
114
115 7
    protected function getDefaultNamespace($rootNamespace): string
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Commands\MakePageCommand::getDefaultNamespace() does not have parameter type hint nor @param annotation for its parameter $rootNamespace.
Loading history...
116
    {
117 7
        return rtrim($rootNamespace, '\\').'\Pages';
118
    }
119
120 7
    protected function replaceClass($stub, $name): string
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Commands\MakePageCommand::replaceClass() does not have parameter type hint nor @param annotation for its parameter $stub.
Loading history...
introduced by Gummibeer
Method \Astrotomic\Stancy\Commands\MakePageCommand::replaceClass() does not have parameter type hint nor @param annotation for its parameter $name.
Loading history...
121
    {
122 7
        return parent::replaceClass($stub, $name);
123
    }
124
}
125