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() |
|
|
|
|
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
|
|
|
|
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.