Completed
Push — master ( f11a34...f2f137 )
by Christian
02:47
created

UpgradeTask::isDryRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 Composer\Command\UpdateCommand;
24
use Composer\Factory;
25
use Composer\Installer\InstallerEvent;
26
use Composer\Installer\InstallerEvents;
27
use Symfony\Component\Console\Input\ArrayInput;
28
use Tenside\Core\Composer\Installer\InstallationManager;
29
use Tenside\Core\Util\JsonFile;
30
use Tenside\Core\Util\RuntimeHelper;
31
32
/**
33
 * This class holds the information for an upgrade of some or all packages (composer update).
34
 */
35
class UpgradeTask extends AbstractComposerCommandTask
36
{
37
    /**
38
     * The packages to upgrade.
39
     */
40
    const SETTING_PACKAGES = 'packages';
41
42
    /**
43
     * The home path of tenside.
44
     */
45
    const SETTING_HOME = 'home';
46
47
    /**
48
     * The data directory of tenside.
49
     */
50
    const SETTING_DATA_DIR = 'data-dir';
51
52
    /**
53
     * The dry-run flag
54
     */
55
    const SETTING_DRY_RUN = 'dry-run';
56
57
    /**
58
     * Retrieve the names of the packages to upgrade or null if none.
59
     *
60
     * @return string[]|null
0 ignored issues
show
Documentation introduced by
Should the return type not be array|string|integer|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
61
     */
62
    public function getPackages()
63
    {
64
        return $this->file->get(self::SETTING_PACKAGES);
65
    }
66
67
    /**
68
     * Check if the upgrade is selective or for all packages.
69
     *
70
     * @return bool
71
     */
72
    public function isSelectiveUpgrade()
73
    {
74
        return (null !== $this->getPackages());
75
    }
76
77
    /**
78
     * Retrieve the home path of tenside.
79
     *
80
     * @return string
81
     */
82
    public function getHome()
83
    {
84
        return (string) $this->file->get(self::SETTING_HOME);
85
    }
86
87
    /**
88
     * Retrieve the data path of tenside.
89
     *
90
     * @return string
91
     */
92
    public function getDataDir()
93
    {
94
        return (string) $this->file->get(self::SETTING_DATA_DIR);
95
    }
96
97
    /**
98
     * Check if the upgrade is a dry-run.
99
     *
100
     * @return bool
101
     */
102
    public function isDryRun()
103
    {
104
        return (bool) $this->file->get(self::SETTING_DRY_RUN);
105
    }
106
107
    /**
108
     * Returns 'upgrade'.
109
     *
110
     * {@inheritdoc}
111
     */
112
    public function getType()
113
    {
114
        return 'upgrade';
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120
    protected function prepareCommand()
121
    {
122
        RuntimeHelper::setupHome($this->getHome());
123
124
        $command = new UpdateCommand();
125
        $command->setComposer(Factory::create($this->getIO()));
126
127
        if ($this->isDryRun()) {
128
            $pendingUpgrades     = $this->getDataDir() . DIRECTORY_SEPARATOR . 'upgrades.json';
129
            $installationManager = new InstallationManager(new JsonFile($pendingUpgrades, null));
130
            $command->getComposer()->setInstallationManager($installationManager);
131
132
            $command->getComposer()->getEventDispatcher()->addListener(
133
                InstallerEvents::PRE_DEPENDENCIES_SOLVING,
134
                function (InstallerEvent $event) use ($installationManager) {
135
                    $installationManager->setPool($event->getPool());
136
                }
137
            );
138
        }
139
140
        return $command;
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    protected function prepareInput()
147
    {
148
        $arguments = [];
149
150
        if ($this->isSelectiveUpgrade()) {
151
            $arguments['packages'] = $this->getPackages();
152
        }
153
154
        if ($this->isDryRun()) {
155
            $arguments['--dry-run'] = true;
156
        }
157
158
        $input = new ArrayInput($arguments);
159
        $input->setInteractive(false);
160
161
        return $input;
162
    }
163
}
164