GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 11c0a1...856a6b )
by Jacky
34s
created

TaskManager::createTaskFromData()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 8
nop 1
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Asylamba\Classes\Task;
4
5
use Asylamba\Classes\DependencyInjection\Container;
6
7
use Asylamba\Classes\Process\Process;
8
9
class TaskManager
10
{
11
	/** @var Container **/
12
	protected $container;
13
    
14
	/**
15
	 * @param Container $container
16
	 */
17
    public function __construct(Container $container)
18
    {
19
		$this->container = $container;
20
    }
21
	
22
	/**
23
	 * @param array $data
24
	 * @return Task
25
	 */
26
	public function createTaskFromData($data)
27
	{
28
		switch ($data['type']) {
29
			case Task::TYPE_TECHNICAL:
30
				$task = $this->createTechnicalTask($data['manager'], $data['method'], $data['id']);
31
				break;
32
			case Task::TYPE_REALTIME:
33
				$task = $this->createRealTimeTask($data['manager'], $data['method'], $data['object_id'], $data['date'], $data['id'], ($data['context'] ?? null));
34
				break;
35
			case Task::TYPE_CYCLIC:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
36
				$task = $this->createCyclicTask($data['manager'], $data['method'], $data['id']);
37
				break;
38
		}
39
		if (isset($data['estimated_time'])) {
40
			$task->setEstimatedTime($data['estimated_time']);
0 ignored issues
show
Bug introduced by
The variable $task 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...
41
		}
42
		return $task;
43
	}
44
    
45
    /**
46
     * @param string $manager
47
     * @param string $method
48
     * @param int $objectId
49
     * @param string $date
50
	 * @param int $id
51
	 * @param array $context
52
     * @return Task
53
     */
54
    public function createRealTimeTask($manager, $method, $objectId, $date, $id = null, $context = null)
55
    {
56
        return
57
            (new RealTimeTask())
58
            ->setId((($id !== null) ? $id : $this->generateId()))
59
            ->setManager($manager)
60
            ->setMethod($method)
61
            ->setObjectId($objectId)
62
            ->setDate($date)
63
			->setContext($context)
64
        ;
65
    }
66
    
67
    /**
68
     * @param string $manager
69
     * @param string $method
70
     * @return Task
71
     */
72
    public function createCyclicTask($manager, $method, $id = null)
73
    {
74
        return
75
            (new CyclicTask())
76
            ->setId((($id !== null) ? $id : $this->generateId()))
77
            ->setManager($manager)
78
            ->setMethod($method)
79
        ;
80
    }
81
    
82
    /**
83
     * @param string $manager
84
     * @param string $method
85
     * @return Task
86
     */
87
    public function createTechnicalTask($manager, $method, $id = null)
88
    {
89
        return
90
            (new TechnicalTask())
91
            ->setId((($id !== null) ? $id : $this->generateId()))
92
            ->setManager($manager)
93
            ->setMethod($method)
94
        ;
95
    }
96
    
97
	/**
98
	 * @param \Asylamba\Classes\Task\Task $task
99
	 * @return array
100
	 */
101
    public function perform(Task $task)
102
    {
103
		// Get the manager from the container and then execute the given method with its arguments
104
		call_user_func_array(
105
			[$this->container->get($task->getManager()), $task->getMethod()],
106
			($task instanceof RealTimeTask) ? [$task->getObjectId()] : []
107
		);
108
		return ['success' => true, 'task' => $task];
109
    }
110
	
111
	/**
112
	 * @param Process $process
113
	 * @param array $data
114
	 */
115
	public function validateTask(Process $process, $data)
116
	{
117
		$task = $this->createTaskFromData($data['task']);
118
		
119
		if (!isset($data['time'])) {
120
			$data['time'] = 0.0;
121
		}
122
		$task->setTime((float) $data['time']);
123
		
124
		$process->removeTask($task);
125
		if ($task instanceof RealTimeTask && $task->getContext() !== null) {
126
			$process->removeContext($task->getContext());
127
		}
128
		
129
		if ($data['success'] === true) {
130
			$this->container->get('load_balancer')->storeStats($task);
131
		}
132
	}
133
    
134
    public function generateId()
135
    {
136
        return uniqid('process_');
137
    }
138
}