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

InstallationManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 20.97 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 8
dl 26
loc 124
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPool() 0 6 1
A install() 13 13 1
A update() 13 13 1
A uninstall() 0 11 1
B getReason() 0 19 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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