Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

PushCategoryToGroup   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 3 Features 1
Metric Value
wmc 7
c 4
b 3
f 1
lcom 1
cbo 3
dl 0
loc 106
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fire() 0 19 3
A getArgumentsConsole() 0 12 1
A getArguments() 0 6 1
A getOptions() 0 6 1
1
<?php
2
3
namespace Fenos\Notifynder\Artisan;
4
5
use Fenos\Notifynder\Contracts\NotifynderGroup;
6
use Fenos\Notifynder\Parsers\ArtisanOptionsParser;
7
use Illuminate\Console\Command;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputArgument;
10
11
class PushCategoryToGroup extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'notifynder:push:category';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Associate the categories to a group';
26
27
    /**
28
     * @var NotifynderGroup
29
     */
30
    private $notifynderGroup;
31
32
    /**
33
     * @var ArtisanOptionsParser
34
     */
35
    private $artisanOptionsParser;
36
37
    /**
38
     * Create a new command instance.
39
     *
40
     * @param  NotifynderGroup                              $notifynderGroup
41
     * @param  ArtisanOptionsParser                         $artisanOptionsParser
42
     * @return \Fenos\Notifynder\Artisan\PushCategoryToGroup
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
43
     */
44
    public function __construct(NotifynderGroup $notifynderGroup,
45
        ArtisanOptionsParser $artisanOptionsParser)
46
    {
47
        parent::__construct();
48
        $this->notifynderGroup = $notifynderGroup;
49
        $this->artisanOptionsParser = $artisanOptionsParser;
50
    }
51
52
    /**
53
     * Execute the console command.
54
     *
55
     * @return mixed
56
     */
57
    public function fire()
58
    {
59
        $arguments = $this->getArgumentsConsole();
60
61
        $categoryGroup = array_shift($arguments);
62
        $arguments = $arguments[0];
63
        $categories = explode(',', $arguments);
64
65
        $groupCategories = $this->notifynderGroup
66
            ->addMultipleCategoriesToGroup($categoryGroup, $categories);
0 ignored issues
show
Unused Code introduced by
The call to NotifynderGroup::addMultipleCategoriesToGroup() has too many arguments starting with $categoryGroup.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
67
68
        if ($groupCategories) {
69
            foreach ($groupCategories->categories as $category) {
70
                $this->info("Category {$category->name} has been associated to the group {$groupCategories->name}");
71
            }
72
        } else {
73
            $this->error('The name must be a string with dots as namespaces');
74
        }
75
    }
76
77
    /**
78
     * @return array|string
79
     */
80
    public function getArgumentsConsole()
81
    {
82
        $names = $this->argument('name');
83
84
        $categories = $this->option('categories');
85
86
        $categories = $this->artisanOptionsParser->parse($categories);
0 ignored issues
show
Bug introduced by
It seems like $categories defined by $this->artisanOptionsParser->parse($categories) on line 86 can also be of type array; however, Fenos\Notifynder\Parsers...nOptionsParser::parse() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
88
        array_unshift($categories, $names);
89
90
        return $categories;
91
    }
92
93
    /**
94
     * Get the console command arguments.
95
     *
96
     * @return array
97
     */
98
    protected function getArguments()
99
    {
100
        return [
101
            ['name', InputArgument::REQUIRED, 'user.post.add'],
102
        ];
103
    }
104
105
    /**
106
     * Get the console command options.
107
     *
108
     * @return array
109
     */
110
    protected function getOptions()
111
    {
112
        return [
113
            ['categories', null, InputOption::VALUE_OPTIONAL, 'notifynder.name', []],
114
        ];
115
    }
116
}
117