RunCommand::execute()   B
last analyzed

Complexity

Conditions 8
Paths 80

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 14
Bugs 0 Features 6
Metric Value
c 14
b 0
f 6
dl 0
loc 55
rs 7.4033
cc 8
eloc 31
nc 80
nop 2

How to fix   Long Method   

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
/*
3
 * This file is part of JoliCi.
4
*
5
* (c) Joel Wurtz <[email protected]>
6
*
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
9
*/
10
11
namespace Joli\JoliCi\Command;
12
13
use Joli\JoliCi\Container;
14
use Joli\JoliNotif\Notification;
15
use Joli\JoliNotif\NotifierFactory;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputOption;
21
22
class RunCommand extends Command
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $this->setName('run');
30
        $this->setDescription('Run tests on your project');
31
        $this->addOption('project-path', 'p', InputOption::VALUE_OPTIONAL, "Path where you project is (default to current directory)", ".");
32
        $this->addOption('keep', 'k', InputOption::VALUE_OPTIONAL, "Number of images / containers / directories per build to keep when cleaning at the end of run", 1);
33
        $this->addOption('no-cache', null, InputOption::VALUE_NONE, "Do not use cache of docker");
34
        $this->addOption('timeout', null, InputOption::VALUE_OPTIONAL, "Timeout for docker client in seconds (default to 5 minutes)", "300");
35
        $this->addOption('notify', null, InputOption::VALUE_NONE, "Show desktop notifications when a build is finished");
36
        $this->addArgument('cmd', InputArgument::OPTIONAL, "Override test command");
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $container  = new Container();
45
        $verbose    = (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity());
46
        $strategy   = $container->getChainStrategy();
47
        $executor   = $container->getExecutor(!$input->getOption('no-cache'), $verbose, $input->getOption('timeout'));
48
        $serviceManager = $container->getServiceManager($verbose);
49
        $notifier   = NotifierFactory::create();
50
51
        $output->writeln("<info>Creating builds...</info>");
52
53
        $jobs = $strategy->getJobs($input->getOption("project-path"));
54
55
        $output->writeln(sprintf("<info>%s builds created</info>", count($jobs)));
56
57
        $exitCode = 0;
58
59
        try {
60
            foreach ($jobs as $job) {
61
                $output->writeln(sprintf("\n<info>Running job %s</info>\n", $job->getDescription()));
62
63
                $serviceManager->start($job);
64
                $strategy->prepareJob($job);
65
66
                $success  = $executor->test($job, $input->getArgument('cmd'));
67
                $exitCode += $success == 0 ? 0 : 1;
68
69
                if ($input->getOption('notify')) {
70
                    $notification = new Notification();
71
                    $notification->setBody(sprintf('Test results for %s on %s', $container->getNaming()->getProjectName($input->getOption('project-path')), $job->getDescription()));
72
                    $notification->setTitle($success == 0 ? 'Tests passed' : 'Tests failed');
73
74
                    $notifier->send($notification);
75
                }
76
77
                $serviceManager->stop($job);
78
            }
79
        } catch (\Exception $e) {
80
            // Try stop last builds
81
            if (isset($job)) {
82
                $serviceManager->stop($job);
83
            }
84
            // We do not deal with exception (Console Component do it well),
85
            // we just catch it to allow cleaner to be runned even if one of the build failed hard
86
            // Simulation of a finally for php < 5.6 :-°
87
        }
88
89
        $container->getVacuum()->clean($input->getOption("project-path"), $input->getOption("keep"));
90
91
        if (isset($e)) {
92
            throw $e;
93
        }
94
95
        return $exitCode;
96
    }
97
}
98