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

LoadBindingDescriptor::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 13
cts 14
cp 0.9286
rs 8.7972
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\Discovery\Binding;
13
14
use Puli\Manager\Api\Discovery\BindingDescriptor;
15
use Puli\Manager\Api\Module\Module;
16
use Puli\Manager\Discovery\Type\BindingTypeDescriptorCollection;
17
use Puli\Manager\Transaction\AtomicOperation;
18
19
/**
20
 * Loads a binding descriptor.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class LoadBindingDescriptor implements AtomicOperation
27
{
28
    /**
29
     * @var BindingDescriptor
30
     */
31
    private $bindingDescriptor;
32
33
    /**
34
     * @var Module
35
     */
36
    private $containingModule;
37
38
    /**
39
     * @var BindingDescriptorCollection
40
     */
41
    private $bindingDescriptors;
42
43
    /**
44
     * @var BindingTypeDescriptorCollection
45
     */
46
    private $typeDescriptors;
47
48
    /**
49
     * @var BindingDescriptor
50
     */
51
    private $previousDescriptor;
52
53 24
    public function __construct(BindingDescriptor $bindingDescriptor, Module $containingModule, BindingDescriptorCollection $bindingDescriptors, BindingTypeDescriptorCollection $typeDescriptors)
54
    {
55 24
        $this->bindingDescriptor = $bindingDescriptor;
56 24
        $this->containingModule = $containingModule;
57 24
        $this->bindingDescriptors = $bindingDescriptors;
58 24
        $this->typeDescriptors = $typeDescriptors;
59 24
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 24
    public function execute()
65
    {
66
        // sanity check
67 24
        if ($this->bindingDescriptor->isLoaded()) {
68
            return;
69
        }
70
71 24
        $typeName = $this->bindingDescriptor->getTypeName();
72 24
        $typeDescriptor = $this->typeDescriptors->contains($typeName)
73 21
            ? $this->typeDescriptors->getFirst($typeName)
74 24
            : null;
75
76 24
        $this->bindingDescriptor->load($this->containingModule, $typeDescriptor);
77
78 24
        $binding = $this->bindingDescriptor->getBinding();
79 24
        $moduleName = $this->containingModule->getName();
80
81 24
        if ($this->bindingDescriptors->contains($binding, $moduleName)) {
82 1
            $this->previousDescriptor = $this->bindingDescriptors->get($binding, $moduleName);
83
        }
84
85 24
        $this->bindingDescriptors->add($this->bindingDescriptor);
86 24
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 3
    public function rollback()
92
    {
93
        // sanity check
94 3
        if (!$this->bindingDescriptor->isLoaded()) {
95
            return;
96
        }
97
98
        // never fails with the check before
99 3
        $this->bindingDescriptor->unload();
100
101 3
        if ($this->previousDescriptor) {
102
            // never fails
103
            $this->bindingDescriptors->add($this->previousDescriptor);
104
        } else {
105
            // never fails
106 3
            $this->bindingDescriptors->remove(
107 3
                $this->bindingDescriptor->getBinding(),
108 3
                $this->bindingDescriptor->getContainingModule()->getName()
109
            );
110
        }
111
    }
112
}
113