RunSingle   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 86
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A execute() 0 29 3
1
<?php
2
/**
3
 * Wrapper script to run a command not more than once at a time across multiple instances.
4
 *
5
 * @author    Matthias Gisder <[email protected]>
6
 * @copyright 2014 inGenerator Ltd
7
 * @licence   BSD
8
 */
9
10
namespace Ingenerator\RunSingle;
11
12
use Psr\Log\LoggerInterface;
13
14
class RunSingle
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $config_file = 'run_single_config.php';
20
21
    /**
22
     * @var \Ingenerator\RunSingle\LockDriver
23
     */
24
    protected $driver;
25
26
    /**
27
     * @var \Ingenerator\RunSingle\CommandRunner $runner
28
     */
29
    protected $runner;
30
31
    /**
32
     * @var LoggerInterface
33
     */
34
    protected $logger;
35
36
    /**
37
     * @var LockHolder
38
     */
39
    protected $lock_holder;
40
41
    /**
42
     * @param LockDriver       $driver
43
     * @param CommandRunner    $runner
44
     * @param LoggerInterface  $logger
45
     * @param LockHolder $lock_holder
46
     */
47
    public function __construct(
48
        LockDriver $driver,
49
        CommandRunner $runner,
50
        LoggerInterface $logger,
51
        LockHolder $lock_holder
52
    ) {
53
        $this->driver      = $driver;
54
        $this->runner      = $runner;
55
        $this->logger      = $logger;
56
        $this->lock_holder = $lock_holder;
57
//        //print_r($lock_holder->get_lock_holder(), 1);
58
//        echo $lock_holder->get_lock_holder();
59
    }
60
61
    /**
62
     * @param string $task_name
63
     * @param string $command
64
     * @param string $timeout
65
     * @param string $garbage_collect
66
     *
67
     * @return integer
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69
    public function execute($task_name, $command, $timeout, $garbage_collect)
70
    {
71
        if ($garbage_collect === TRUE) {
72
            $this->logger->info('garbage collecting for task '.$task_name);
73
            $this->driver->garbage_collect($task_name);
74
        }
75
76
        $this->logger->info('lock_holder id is '.$this->lock_holder->get_lock_holder());
77
        $this->logger->info('trying to get lock for task '.$task_name);
78
        $lock_id = $this->driver->get_lock($task_name, $timeout, $this->lock_holder->get_lock_holder());
79
        if ($lock_id !== FALSE) {
80
            $this->logger->info('executing task '.$task_name.' ...');
81
            $this->logger->info('<command output>');
82
83
            $start_time = \time();
84
            $exit_code  = $this->runner->execute($command);
85
            $end_time   = \time();
86
87
            $elapsed_time = $end_time - $start_time;
88
            $this->logger->info('</command output>');
89
            $this->logger->info('finished '.$task_name.' after '.$elapsed_time.' seconds with exit code '.$exit_code);
90
91
            $this->driver->release_lock($task_name, $lock_id);
0 ignored issues
show
Bug introduced by
It seems like $lock_id defined by $this->driver->get_lock(...der->get_lock_holder()) on line 78 can also be of type boolean; however, Ingenerator\RunSingle\LockDriver::release_lock() does only seem to accept integer, 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...
92
            return $exit_code;
93
        }
94
95
        $this->logger->info('no lock available for '.$task_name);
96
        return 0;
97
    }
98
99
}
100