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\Doctrine\Entity\EzInfoCollection; |
12
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository; |
13
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository; |
14
|
|
|
|
15
|
|
|
class AnonymizerService implements Anonymizer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository |
19
|
|
|
*/ |
20
|
|
|
protected $collectionRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository |
24
|
|
|
*/ |
25
|
|
|
protected $collectionAttributeRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
29
|
|
|
*/ |
30
|
|
|
protected $repository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Netgen\InformationCollection\API\Persistence\Anonymizer\Visitor\FieldAnonymizerVisitor |
34
|
|
|
*/ |
35
|
|
|
protected $fieldAnonymizerVisitor; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Anonymizer constructor. |
39
|
|
|
* |
40
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
41
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository $collectionRepository |
42
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository $collectionAttributeRepository |
43
|
|
|
* @param \Netgen\InformationCollection\API\Persistence\Anonymizer\Visitor\FieldAnonymizerVisitor $fieldAnonymizerVisitor |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
Repository $repository, |
47
|
|
|
EzInfoCollectionRepository $collectionRepository, |
48
|
|
|
EzInfoCollectionAttributeRepository $collectionAttributeRepository, |
49
|
|
|
FieldAnonymizerVisitor $fieldAnonymizerVisitor |
50
|
|
|
) { |
51
|
|
|
$this->collectionRepository = $collectionRepository; |
52
|
|
|
$this->collectionAttributeRepository = $collectionAttributeRepository; |
53
|
|
|
$this->repository = $repository; |
54
|
|
|
$this->fieldAnonymizerVisitor = $fieldAnonymizerVisitor; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function anonymizeCollection($collectionId, array $fields = []): void |
58
|
|
|
{ |
59
|
|
|
$collection = $this->collectionRepository->find($collectionId); |
60
|
|
|
|
61
|
|
|
if (!$collection instanceof EzInfoCollection) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->destroyData($collection, $fields); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection $collection |
70
|
|
|
* @param array $fields |
71
|
|
|
* |
72
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
73
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
74
|
|
|
*/ |
75
|
|
|
protected function destroyData(EzInfoCollection $collection, array $fields = []) |
76
|
|
|
{ |
77
|
|
|
$content = $this->repository |
78
|
|
|
->getContentService() |
79
|
|
|
->loadContent($collection->getContentObjectId()); |
80
|
|
|
|
81
|
|
|
$contentType = $this->repository |
82
|
|
|
->getContentTypeService() |
83
|
|
|
->loadContentType($content->contentInfo->contentTypeId); |
84
|
|
|
|
85
|
|
|
$query = [ |
86
|
|
|
'informationCollectionId' => $collection->getId(), |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
if (!empty($fields)) { |
90
|
|
|
$query['contentClassAttributeId'] = $fields; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$attributes = $this->collectionAttributeRepository |
94
|
|
|
->findBy($query); |
95
|
|
|
|
96
|
|
|
$this->anonymize($attributes, $contentType); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute[] $attributes |
101
|
|
|
*/ |
102
|
|
|
protected function anonymize(array $attributes, ContentType $contentType) |
103
|
|
|
{ |
104
|
|
|
foreach ($attributes as $attribute) { |
105
|
|
|
$value = $this->fieldAnonymizerVisitor->visit($attribute, $contentType); |
106
|
|
|
$attribute->setDataText($value); |
107
|
|
|
|
108
|
|
|
$this->collectionAttributeRepository->save($attribute); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|