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

UnloadPathMapping::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058
Metric Value
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
rs 8.5806
cc 4
eloc 13
nc 5
nop 0
crap 4.0058
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\PathConflict;
17
use Puli\Manager\Api\Repository\PathMapping;
18
use Puli\Manager\Conflict\PackageConflictDetector;
19
use Puli\Manager\Transaction\AtomicOperation;
20
21
/**
22
 * Unloads a path mapping.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class UnloadPathMapping implements AtomicOperation
29
{
30
    /**
31
     * @var PathMapping
32
     */
33
    private $mapping;
34
35
    /**
36
     * @var Package
37
     */
38
    private $containingPackage;
39
40
    /**
41
     * @var PackageCollection
42
     */
43
    private $packages;
44
45
    /**
46
     * @var PathMappingCollection
47
     */
48
    private $mappings;
49
50
    /**
51
     * @var PathMappingCollection
52
     */
53
    private $mappingsByResource;
54
55
    /**
56
     * @var PackageConflictDetector
57
     */
58
    private $conflictDetector;
59
60
    /**
61
     * @var PathConflict[]
62
     */
63
    private $conflicts = array();
64
65
    /**
66
     * @var PathMapping[][]
67
     */
68
    private $conflictingMappings = array();
69
70 12 View Code Duplication
    public function __construct(PathMapping $mapping, 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...
71
    {
72 12
        $this->mapping = $mapping;
73 12
        $this->packages = $packages;
74 12
        $this->mappings = $mappings;
75 12
        $this->mappingsByResource = $mappingsByResource;
76 12
        $this->conflictDetector = $conflictDetector;
77 12
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 12
    public function execute()
83
    {
84 12
        if (!$this->mapping->isLoaded()) {
85
            return;
86
        }
87
88 12
        $this->containingPackage = $this->mapping->getContainingPackage();
89
90
        // Remember the conflicts that will be adjusted during unload()
91 12
        foreach ($this->mapping->getConflicts() as $conflict) {
92 2
            $this->conflicts[$conflict->getRepositoryPath()] = $conflict;
93 2
            $this->conflictingMappings[$conflict->getRepositoryPath()] = $conflict->getMappings();
94
        }
95
96 12
        $packageName = $this->containingPackage->getName();
97
98 12
        $this->mappings->remove($this->mapping->getRepositoryPath(), $packageName);
99
100 12
        foreach ($this->mapping->listRepositoryPaths() as $repositoryPath) {
101 12
            $this->mappingsByResource->remove($repositoryPath, $packageName);
102 12
            $this->conflictDetector->release($repositoryPath, $packageName);
103
        }
104
105
        // Unload after iterating, otherwise the paths are gone
106 12
        $this->mapping->unload();
107 12
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 4
    public function rollback()
113
    {
114 4
        if ($this->mapping->isLoaded() || !$this->containingPackage) {
115
            return;
116
        }
117
118 4
        $this->mapping->load($this->containingPackage, $this->packages);
119
120 4
        $packageName = $this->containingPackage->getName();
121
122 4
        foreach ($this->mapping->listRepositoryPaths() as $repositoryPath) {
123 4
            $this->mappings->add($this->mapping);
124 4
            $this->conflictDetector->claim($repositoryPath, $packageName);
125
        }
126
127
        // Restore conflicts
128 4
        foreach ($this->conflicts as $repositoryPath => $conflict) {
129 1
            $conflict->addMappings($this->conflictingMappings[$repositoryPath]);
130
        }
131 4
    }
132
}
133