SyncBindingUuid::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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 LogicException;
15
use Puli\Discovery\Api\EditableDiscovery;
16
use Puli\Manager\Api\Discovery\BindingDescriptor;
17
use Puli\Manager\Transaction\AtomicOperation;
18
use Rhumsaa\Uuid\Uuid;
19
20
/**
21
 * Synchronizes a binding descriptor UUID with the discovery.
22
 *
23
 * The method {@link takeSnapshot()} must be called before executing the
24
 * operation. This method will record whether a binding descriptor is currently
25
 * enabled (i.e. loaded in the discovery) for that UUID.
26
 *
27
 * Once the operation is executed, another snapshot is taken. If the snapshots
28
 * differ, the binding descriptor for the UUID is then either bound to or
29
 * unbound from the discovery, depending on the outcome.
30
 *
31
 * @since  1.0
32
 *
33
 * @author Bernhard Schussek <[email protected]>
34
 */
35
class SyncBindingUuid implements AtomicOperation
36
{
37
    /**
38
     * @var Uuid
39
     */
40
    private $uuid;
41
42
    /**
43
     * @var EditableDiscovery
44
     */
45
    private $discovery;
46
47
    /**
48
     * @var BindingDescriptor
49
     */
50
    private $enabledBindingBefore;
51
52
    /**
53
     * @var BindingDescriptor
54
     */
55
    private $enabledBindingAfter;
56
57
    /**
58
     * @var BindingDescriptorCollection
59
     */
60
    private $bindingDescriptors;
61
62
    /**
63
     * @var bool
64
     */
65
    private $snapshotTaken = false;
66
67 24
    public function __construct(Uuid $uuid, EditableDiscovery $discovery, BindingDescriptorCollection $bindingDescriptors)
68
    {
69 24
        $this->uuid = $uuid;
70 24
        $this->discovery = $discovery;
71 24
        $this->bindingDescriptors = $bindingDescriptors;
72 24
    }
73
74
    /**
75
     * Records whether the UUID is currently enabled.
76
     */
77 24
    public function takeSnapshot()
78
    {
79 24
        $this->enabledBindingBefore = null;
80
81 24 View Code Duplication
        if ($this->bindingDescriptors->contains($this->uuid)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82 19
            $bindingDescriptor = $this->bindingDescriptors->get($this->uuid);
83
84 19
            if ($bindingDescriptor->isEnabled()) {
85
                // Clone so that rollback() works if the binding is unloaded
86 11
                $this->enabledBindingBefore = clone $bindingDescriptor;
87
            }
88
        }
89
90 24
        $this->snapshotTaken = true;
91 24
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 23
    public function execute()
97
    {
98 23
        if (!$this->snapshotTaken) {
99
            throw new LogicException('takeSnapshot() was not called');
100
        }
101
102
        // Remember for rollback()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103 23
        $this->enabledBindingAfter = null;
104
105 23 View Code Duplication
        if ($this->bindingDescriptors->contains($this->uuid)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
106 16
            $bindingDescriptor = $this->bindingDescriptors->get($this->uuid);
107
108 16
            if ($bindingDescriptor->isEnabled()) {
109
                // Clone so that rollback() works if the binding is unloaded
110 8
                $this->enabledBindingAfter = clone $bindingDescriptor;
111
            }
112
        }
113
114 23
        $this->syncBindingUuid($this->enabledBindingBefore, $this->enabledBindingAfter);
115 23
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 7
    public function rollback()
121
    {
122 7
        $this->syncBindingUuid($this->enabledBindingAfter, $this->enabledBindingBefore);
123 7
    }
124
125 23
    private function syncBindingUuid(BindingDescriptor $enabledBefore = null, BindingDescriptor $enabledAfter = null)
126
    {
127 23
        if (!$enabledBefore && $enabledAfter) {
128 11
            $this->discovery->addBinding($enabledAfter->getBinding());
129 19
        } elseif ($enabledBefore && !$enabledAfter) {
130 13
            $this->discovery->removeBinding($enabledBefore->getUuid());
131 6
        } elseif ($enabledBefore && $enabledAfter && $enabledBefore->getBinding() != $enabledAfter->getBinding()) {
132 1
            $this->discovery->removeBinding($enabledBefore->getUuid());
133 1
            $this->discovery->addBinding($enabledAfter->getBinding());
134
        }
135 23
    }
136
}
137