PostMakeCommand   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 105
ccs 0
cts 60
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C fire() 0 47 14
A getArguments() 0 4 1
A getOptions() 0 9 1
1
<?php
2
3
use Flaviozantut\Storage\Posts\PostRepositoryInterface;
4
use Illuminate\Console\Command;
5
use Symfony\Component\Console\Input\InputOption;
6
7
class PostMakeCommand extends Command
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * The console command name.
11
     *
12
     * @var string
13
     */
14
    protected $name = 'post:make';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Create new Post file';
22
23
    /**
24
     * Create a new command instance.
25
     *
26
     * @return void
27
     */
28
    public function __construct(PostRepositoryInterface $post)
29
    {
30
        parent::__construct();
31
        $this->post = $post;
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return void
38
     */
39
    public function fire()
40
    {
41
        $options = $this->option();
42
        $newPost = [];
43
44
        //POST TYPE
45
        $options['type'] = strtolower($options['type']);
46
        if ($options['type'] and ($options['type'] == 'article' or $options['type'] == 'page')) {
47
            $newPost['type'] = $options['type'];
48
        } else {
49
            $newPost['type'] = $this->ask("What's the post type? draft/article/page");
50
            if ($newPost['type'] != 'page' or $newPost['type'] != 'article') {
51
                $newPost['type'] = 'draft';
52
            }
53
        }
54
        //COMMENTS
55
        $options['comments'] = strtolower($options['comments']);
56
        if ($options['comments'] and ($options['comments'] == 'y' or $options['comments'] == 'n')) {
57
            $newPost['comments'] = $options['comments'];
58
        } else {
59
            $newPost['comments'] = $this->ask('Enable comments? y/n');
60
            if ($newPost['comments'] == 'n') {
61
                $newPost['comments'] = false;
62
            } else {
63
                $newPost['comments'] = true;
64
            }
65
        }
66
        //title
67
        if ($options['title']) {
68
            $newPost['title'] = $options['title'];
69
        } else {
70
            while (true) {
71
                $newPost['title'] = $this->ask("What's the title of post?");
72
                if ($newPost['title']) {
73
                    break;
74
                }
75
            }
76
        }
77
78
        if ($options['post']) {
79
            $newPost['post'] = $options['post'];
80
        } else {
81
            $newPost['post'] = $this->ask("What's the content of post?");
82
        }
83
        $this->info($this->post->store($newPost));
84
        $this->call('cache:clear');
85
    }
86
87
    /**
88
     * Get the console command arguments.
89
     *
90
     * @return array
91
     */
92
    protected function getArguments()
93
    {
94
        return [];
95
    }
96
97
    /**
98
     * Get the console command options.
99
     *
100
     * @return array
101
     */
102
    protected function getOptions()
103
    {
104
        return [
105
            ['title', null, InputOption::VALUE_OPTIONAL, 'Title of post', null],
106
            ['type', null, InputOption::VALUE_OPTIONAL, 'article/page', null],
107
            ['comments', null, InputOption::VALUE_OPTIONAL, 'y/n', null],
108
            ['post', null, InputOption::VALUE_OPTIONAL, 'Content of post', null],
109
        ];
110
    }
111
}
112