InstallationManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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\Composer\Installer;
22
23
use Composer\DependencyResolver\Operation\InstallOperation;
24
use Composer\DependencyResolver\Operation\OperationInterface;
25
use Composer\DependencyResolver\Operation\UninstallOperation;
26
use Composer\DependencyResolver\Operation\UpdateOperation;
27
use Composer\DependencyResolver\Pool;
28
use Composer\DependencyResolver\Rule;
29
use Composer\Package\Dumper\ArrayDumper;
30
use Composer\Repository\RepositoryInterface;
31
use Tenside\Core\Util\JsonArray;
32
33
/**
34
 * This class wraps the real installation manager to be able to generate a log of package changes when upgrading.
35
 */
36
class InstallationManager extends \Composer\Installer\InstallationManager
37
{
38
    /**
39
     * The package information.
40
     *
41
     * @var JsonArray
42
     */
43
    private $packageInformation;
44
45
    /**
46
     * The dumper to use.
47
     *
48
     * @var ArrayDumper
49
     */
50
    private $dumper;
51
52
    /**
53
     * The pool in use.
54
     *
55
     * @var Pool
56
     */
57
    private $pool;
58
59
    /**
60
     * Create a new instance.
61
     *
62
     * @param JsonArray $packageInformation The log where package manipulations shall get logged to.
63
     */
64
    public function __construct(JsonArray $packageInformation)
65
    {
66
        $this->packageInformation = $packageInformation;
67
        $this->dumper             = new ArrayDumper();
68
    }
69
70
    /**
71
     * Set the pool.
72
     *
73
     * @param Pool $pool The new value.
74
     *
75
     * @return InstallationManager
76
     */
77
    public function setPool($pool)
78
    {
79
        $this->pool = $pool;
80
81
        return $this;
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 View Code Duplication
    public function install(RepositoryInterface $repo, InstallOperation $operation)
88
    {
89
        $this->packageInformation->set(
90
            $this->packageInformation->escape($operation->getPackage()->getPrettyName()),
91
            [
92
                'type'    => 'install',
93
                'reason'  => $this->getReason($operation),
94
                'package' => $this->dumper->dump($operation->getPackage())
95
            ]
96
        );
97
98
        parent::install($repo, $operation);
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 View Code Duplication
    public function update(RepositoryInterface $repo, UpdateOperation $operation)
105
    {
106
        $this->packageInformation->set(
107
            $this->packageInformation->escape($operation->getInitialPackage()->getPrettyName()),
108
            [
109
                'type'    => 'update',
110
                'reason'  => $this->getReason($operation),
111
                'package' => $this->dumper->dump($operation->getInitialPackage()),
112
                'target'  => $this->dumper->dump($operation->getTargetPackage())
113
            ]
114
        );
115
        parent::update($repo, $operation);
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function uninstall(RepositoryInterface $repo, UninstallOperation $operation)
122
    {
123
        $this->packageInformation->set(
124
            $this->packageInformation->escape($operation->getPackage()->getPrettyName()),
125
            [
126
                'type'    => 'uninstall',
127
                'package' => $this->dumper->dump($operation->getPackage())
128
            ]
129
        );
130
        parent::uninstall($repo, $operation);
131
    }
132
133
    /**
134
     * Convert reason to text.
135
     *
136
     * @param OperationInterface $operation The operation to obtain the reason from.
137
     *
138
     * @return string|null
139
     */
140
    private function getReason(OperationInterface $operation)
141
    {
142
        if (!$this->pool) {
143
            return null;
144
        }
145
146
        $reason = $operation->getReason();
147
        if ($reason instanceof Rule) {
148
            switch ($reason->getReason()) {
149
                case Rule::RULE_JOB_INSTALL:
150
                    return 'Required by the root package: ' . $reason->getPrettyString($this->pool);
151
                case Rule::RULE_PACKAGE_REQUIRES:
152
                    return $reason->getPrettyString($this->pool);
153
                default:
154
            }
155
        }
156
157
        return null;
158
    }
159
}
160