Completed
Push — 2.0 ( 14c778...b5ef61 )
by Marco
11:18
created

Runner   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B run() 0 44 4
1
<?php namespace Comodojo\Extender\Tasks;
2
3
use \Comodojo\Dispatcher\Components\Configuration;
4
use \Psr\Log\LoggerInterface;
5
6
/**
7
 * Job runner
8
 *
9
 * @package     Comodojo extender
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 * 
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
class Runner {
30
    
31
    protected $configuration;
32
    
33
    protected $logger;
34
    
35
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
36
        
37
        // init components
38
        $this->configuration = $configuration;
39
        
40
        $this->logger = $logger;
41
        
42
    }
43
    
44
    public function run($id, $task, $class, $timestamp = null, $name = null) {
45
        
46
        $this->logger->info("Starting task $task ($id) ");
47
48
        try {
49
50
            // create a task instance
51
52
            $thetask = new $class(
53
                $this->configuration,
54
                $this->logger,
55
                $name,
56
                $timestamp,
57
                $id,
58
                $parameters
0 ignored issues
show
Bug introduced by
The variable $parameters does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
59
            );
60
61
            // get the task pid (we are in singlethread mode)
62
63
            $pid = $thetask->pid;
64
65
            // run task
66
67
            $result = $thetask->start();
68
        
69
        } catch (TaskException $te) {
0 ignored issues
show
Bug introduced by
The class Comodojo\Extender\Tasks\TaskException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
70
71
            $this->logger->notice("Job ".$job['name']."(".$job['id'].") ends with error");
0 ignored issues
show
Bug introduced by
The variable $job does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
72
        
73
            return array($pid, $name, false, $start_timestamp, $te->getEndTimestamp(), $te->getMessage(), $id, $te->getWorklogId());
0 ignored issues
show
Bug introduced by
The variable $pid does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $start_timestamp does not exist. Did you mean $timestamp?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
74
        
75
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Comodojo\Extender\Tasks\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
76
77
            $this->logger->notice("Job ".$job['name']."(".$job['id'].") ends with error");
78
        
79
            return array($pid, $name, false, $start_timestamp, null, $e->getMessage(), $id, null);
0 ignored issues
show
Bug introduced by
The variable $start_timestamp does not exist. Did you mean $timestamp?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
80
        
81
        }
82
83
        $this->logger->notice("Job ".$job['name']."(".$job['id'].") ends with ".($result["success"] ? "success" : "failure"));
84
85
        return array($pid, $name, $result["success"], $start_timestamp, $result["timestamp"], $result["result"], $id, $result["worklogid"]);
0 ignored issues
show
Bug introduced by
The variable $start_timestamp does not exist. Did you mean $timestamp?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
86
        
87
    }
88
    
89
}