1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Offer\Commands; |
4
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Label; |
6
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Labels; |
7
|
|
|
use CultuurNet\UDB3\Security\LabelSecurityInterface; |
8
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
9
|
|
|
|
10
|
|
|
abstract class AbstractImportLabels extends AbstractCommand implements LabelSecurityInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Labels |
14
|
|
|
*/ |
15
|
|
|
private $labels; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Labels |
19
|
|
|
*/ |
20
|
|
|
private $labelsToKeepIfAlreadyOnOffer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Labels |
24
|
|
|
*/ |
25
|
|
|
private $labelsToRemoveWhenOnOffer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $itemId |
29
|
|
|
* @param Labels $labels |
30
|
|
|
*/ |
31
|
|
|
public function __construct($itemId, Labels $labels) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($itemId); |
34
|
|
|
$this->labels = $labels; |
35
|
|
|
$this->labelsToKeepIfAlreadyOnOffer = new Labels(); |
36
|
|
|
$this->labelsToRemoveWhenOnOffer = new Labels(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function withLabelsToKeepIfAlreadyOnOffer(Labels $labels): self |
40
|
|
|
{ |
41
|
|
|
$c = clone $this; |
42
|
|
|
$c->labelsToKeepIfAlreadyOnOffer = $labels; |
43
|
|
|
return $c; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getLabelsToKeepIfAlreadyOnOffer(): Labels |
47
|
|
|
{ |
48
|
|
|
return $this->labelsToKeepIfAlreadyOnOffer; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function withLabelsToRemoveWhenOnOffer(Labels $labels): self |
52
|
|
|
{ |
53
|
|
|
$c = clone $this; |
54
|
|
|
$c->labelsToRemoveWhenOnOffer = $labels; |
55
|
|
|
return $c; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getLabelsToRemoveWhenOnOffer(): Labels |
59
|
|
|
{ |
60
|
|
|
return $this->labelsToRemoveWhenOnOffer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Labels |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function getLabelsToImport() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$labelNamesToKeep = array_map( |
69
|
|
|
function (Label $label) { |
70
|
|
|
return $label->getName(); |
71
|
|
|
}, |
72
|
|
|
$this->labelsToKeepIfAlreadyOnOffer->toArray() |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
return $this->labels->filter( |
76
|
|
|
function (Label $label) use ($labelNamesToKeep) { |
77
|
|
|
return !in_array($label->getName(), $labelNamesToKeep); |
78
|
|
|
} |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
public function getNames() |
86
|
|
|
{ |
87
|
|
|
return array_map( |
88
|
|
|
function (Label $label) { |
89
|
|
|
return new StringLiteral($label->getName()->toString()); |
90
|
|
|
}, |
91
|
|
|
$this->getLabelsToImport()->toArray() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.