CompositeTaskFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A add() 0 6 1
A isTypeSupported() 0 4 1
A createInstance() 0 8 2
A getFactoryForType() 0 10 3
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[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
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Task;
22
23
use Tenside\Core\Util\JsonArray;
24
25
/**
26
 * This class is a composite factory of several other task factories.
27
 *
28
 * When queried, it will try all contained factories and return as soon as any provides an result.
29
 */
30
class CompositeTaskFactory implements TaskFactoryInterface
31
{
32
    /**
33
     * The registered factories.
34
     *
35
     * @var TaskFactoryInterface[]
36
     */
37
    private $factories = [];
38
39
    /**
40
     * Create an instance containing the passed factories.
41
     *
42
     * @param TaskFactoryInterface[] $factories The factories to add.
43
     */
44
    public function __construct(array $factories = [])
45
    {
46
        foreach ($factories as $factory) {
47
            $this->add($factory);
48
        }
49
    }
50
51
    /**
52
     * Add the passed factory.
53
     *
54
     * @param TaskFactoryInterface $factory The factory to add.
55
     *
56
     * @return CompositeTaskFactory
57
     */
58
    public function add(TaskFactoryInterface $factory)
59
    {
60
        $this->factories[] = $factory;
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function isTypeSupported($taskType)
69
    {
70
        return ($this->getFactoryForType($taskType) instanceof TaskFactoryInterface);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     *
76
     * @throws \InvalidArgumentException For unsupported task types.
77
     */
78
    public function createInstance($taskType, JsonArray $metaData)
79
    {
80
        if (!($factory = $this->getFactoryForType($taskType)) instanceof TaskFactoryInterface) {
81
            throw new \InvalidArgumentException('Do not know how to create task.');
82
        }
83
84
        return $factory->createInstance($taskType, $metaData);
85
    }
86
87
    /**
88
     * Search the factory that can handle the type.
89
     *
90
     * @param string $taskType The task type to search.
91
     *
92
     * @return null|TaskFactoryInterface
93
     */
94
    private function getFactoryForType($taskType)
95
    {
96
        foreach ($this->factories as $factory) {
97
            if ($factory->isTypeSupported($taskType)) {
98
                return $factory;
99
            }
100
        }
101
102
        return null;
103
    }
104
}
105