1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Persistence\Anonymizer; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer; |
9
|
|
|
use Netgen\InformationCollection\Core\Persistence\ContentTypeUtils; |
10
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository; |
11
|
|
|
use OutOfBoundsException; |
12
|
|
|
|
13
|
|
|
final class AnonymizerServiceFacade |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer |
17
|
|
|
*/ |
18
|
|
|
private $anonymizer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Netgen\InformationCollection\Core\Persistence\ContentTypeUtils |
22
|
|
|
*/ |
23
|
|
|
private $contentTypeUtils; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository |
27
|
|
|
*/ |
28
|
|
|
private $ezInfoCollectionRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AnonymizerServiceFacade constructor. |
32
|
|
|
* |
33
|
|
|
* @param \Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer $anonymizer |
34
|
|
|
* @param \Netgen\InformationCollection\Core\Persistence\ContentTypeUtils $contentTypeUtils |
35
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository $ezInfoCollectionRepository |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
Anonymizer $anonymizer, |
39
|
|
|
ContentTypeUtils $contentTypeUtils, |
40
|
|
|
EzInfoCollectionRepository $ezInfoCollectionRepository |
41
|
|
|
) { |
42
|
|
|
$this->anonymizer = $anonymizer; |
43
|
|
|
$this->contentTypeUtils = $contentTypeUtils; |
44
|
|
|
$this->ezInfoCollectionRepository = $ezInfoCollectionRepository; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Anonymize collections by content id. |
49
|
|
|
* |
50
|
|
|
* @param int $contentId Content id |
51
|
|
|
* @param array $fields Fields list |
52
|
|
|
* @param DateTime|null $date Anonymize collections older that this date |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
public function anonymize($contentId, array $fields = [], \DateTimeImmutable $date = null) |
57
|
|
|
{ |
58
|
|
|
$fieldsWithIds = $this->getFieldIds($contentId, $fields); |
59
|
|
|
|
60
|
|
|
if (!empty($fields) && empty($fieldsWithIds)) { |
61
|
|
|
return 0; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$collections = $this->getCollections($contentId, $date); |
65
|
|
|
|
66
|
|
|
foreach ($collections as $collection) { |
67
|
|
|
$this->anonymizer->anonymizeCollection($collection, $fields); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return count($collections); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Map field id's to list of field identifiers. |
75
|
|
|
* |
76
|
|
|
* @param int $content |
|
|
|
|
77
|
|
|
* @param array $fieldIdentifiers |
78
|
|
|
* @param mixed $contentId |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private function getFieldIds($contentId, array $fieldIdentifiers) |
83
|
|
|
{ |
84
|
|
|
$ids = []; |
85
|
|
|
foreach ($fieldIdentifiers as $identifier) { |
86
|
|
|
try { |
87
|
|
|
$ids[] = $this->contentTypeUtils->getFieldId($contentId, $identifier); |
88
|
|
|
} catch (OutOfBoundsException $e) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $ids; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function getCollections($contentId, \DateTimeImmutable $date = null) |
97
|
|
|
{ |
98
|
|
|
if (null === $date) { |
99
|
|
|
$collections = $this->ezInfoCollectionRepository->findByContentId($contentId); |
100
|
|
|
} else { |
101
|
|
|
$collections = $this->ezInfoCollectionRepository->findByContentIdOlderThan($contentId, $date); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $collections; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.