TaskFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Config\Definition\ConfigurationInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Webcreate\Conveyor\Task\Task;
17
18
class TaskFactory
19
{
20
    /**
21
     * @var ContainerBuilder
22
     */
23
    protected $container;
24
25
    /**
26
     * @var string[]
27
     */
28
    protected $tasks = array();
29
30
    /**
31
     * @var string[]
32
     */
33
    protected $configurations = array();
34
35
    /**
36
     * @param ContainerBuilder $container
37
     */
38 3
    public function __construct(ContainerBuilder $container)
39
    {
40 3
        $this->container = $container;
41 3
    }
42
43
    /**
44
     * @param string      $serviceId
45
     * @param string      $alias
46
     * @param string|bool $configuration
47
     */
48 3
    public function addTask($serviceId, $alias, $configuration = false)
49
    {
50 3
        $this->tasks[$alias]          = $serviceId;
51 3
        $this->configurations[$alias] = $configuration;
52 3
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getTasks()
58
    {
59
        return $this->tasks;
60
    }
61
62
    /**
63
     * Returns task
64
     *
65
     * @param  string $alias   name of the task
66
     * @param  array  $options configuration options for the task
67
     * @return Task
68
     */
69
    public function get($alias, array $options = array())
70
    {
71
        $serviceId  = $this->tasks[$alias];
72
73
        $task = $this->container->get($serviceId);
74
        $task->setOptions($options);
75
76
        return $task;
77
    }
78
79
    /**
80
     * Returns task configuration
81
     *
82
     * @param  string                         $alias task name
83
     * @return ConfigurationInterface|boolean
84
     */
85
    public function configuration($alias)
86
    {
87
        $configurationClass = $this->configurations[$alias];
88
89
        if (false !== $configurationClass) {
90
            return new $configurationClass();
91
        }
92
93
        return false;
94
    }
95
}
96