DaemoniseCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 1
cbo 4
dl 0
loc 87
ccs 0
cts 38
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 6 1
C execute() 0 34 7
A logCallback() 0 4 1
1
<?php
2
/**
3
 * PHPCI - Continuous Integration for PHP.
4
 *
5
 * @copyright    Copyright 2014, Block 8 Limited.
6
 * @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
7
 *
8
 * @link         https://www.phptesting.org/
9
 */
10
11
namespace PHPCI\Command;
12
13
use Monolog\Logger;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\ArgvInput;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Daemon that loops and call the run-command.
21
 *
22
 * @author       Gabriel Baker <[email protected]>
23
 */
24
class DaemoniseCommand extends Command
25
{
26
    /**
27
     * @var Logger
28
     */
29
    protected $logger;
30
31
    /**
32
     * @var OutputInterface
33
     */
34
    protected $output;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $run;
40
41
    /**
42
     * @var int
43
     */
44
    protected $sleep;
45
46
    /**
47
     * @param \Monolog\Logger $logger
48
     * @param string          $name
49
     */
50
    public function __construct(Logger $logger, $name = null)
51
    {
52
        parent::__construct($name);
53
        $this->logger = $logger;
54
    }
55
56
    protected function configure()
57
    {
58
        $this
59
            ->setName('phpci:daemonise')
60
            ->setDescription('Starts the daemon to run commands.');
61
    }
62
63
    /**
64
     * Loops through running.
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        $cmd = "echo %s > '%s/daemon/daemon.pid'";
69
        $command = sprintf($cmd, getmypid(), PHPCI_DIR);
70
        exec($command);
71
72
        $this->output = $output;
73
        $this->run = true;
74
        $this->sleep = 0;
75
        $runner = new RunCommand($this->logger);
76
        $runner->setMaxBuilds(1);
77
        $runner->setDaemon(true);
78
79
        $emptyInput = new ArgvInput(array());
80
81
        while ($this->run) {
82
            $buildCount = 0;
83
84
            try {
85
                $buildCount = $runner->run($emptyInput, $output);
86
            } catch (\Exception $e) {
87
                $output->writeln('<error>Exception: '.$e->getMessage().'</error>');
88
                $output->writeln('<error>Line: '.$e->getLine().' - File: '.$e->getFile().'</error>');
89
            }
90
91
            if (0 == $buildCount && $this->sleep < 15) {
92
                ++$this->sleep;
93
            } elseif (1 < $this->sleep) {
94
                --$this->sleep;
95
            }
96
            echo '.'.(0 === $buildCount ? '' : 'build');
97
            sleep($this->sleep);
98
        }
99
    }
100
101
    /**
102
     * Called when log entries are made in Builder / the plugins.
103
     *
104
     * @see \PHPCI\Builder::log()
105
     */
106
    public function logCallback($log)
107
    {
108
        $this->output->writeln($log);
109
    }
110
}
111