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

SyncBinding   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 84.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 104
ccs 27
cts 32
cp 0.8438
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A takeSnapshot() 0 13 3
A execute() 0 18 4
A rollback() 0 4 1
B syncBinding() 0 17 5
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\Binding\Binding;
16
use Puli\Discovery\Api\EditableDiscovery;
17
use Puli\Manager\Api\Discovery\BindingDescriptor;
18
use Puli\Manager\Transaction\AtomicOperation;
19
use Rhumsaa\Uuid\Uuid;
20
use Webmozart\Expression\Expr;
21
22
/**
23
 * Synchronizes a binding descriptor with the discovery.
24
 *
25
 * The method {@link takeSnapshot()} must be called before executing the
26
 * operation. This method will record whether the binding descriptor is currently
27
 * enabled (i.e. loaded in the discovery).
28
 *
29
 * Once the operation is executed, another snapshot is taken. If the snapshots
30
 * differ, the binding descriptor is then either added to or removed from the
31
 * discovery, depending on the outcome.
32
 *
33
 * @since  1.0
34
 *
35
 * @author Bernhard Schussek <[email protected]>
36
 */
37
class SyncBinding implements AtomicOperation
38
{
39
    /**
40
     * @var Binding
41
     */
42
    private $binding;
43
44
    /**
45
     * @var EditableDiscovery
46
     */
47
    private $discovery;
48
49
    /**
50
     * @var BindingDescriptor
51
     */
52
    private $enabledBindingBefore;
53
54
    /**
55
     * @var BindingDescriptor
56
     */
57
    private $enabledBindingAfter;
58
59
    /**
60
     * @var BindingDescriptorCollection
61
     */
62
    private $bindingDescriptors;
63
64
    /**
65
     * @var bool
66
     */
67
    private $snapshotTaken = false;
68
69 6
    public function __construct(Binding $binding, EditableDiscovery $discovery, BindingDescriptorCollection $bindingDescriptors)
70
    {
71 6
        $this->binding = $binding;
72 6
        $this->discovery = $discovery;
73 6
        $this->bindingDescriptors = $bindingDescriptors;
74 6
    }
75
76
    /**
77
     * Records whether the UUID is currently enabled.
78
     */
79 6
    public function takeSnapshot()
80
    {
81 6
        $this->enabledBindingBefore = null;
82
83 6
        foreach ($this->bindingDescriptors->listByBinding($this->binding) as $bindingDescriptor) {
84 3
            if ($bindingDescriptor->isEnabled()) {
85
                // Clone so that rollback() works if the binding is unloaded
86 3
                $this->enabledBindingBefore = clone $bindingDescriptor;
87
            }
88
        }
89
90 6
        $this->snapshotTaken = true;
91 6
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 5
    public function execute()
97
    {
98 5
        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 5
        $this->enabledBindingAfter = null;
104
105 5
        foreach ($this->bindingDescriptors->listByBinding($this->binding) as $bindingDescriptor) {
106 5
            if ($bindingDescriptor->isEnabled()) {
107
                // Clone so that rollback() works if the binding is unloaded
108 5
                $this->enabledBindingAfter = clone $bindingDescriptor;
109
            }
110
        }
111
112 5
        $this->syncBinding($this->enabledBindingBefore, $this->enabledBindingAfter);
0 ignored issues
show
Bug introduced by
It seems like $this->enabledBindingAfter can also be of type object; however, Puli\Manager\Discovery\B...cBinding::syncBinding() does only seem to accept null|object<Puli\Manager...very\BindingDescriptor>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
113 5
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 2
    public function rollback()
119
    {
120 2
        $this->syncBinding($this->enabledBindingAfter, $this->enabledBindingBefore);
121 2
    }
122
123 5
    private function syncBinding(BindingDescriptor $enabledBefore = null, BindingDescriptor $enabledAfter = null)
124
    {
125 5
        if (!$enabledBefore && $enabledAfter) {
126
            $this->discovery->addBinding($this->binding);
127 5
        } elseif ($enabledBefore && !$enabledAfter) {
128
            $this->discovery->removeBindings(
129
                $this->binding->getTypeName(),
130
                Expr::method('equals', $this->binding, Expr::same(true))
131
            );
132
//        } elseif ($enabledBefore && $enabledAfter && !$enabledBefore->getBinding()->equals($enabledAfter->getBinding())) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
133
//            $this->discovery->removeBindings(
134
//                $enabledBefore->getTypeName(),
135
//                Expr::method('equals', $enabledBefore->getBinding(), Expr::same(true))
136
//            );
137
//            $this->discovery->addBinding($enabledAfter->getBinding());
138
        }
139 5
    }
140
}
141