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

UnloadBindingDescriptor::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092
Metric Value
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
rs 8.9197
cc 4
eloc 11
nc 3
nop 0
crap 4.0092
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\Package\Package;
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 Package
40
     */
41
    private $containingPackage;
42
43
    /**
44
     * @var BindingTypeDescriptor
45
     */
46
    private $typeDescriptor;
47
48
    /**
49
     * @var bool
50
     */
51
    private $wasRemoved = false;
52
53 7
    public function __construct(BindingDescriptor $bindingDescriptor, BindingDescriptorCollection $bindingDescriptors)
54
    {
55 7
        $this->bindingDescriptor = $bindingDescriptor;
56 7
        $this->bindingDescriptors = $bindingDescriptors;
57 7
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 7
    public function execute()
63
    {
64
        // sanity check
65 7
        if (!$this->bindingDescriptor->isLoaded()) {
66
            return;
67
        }
68
69 7
        $this->containingPackage = $this->bindingDescriptor->getContainingPackage();
70 7
        $this->typeDescriptor = $this->bindingDescriptor->getTypeDescriptor();
71
72 7
        $uuid = $this->bindingDescriptor->getUuid();
73
74
        // never fails with the check in the beginning
75 7
        $this->bindingDescriptor->unload();
76
77 7
        if ($this->bindingDescriptors->contains($uuid)
78 7
            && $this->bindingDescriptor === $this->bindingDescriptors->get($uuid)) {
79
            // never fails
80 7
            $this->bindingDescriptors->remove($uuid);
81 7
            $this->wasRemoved = true;
82
        }
83 7
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 2
    public function rollback()
89
    {
90 2
        if ($this->bindingDescriptor->isLoaded() || !$this->containingPackage || !$this->typeDescriptor) {
91
            return;
92
        }
93
94
        // never fails with the check before, given that the type name of
95
        // the description/type didn't changed, which is impossible since
96
        // they're immutable
97 2
        $this->bindingDescriptor->load($this->containingPackage, $this->typeDescriptor);
98
99 2
        if ($this->wasRemoved) {
100
            // never fails
101 2
            $this->bindingDescriptors->add($this->bindingDescriptor);
102
        }
103 2
    }
104
}
105