Completed
Push — master ( a6e986...36a29e )
by Andreas
10:44
created

ComposerTaskFactory::ensureHomePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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\Composer;
22
23
use Tenside\Core\Task\TaskFactoryInterface;
24
use Tenside\Core\Util\JsonArray;
25
use Tenside\CoreBundle\Util\HomePathDeterminator;
26
27
/**
28
 * This class provides loading of the tenside core configuration.
29
 */
30
class ComposerTaskFactory implements TaskFactoryInterface
31
{
32
    /**
33
     * The home path.
34
     *
35
     * @var HomePathDeterminator
36
     */
37
    private $home;
38
39
    /**
40
     * Create a new instance.
41
     *
42
     * @param HomePathDeterminator $home The home path to use.
43
     */
44
    public function __construct(HomePathDeterminator $home)
45
    {
46
        $this->home = $home;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function isTypeSupported($taskType)
53
    {
54
        return in_array($taskType, ['install', 'upgrade', 'require-package', 'remove-package']);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @throws \InvalidArgumentException For unsupported task types.
61
     */
62
    public function createInstance($taskType, JsonArray $metaData)
63
    {
64
        switch ($taskType) {
65
            case 'install':
66
                return new InstallTask($metaData);
67
            case 'upgrade':
68
                $this->ensureHomePath($metaData);
69
                if (!$metaData->has(UpgradeTask::SETTING_DATA_DIR)) {
70
                    $metaData->set(UpgradeTask::SETTING_DATA_DIR, $this->home->tensideDataDir());
71
                }
72
                return new UpgradeTask($metaData);
73
            case 'require-package':
74
                $this->ensureHomePath($metaData);
75
                return new RequirePackageTask($metaData);
76
            case 'remove-package':
77
                $this->ensureHomePath($metaData);
78
                return new RemovePackageTask($metaData);
79
            default:
80
        }
81
82
        throw new \InvalidArgumentException('Do not know how to create task.');
83
    }
84
85
    /**
86
     * Ensure the home path has been set in the passed meta data.
87
     *
88
     * @param JsonArray $metaData The meta data to examine.
89
     *
90
     * @return void
91
     */
92
    private function ensureHomePath(JsonArray $metaData)
93
    {
94
        if ($metaData->has(RequirePackageTask::SETTING_HOME)) {
95
            return;
96
        }
97
        $metaData->set(RequirePackageTask::SETTING_HOME, $this->home->homeDir());
98
    }
99
}
100