TaskRunnerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 46
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 13 2
A getTasksConfig() 0 16 3
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Factory;
13
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Webcreate\Conveyor\Config\YamlConfig;
16
use Webcreate\Conveyor\IO\IOInterface;
17
use Webcreate\Conveyor\Task\TaskRunner;
18
19
class TaskRunnerFactory
20
{
21
    /**
22
     * @param  string                   $taskConfigPath path to the tasks configuration in the conveyor.yml config
23
     * @param  TaskFactory              $taskFactory
24
     * @param  YamlConfig               $config
25
     * @param  IOInterface              $io
26
     * @param  EventDispatcherInterface $dispatcher
27
     * @return TaskRunner
28
     */
29
    public static function get($taskConfigPath, TaskFactory $taskFactory, YamlConfig $config, IOInterface $io, EventDispatcherInterface $dispatcher = null)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
30
    {
31
        $_config = $config->getConfig();
32
33
        $taskRunner = new TaskRunner($io, $dispatcher);
34
35
        foreach ((array) self::getTasksConfig($_config, $taskConfigPath) as $t => $taskConfig) {
36
            $task = $taskFactory->get($taskConfig['type'], $taskConfig);
37
            $taskRunner->addTask($task);
0 ignored issues
show
Bug introduced by
It seems like $task defined by $taskFactory->get($taskC...g['type'], $taskConfig) on line 36 can be null; however, Webcreate\Conveyor\Task\TaskRunner::addTask() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
38
        }
39
40
        return $taskRunner;
41
    }
42
43
    /**
44
     * @param  array  $config
45
     * @param  string $taskConfigPath
46
     * @return array
47
     */
48
    protected static function getTasksConfig(array $config, $taskConfigPath)
49
    {
50
        $path = explode('.', $taskConfigPath);
51
52
        $currentConfig = $config;
53
54
        foreach ($path as $p => $_path) {
55
            if (!isset($currentConfig[$_path])) {
56
                return array();
57
            }
58
59
            $currentConfig = $currentConfig[$_path];
60
        }
61
62
        return $currentConfig;
63
    }
64
}
65