RunCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 14.47 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 17
Bugs 1 Features 7
Metric Value
wmc 9
c 17
b 1
f 7
lcom 1
cbo 12
dl 11
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 11 11 1
B execute() 0 55 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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