Failed Conditions
Push — master ( d49c03...6343d0 )
by Bernhard
05:03
created

AddPathMappingToModuleFile::rollback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2.032
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\Module\RootModuleFile;
15
use Puli\Manager\Api\Repository\PathMapping;
16
use Puli\Manager\Transaction\AtomicOperation;
17
18
/**
19
 * Adds a path mapping to the root module file.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 */
25
class AddPathMappingToModuleFile implements AtomicOperation
26
{
27
    /**
28
     * @var PathMapping
29
     */
30
    private $mapping;
31
32
    /**
33
     * @var RootModuleFile
34
     */
35
    private $rootModuleFile;
36
37
    /**
38
     * @var PathMapping
39
     */
40
    private $previousMapping;
41
42 13
    public function __construct(PathMapping $mapping, RootModuleFile $rootModuleFile)
43
    {
44 13
        $this->mapping = $mapping;
45 13
        $this->rootModuleFile = $rootModuleFile;
46 13
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 13
    public function execute()
52
    {
53 13
        $repositoryPath = $this->mapping->getRepositoryPath();
54
55 13
        if ($this->rootModuleFile->hasPathMapping($repositoryPath)) {
56 1
            $this->previousMapping = $this->rootModuleFile->getPathMapping($repositoryPath);
57
        }
58
59 13
        $this->rootModuleFile->addPathMapping($this->mapping);
60 13
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function rollback()
66
    {
67 2
        if ($this->previousMapping) {
68
            $this->rootModuleFile->addPathMapping($this->previousMapping);
69
        } else {
70 2
            $this->rootModuleFile->removePathMapping($this->mapping->getRepositoryPath());
71
        }
72 2
    }
73
}
74