SharedPackageInstallerSolver   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 12.21 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 3 Features 1
Metric Value
dl 16
loc 131
c 4
b 3
f 1
wmc 15
lcom 1
cbo 4
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstallPath() 0 8 2
A install() 8 8 2
A uninstall() 0 12 3
A __construct() 0 10 1
A isInstalled() 8 8 2
A update() 0 13 4
A supports() 0 6 1

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 the "Composer Shared Package Plugin" package.
5
 *
6
 * https://github.com/Letudiant/composer-shared-package-plugin
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LEtudiant\Composer\Installer\Solver;
13
14
use Composer\Config;
15
use Composer\Downloader\FilesystemException;
16
use Composer\Installer\InstallerInterface;
17
use Composer\Installer\LibraryInstaller;
18
use Composer\Package\PackageInterface;
19
use Composer\Repository\InstalledRepositoryInterface;
20
use LEtudiant\Composer\Installer\SharedPackageInstaller;
21
use LEtudiant\Composer\Util\SymlinkFilesystem;
22
23
/**
24
 * @author Sylvain Lorinet <[email protected]>
25
 */
26
class SharedPackageInstallerSolver implements InstallerInterface
27
{
28
    /**
29
     * @var SymlinkFilesystem
30
     */
31
    protected $filesystem;
32
33
    /**
34
     * @var SharedPackageSolver
35
     */
36
    protected $solver;
37
38
    /**
39
     * @var SharedPackageInstaller
40
     */
41
    protected $symlinkInstaller;
42
43
    /**
44
     * @var LibraryInstaller
45
     */
46
    protected $defaultInstaller;
47
48
49
    /**
50
     * @param SharedPackageSolver    $solver
51
     * @param SharedPackageInstaller $symlinkInstaller
52
     * @param LibraryInstaller       $defaultInstaller
53
     */
54
    public function __construct(
55
        SharedPackageSolver $solver,
56
        SharedPackageInstaller $symlinkInstaller,
57
        LibraryInstaller $defaultInstaller
58
    )
59
    {
60
        $this->solver           = $solver;
61
        $this->symlinkInstaller = $symlinkInstaller;
62
        $this->defaultInstaller = $defaultInstaller;
63
    }
64
65
    /**
66
     * Returns the installation path of a package
67
     *
68
     * @param  PackageInterface $package
69
     *
70
     * @return string
71
     */
72
    public function getInstallPath(PackageInterface $package)
73
    {
74
        if ($this->solver->isSharedPackage($package)) {
75
            return $this->symlinkInstaller->getInstallPath($package);
76
        }
77
78
        return $this->defaultInstaller->getInstallPath($package);
79
    }
80
81
    /**
82
     * @param InstalledRepositoryInterface $repo
83
     * @param PackageInterface             $package
84
     */
85 View Code Duplication
    public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
86
    {
87
        if ($this->solver->isSharedPackage($package)) {
88
            $this->symlinkInstaller->install($repo, $package);
89
        } else {
90
            $this->defaultInstaller->install($repo, $package);
91
        }
92
    }
93
94
    /**
95
     * @param InstalledRepositoryInterface $repo
96
     * @param PackageInterface             $package
97
     *
98
     * @return bool
99
     */
100 View Code Duplication
    public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
101
    {
102
        if ($this->solver->isSharedPackage($package)) {
103
            return $this->symlinkInstaller->isInstalled($repo, $package);
104
        }
105
106
        return $this->defaultInstaller->isInstalled($repo, $package);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
113
    {
114
        // If both packages are not shared
115
        if (!$this->solver->isSharedPackage($initial) && !$this->solver->isSharedPackage($target)) {
116
            $this->defaultInstaller->update($repo, $initial, $target);
117
        } else {
118
            if (!$repo->hasPackage($initial)) {
119
                throw new \InvalidArgumentException('Package is not installed : ' . $initial->getPrettyName());
120
            }
121
122
            $this->symlinkInstaller->update($repo, $initial, $target);
123
        }
124
    }
125
126
    /**
127
     * @param InstalledRepositoryInterface $repo
128
     * @param PackageInterface             $package
129
     *
130
     * @throws FilesystemException
131
     */
132
    public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
133
    {
134
        if ($this->solver->isSharedPackage($package)) {
135
            if (!$repo->hasPackage($package)) {
136
                throw new \InvalidArgumentException('Package is not installed : ' . $package->getPrettyName());
137
            }
138
139
            $this->symlinkInstaller->uninstall($repo, $package);
140
        } else {
141
            $this->defaultInstaller->uninstall($repo, $package);
142
        }
143
    }
144
145
    /**
146
     * @param string $packageType
147
     *
148
     * @return bool
149
     */
150
    public function supports($packageType)
151
    {
152
        // The solving process is in SharedPackageSolver::isSharedPackage() method
153
154
        return true;
155
    }
156
}
157