Failed Conditions
Pull Request — master (#56)
by Bernhard
11:07
created

UnloadBindingDescriptor::rollback()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 8.8571
cc 5
eloc 6
nc 3
nop 0
crap 30
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\Discovery\Binding;
13
14
use Puli\Manager\Api\Discovery\BindingDescriptor;
15
use Puli\Manager\Api\Discovery\BindingTypeDescriptor;
16
use Puli\Manager\Api\Module\Module;
17
use Puli\Manager\Transaction\AtomicOperation;
18
19
/**
20
 * Unloads a binding descriptor.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class UnloadBindingDescriptor implements AtomicOperation
27
{
28
    /**
29
     * @var BindingDescriptor
30
     */
31
    private $bindingDescriptor;
32
33
    /**
34
     * @var BindingDescriptorCollection
35
     */
36
    private $bindingDescriptors;
37
38
    /**
39
     * @var Module
40
     */
41
    private $containingModule;
42
43
    /**
44
     * @var BindingTypeDescriptor
45
     */
46
    private $typeDescriptor;
47
48
    /**
49
     * @var bool
50
     */
51
    private $wasRemoved = false;
52
53
    public function __construct(BindingDescriptor $bindingDescriptor, BindingDescriptorCollection $bindingDescriptors)
54
    {
55
        $this->bindingDescriptor = $bindingDescriptor;
56
        $this->bindingDescriptors = $bindingDescriptors;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function execute()
63
    {
64
        // sanity check
65
        if (!$this->bindingDescriptor->isLoaded()) {
66
            return;
67
        }
68
69
        $this->containingModule = $this->bindingDescriptor->getContainingModule();
70
        $this->typeDescriptor = $this->bindingDescriptor->getTypeDescriptor();
71
72
        $binding = $this->bindingDescriptor->getBinding();
73
        $moduleName = $this->containingModule->getName();
74
75
        // never fails with the check in the beginning
76
        $this->bindingDescriptor->unload();
77
78
        if ($this->bindingDescriptors->contains($binding, $moduleName)
79
            && $this->bindingDescriptor === $this->bindingDescriptors->get($binding, $moduleName)) {
80
            // never fails
81
            $this->bindingDescriptors->remove($binding, $moduleName);
82
            $this->wasRemoved = true;
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function rollback()
90
    {
91
        if ($this->bindingDescriptor->isLoaded() || !$this->containingModule || !$this->typeDescriptor) {
92
            return;
93
        }
94
95
        // never fails with the check before, given that the type name of
96
        // the description/type didn't changed, which is impossible since
97
        // they're immutable
98
        $this->bindingDescriptor->load($this->containingModule, $this->typeDescriptor);
99
100
        if ($this->wasRemoved) {
101
            // never fails
102
            $this->bindingDescriptors->add($this->bindingDescriptor);
103
        }
104
    }
105
}
106