Completed
Push — master ( c612e7...f3e1c8 )
by Christian
06:23
created

CompositeTaskFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 74
rs 10
c 1
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 9 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
    public function createInstance($taskType, JsonArray $metaData)
77
    {
78
        if (!($factory = $this->getFactoryForType($taskType)) instanceof TaskFactoryInterface) {
79
80
            throw new \InvalidArgumentException('Do not know how to create task.');
81
        }
82
83
        return $factory->createInstance($taskType, $metaData);
84
    }
85
86
    /**
87
     * Search the factory that can handle the type.
88
     *
89
     * @param string $taskType The task type to search.
90
     *
91
     * @return null|TaskFactoryInterface
92
     */
93
    private function getFactoryForType($taskType)
94
    {
95
        foreach ($this->factories as $factory) {
96
            if ($factory->isTypeSupported($taskType)) {
97
                return $factory;
98
            }
99
        }
100
101
        return null;
102
    }
103
}
104