|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Persistence\Anonymizer; |
|
6
|
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
8
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
|
9
|
|
|
use Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer; |
|
10
|
|
|
use Netgen\InformationCollection\API\Persistence\Anonymizer\Visitor\FieldAnonymizerVisitor; |
|
11
|
|
|
use Netgen\InformationCollection\API\Service\InformationCollection; |
|
12
|
|
|
use Netgen\InformationCollection\API\Value\Collection; |
|
13
|
|
|
use Netgen\InformationCollection\API\Value\Filter\CollectionId; |
|
14
|
|
|
use Netgen\InformationCollection\API\Value\Attribute; |
|
15
|
|
|
|
|
16
|
|
|
class AnonymizerService implements Anonymizer |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $repository; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Netgen\InformationCollection\API\Persistence\Anonymizer\Visitor\FieldAnonymizerVisitor |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $fieldAnonymizerVisitor; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Netgen\InformationCollection\API\Service\InformationCollection |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $informationCollection; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Anonymizer constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
|
37
|
|
|
* @param \Netgen\InformationCollection\API\Service\InformationCollection $informationCollection |
|
38
|
|
|
* @param \Netgen\InformationCollection\API\Persistence\Anonymizer\Visitor\FieldAnonymizerVisitor $fieldAnonymizerVisitor |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
Repository $repository, |
|
42
|
|
|
InformationCollection $informationCollection, |
|
43
|
|
|
FieldAnonymizerVisitor $fieldAnonymizerVisitor |
|
44
|
|
|
) { |
|
45
|
|
|
$this->informationCollection = $informationCollection; |
|
46
|
|
|
$this->repository = $repository; |
|
47
|
|
|
$this->fieldAnonymizerVisitor = $fieldAnonymizerVisitor; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function anonymizeCollection(int $collectionId, array $fields = []): void |
|
51
|
|
|
{ |
|
52
|
|
|
$collectionId = new CollectionId($collectionId); |
|
53
|
|
|
$collection = $this->informationCollection->getCollection($collectionId); |
|
54
|
|
|
|
|
55
|
|
|
$this->destroyData($collection, $fields); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param \Netgen\InformationCollection\API\Value\Collection $collection |
|
60
|
|
|
* @param array $fields |
|
61
|
|
|
* |
|
62
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
|
63
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function destroyData(Collection $collection, array $fields = []) |
|
66
|
|
|
{ |
|
67
|
|
|
$contentType = $this->repository |
|
68
|
|
|
->getContentTypeService() |
|
69
|
|
|
->loadContentType($collection->getContent()->contentInfo->contentTypeId); |
|
70
|
|
|
|
|
71
|
|
|
$attributes = $this->filterAttributes($collection, $fields); |
|
72
|
|
|
|
|
73
|
|
|
if (empty($attributes)) { |
|
74
|
|
|
throw new \OutOfRangeException("The is no valid fields selected for anonymization"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$collectionId = new CollectionId($collection->getId()); |
|
78
|
|
|
$this->anonymize($collectionId, $attributes, $contentType); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param \Netgen\InformationCollection\API\Value\Attribute[] $attributes |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function anonymize(CollectionId $collectionId, array $attributes, ContentType $contentType) |
|
85
|
|
|
{ |
|
86
|
|
|
foreach ($attributes as $attribute) { |
|
87
|
|
|
$value = $this->fieldAnonymizerVisitor->visit($attribute, $contentType); |
|
88
|
|
|
|
|
89
|
|
|
$attributeToUpdate = Attribute::createFromAttributeAndValue($attribute, $value); |
|
90
|
|
|
|
|
91
|
|
|
$this->informationCollection->updateCollectionAttribute($collectionId, $attributeToUpdate); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Filter attributes based on the user selection of fields to anonymize |
|
97
|
|
|
* |
|
98
|
|
|
* @param Collection $collection |
|
99
|
|
|
* @param array $fields |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function filterAttributes(Collection $collection, array $fields): array |
|
104
|
|
|
{ |
|
105
|
|
|
if (empty($fields)) { |
|
106
|
|
|
return $collection->getAttributes(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$attributes = $collection->getAttributes(); |
|
110
|
|
|
|
|
111
|
|
|
$filtered = []; |
|
112
|
|
|
foreach ($attributes as $attribute) { |
|
113
|
|
|
|
|
114
|
|
|
if (in_array($attribute->getFieldDefinition()->id, $fields)) { |
|
115
|
|
|
$filtered[] = $attribute; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $filtered; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|