Completed
Push — master ( c3b4b2...02e296 )
by Christian
02:46
created

AbstractTaskFactory::createInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
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 provides instantiation of composer command tasks.
27
 */
28
class AbstractTaskFactory implements TaskFactoryInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * This implementation returns true as soon as a protected method named: "create<TaskTypeName>" exists.
34
     */
35
    public function isTypeSupported($taskType)
36
    {
37
        return method_exists($this, $this->methodNameFromType($taskType));
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * This implementation will call the protected method named: "create<TaskTypeName>" if it exists.
44
     *
45
     * @throws \InvalidArgumentException For unsupported task types.
46
     */
47
    public function createInstance($taskType, JsonArray $metaData)
48
    {
49
        $methodName = $this->methodNameFromType($taskType);
50
        if ($this->isTypeSupported($taskType)) {
51
            return call_user_func([$this, $methodName], $metaData);
52
        }
53
54
        throw new \InvalidArgumentException('Do not know how to create task ' . $taskType);
55
    }
56
57
    /**
58
     * Create a method name from a task type name.
59
     *
60
     * @param string $type The type to create the method name for.
61
     *
62
     * @return string
63
     */
64
    protected function methodNameFromType($type)
65
    {
66
        return 'create' . implode('', array_map('ucfirst', explode('-', $type)));
67
    }
68
}
69