ModuleManagementTaskFactory::create()   C
last analyzed

Complexity

Conditions 13
Paths 12

Size

Total Lines 45
Code Lines 28

Duplication

Lines 18
Ratio 40 %

Code Coverage

Tests 21
CRAP Score 16.5492

Importance

Changes 0
Metric Value
dl 18
loc 45
ccs 21
cts 29
cp 0.7241
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 28
nc 12
nop 2
crap 16.5492

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Samurai\Module\Task\Factory;
3
4
use Pimple\Container;
5
use Samurai\Module\ModuleCommand;
6
use Samurai\Module\Task\Enabling;
7
use Samurai\Module\Task\Installing;
8
use Samurai\Module\Task\Listing;
9
use Samurai\Module\Task\Removing;
10
use Samurai\Module\Task\Running;
11
use Samurai\Module\Task\Saving;
12
use Samurai\Module\Task\Updating;
13
use Samurai\Task\ITask;
14
use SimilarText\Finder;
15
use Symfony\Component\Console\Input\InputInterface;
16
17
/**
18
 * Class TaskFactory
19
 * @package Samurai\Alias\Task\Factory
20
 * @author Raphaël Lefebvre <[email protected]>
21
 */
22
class ModuleManagementTaskFactory
23
{
24
    /**
25
     * @param InputInterface $input
26
     * @param Container $services
27
     * @return ITask
28
     */
29 10
    public static function create(InputInterface $input, Container $services)
30
    {
31
32 10
        if(!$input->getArgument('action')){
33
            throw new \InvalidArgumentException(sprintf('An action param is required: %s', json_encode(ModuleCommand::$actions)));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
34
        }
35
36 10
        if($input->getArgument('action') === 'list'){
37 1
            return new Listing($services);
38
        }
39 9
        if($input->getArgument('action') === 'install'){
40 2
            return new Installing($services);
41
        }
42 7
        if($input->getArgument('action') === 'update'){
43 2
            return new Updating($services);
44
        }
45 5 View Code Duplication
        if($input->getArgument('action') === 'rm' || $input->getArgument('action') === 'remove'){
46 2
            if(!$input->getArgument('name')){
47
                throw new \InvalidArgumentException('name param is mandatory for this action');
48
            }
49 2
            return new Removing($services);
50
        }
51 3 View Code Duplication
        if($input->getArgument('action') === 'enable'){
52
            if(!$input->getArgument('name')){
53
                throw new \InvalidArgumentException('name param is mandatory for this action');
54
            }
55
            return new Enabling($services);
56
        }
57 3 View Code Duplication
        if($input->getArgument('action') === 'disable'){
58
            if(!$input->getArgument('name')){
59
                throw new \InvalidArgumentException('name param is mandatory for this action');
60
            }
61
            return new Enabling($services);
62
        }
63 3
        if($input->getArgument('action') === 'run'){
64 2
            return new Running($services);
65
        }
66
67 1
        $textFinder = new Finder($input->getArgument('action'), ModuleCommand::$actions);
68 1
        throw new \InvalidArgumentException(sprintf(
69 1
            'Action "%s" not supported. Did you mean "%s"?',
70 1
            $input->getArgument('action'),
71 1
            $textFinder->first()
72 1
        ));
73
    }
74
}
75