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

LoadBindingDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 1
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\Package\Package;
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 Package
35
     */
36
    private $containingPackage;
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 52
    public function __construct(BindingDescriptor $bindingDescriptor, Package $containingPackage, BindingDescriptorCollection $bindingDescriptors, BindingTypeDescriptorCollection $typeDescriptors)
54
    {
55 52
        $this->bindingDescriptor = $bindingDescriptor;
56 52
        $this->containingPackage = $containingPackage;
57 52
        $this->bindingDescriptors = $bindingDescriptors;
58 52
        $this->typeDescriptors = $typeDescriptors;
59 52
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 52 View Code Duplication
    public function execute()
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...
65
    {
66
        // sanity check
67 52
        if ($this->bindingDescriptor->isLoaded()) {
68
            return;
69
        }
70
71 52
        $typeName = $this->bindingDescriptor->getTypeName();
72 52
        $typeDescriptor = $this->typeDescriptors->contains($typeName)
73 45
            ? $this->typeDescriptors->getFirst($typeName)
74 52
            : null;
75
76 52
        $this->bindingDescriptor->load($this->containingPackage, $typeDescriptor);
77
78 52
        $uuid = $this->bindingDescriptor->getUuid();
79
80 52
        if ($this->bindingDescriptors->contains($uuid)) {
81 1
            $this->previousDescriptor = $this->bindingDescriptors->get($uuid);
82
        }
83
84 52
        $this->bindingDescriptors->add($this->bindingDescriptor);
85 52
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function rollback()
91
    {
92
        // sanity check
93 2
        if (!$this->bindingDescriptor->isLoaded()) {
94
            return;
95
        }
96
97
        // never fails with the check before
98 2
        $this->bindingDescriptor->unload();
99
100 2
        if ($this->previousDescriptor) {
101
            // never fails
102
            $this->bindingDescriptors->add($this->previousDescriptor);
103
        } else {
104
            // never fails
105 2
            $this->bindingDescriptors->remove($this->bindingDescriptor->getUuid());
106
        }
107 2
    }
108
}
109