Failed Conditions
Push — master ( ebb1ea...d49c03 )
by Bernhard
06:05
created

LoadPathMapping   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 10.59 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.86%
Metric Value
wmc 7
lcom 1
cbo 4
dl 9
loc 85
ccs 26
cts 28
cp 0.9286
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A execute() 0 17 3
A rollback() 0 18 3

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 puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[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
12
namespace Puli\Manager\Repository\Mapping;
13
14
use Puli\Manager\Api\Package\Package;
15
use Puli\Manager\Api\Package\PackageCollection;
16
use Puli\Manager\Api\Repository\PathMapping;
17
use Puli\Manager\Conflict\PackageConflictDetector;
18
use Puli\Manager\Transaction\AtomicOperation;
19
20
/**
21
 * Loads a path mapping.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class LoadPathMapping implements AtomicOperation
28
{
29
    /**
30
     * @var PathMapping
31
     */
32
    private $mapping;
33
34
    /**
35
     * @var Package
36
     */
37
    private $containingPackage;
38
39
    /**
40
     * @var PackageCollection
41
     */
42
    private $packages;
43
44
    /**
45
     * @var PathMappingCollection
46
     */
47
    private $mappings;
48
49
    /**
50
     * @var PathMappingCollection
51
     */
52
    private $mappingsByResource;
53
54
    /**
55
     * @var PackageConflictDetector
56
     */
57
    private $conflictDetector;
58
59 57 View Code Duplication
    public function __construct(PathMapping $mapping, Package $containingPackage, PackageCollection $packages, PathMappingCollection $mappings, PathMappingCollection $mappingsByResource, PackageConflictDetector $conflictDetector)
0 ignored issues
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...
60
    {
61 57
        $this->mapping = $mapping;
62 57
        $this->containingPackage = $containingPackage;
63 57
        $this->packages = $packages;
64 57
        $this->mappings = $mappings;
65 57
        $this->mappingsByResource = $mappingsByResource;
66 57
        $this->conflictDetector = $conflictDetector;
67 57
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 57
    public function execute()
73
    {
74 57
        if ($this->mapping->isLoaded()) {
75
            return;
76
        }
77
78 57
        $this->mapping->load($this->containingPackage, $this->packages);
79
80 57
        $packageName = $this->containingPackage->getName();
81
82 57
        $this->mappings->add($this->mapping);
83
84 57
        foreach ($this->mapping->listRepositoryPaths() as $repositoryPath) {
85 54
            $this->mappingsByResource->set($repositoryPath, $this->mapping);
86 54
            $this->conflictDetector->claim($repositoryPath, $packageName);
87
        }
88 57
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 3
    public function rollback()
94
    {
95 3
        if (!$this->mapping->isLoaded()) {
96
            return;
97
        }
98
99 3
        $packageName = $this->containingPackage->getName();
100
101 3
        $this->mappings->remove($this->mapping->getRepositoryPath(), $packageName);
102
103 3
        foreach ($this->mapping->listRepositoryPaths() as $repositoryPath) {
104 2
            $this->mappingsByResource->remove($repositoryPath, $packageName);
105 2
            $this->conflictDetector->release($repositoryPath, $packageName);
106
        }
107
108
        // Unload after iterating, otherwise the paths are gone
109 3
        $this->mapping->unload();
110 3
    }
111
}
112