-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from superbrave/create-anonymizers
Anonymization ready
- Loading branch information
Showing
29 changed files
with
1,735 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* This file is part of the GDPR bundle. | ||
* | ||
* @category Bundle | ||
* @package Gdpr | ||
* @author SuperBrave <info@superbrave.nl> | ||
* @copyright 2018 SuperBrave <info@superbrave.nl> | ||
* @license https://github.com/superbrave/gdpr-bundle/blob/master/LICENSE MIT | ||
* @link https://www.superbrave.nl/ | ||
*/ | ||
|
||
namespace SuperBrave\GdprBundle\Anonymize; | ||
|
||
use SuperBrave\GdprBundle\Annotation\AnnotationReader; | ||
use SuperBrave\GdprBundle\Annotation\Anonymize; | ||
use InvalidArgumentException; | ||
use ReflectionException; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Class Anonymizer | ||
* | ||
* @package SuperBrave\GdprBundle\Anonymize | ||
*/ | ||
class Anonymizer | ||
{ | ||
/** | ||
* @var AnnotationReader | ||
*/ | ||
private $annotationReader; | ||
|
||
/** | ||
* @var PropertyAnonymizer | ||
*/ | ||
private $propertyAnonymizer; | ||
|
||
/** | ||
* Anonymizer constructor. | ||
* | ||
* @param AnnotationReader $annotationReader The annotation reader that should be used. | ||
* @param PropertyAnonymizer $propertyAnonymizer The property anonymizer. | ||
*/ | ||
public function __construct( | ||
AnnotationReader $annotationReader, | ||
PropertyAnonymizer $propertyAnonymizer | ||
) { | ||
$this->annotationReader = $annotationReader; | ||
$this->propertyAnonymizer = $propertyAnonymizer; | ||
} | ||
|
||
/** | ||
* Anonymizes the given object which should contain the @see Anonymize annotations. | ||
* | ||
* @param object $object The object to anonymize. | ||
* | ||
* @return void | ||
* | ||
* @throws InvalidArgumentException If argument supplied is not an object. | ||
* @throws ReflectionException If class doesn't exist. | ||
*/ | ||
public function anonymize(/*object */$object) | ||
{ | ||
if (!is_object($object)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Invalid argument given "%s" should be of type object.', | ||
gettype($object) | ||
)); | ||
} | ||
|
||
$reflectionClass = new ReflectionClass($object); | ||
$annotations = $this->annotationReader->getPropertiesWithAnnotation($reflectionClass, Anonymize::class); | ||
|
||
foreach ($annotations as $property => $annotation) { | ||
$this->propertyAnonymizer->anonymizeField($object, $property, $annotation); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* This file is part of the GDPR bundle. | ||
* | ||
* @category Bundle | ||
* @package Gdpr | ||
* @author SuperBrave <info@superbrave.nl> | ||
* @copyright 2018 SuperBrave <info@superbrave.nl> | ||
* @license https://github.com/superbrave/gdpr-bundle/blob/master/LICENSE MIT | ||
* @link https://www.superbrave.nl/ | ||
*/ | ||
|
||
namespace SuperBrave\GdprBundle\Anonymize; | ||
|
||
use LogicException; | ||
use SuperBrave\GdprBundle\Anonymize\Type\AnonymizerInterface; | ||
|
||
/** | ||
* Class AnonymizerCollection | ||
* | ||
* @package SuperBrave\GdprBundle\Anonymize | ||
*/ | ||
class AnonymizerCollection | ||
{ | ||
/** | ||
* Array of anonymizers | ||
* | ||
* @var AnonymizerInterface[] | ||
*/ | ||
private $anonymizers = []; | ||
|
||
/** | ||
* Adds an anonymizer to the collection. | ||
* | ||
* @param string $type The anonymizer type to be added. | ||
* @param AnonymizerInterface $anonymizer The anonymizer class to be added. | ||
* | ||
* @return void | ||
* | ||
* @throws LogicException On duplicate anonymizer keys. | ||
*/ | ||
public function addAnonymizer($type, $anonymizer) | ||
{ | ||
if (array_key_exists($type, $this->anonymizers)) { | ||
throw new LogicException(sprintf('Anonymizer %s already exists.', $type)); | ||
} | ||
|
||
$this->anonymizers[$type] = $anonymizer; | ||
} | ||
|
||
/** | ||
* Get an anonymizer by its type from the collection. | ||
* | ||
* @param string $type The anonymizer type to be fetched. | ||
* | ||
* @return AnonymizerInterface | ||
* | ||
* @throws LogicException If the anonymizer type is not registered. | ||
*/ | ||
public function getAnonymizer($type) | ||
{ | ||
if (!array_key_exists($type, $this->anonymizers)) { | ||
throw new LogicException(sprintf('Anonymizer %s is not registered.', $type)); | ||
} | ||
|
||
return $this->anonymizers[$type]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* This file is part of the GDPR bundle. | ||
* | ||
* @category Bundle | ||
* @package Gdpr | ||
* @author SuperBrave <info@superbrave.nl> | ||
* @copyright 2018 SuperBrave <info@superbrave.nl> | ||
* @license https://github.com/superbrave/gdpr-bundle/blob/master/LICENSE MIT | ||
* @link https://www.superbrave.nl/ | ||
*/ | ||
|
||
namespace SuperBrave\GdprBundle\Anonymize; | ||
|
||
use SuperBrave\GdprBundle\Annotation\Anonymize; | ||
use SuperBrave\GdprBundle\Manipulator\PropertyManipulator; | ||
|
||
/** | ||
* Class PropertyAnonymizer | ||
* | ||
* @package SuperBrave\GdprBundle\Anonymizer | ||
*/ | ||
class PropertyAnonymizer | ||
{ | ||
/** | ||
* Property manipulator service | ||
* | ||
* @var PropertyManipulator | ||
*/ | ||
private $propertyManipulator; | ||
|
||
/** | ||
* The collection of anonymizers | ||
* | ||
* @var AnonymizerCollection | ||
*/ | ||
private $anonymizerCollection; | ||
|
||
/** | ||
* Constructs the class given the parameters | ||
* | ||
* @param PropertyManipulator $propertyManipulator The PropertyManipulator class used to get the property value | ||
* @param AnonymizerCollection $anonymizerCollection A collection of anonymizers registered by the compiler pass | ||
*/ | ||
public function __construct(PropertyManipulator $propertyManipulator, AnonymizerCollection $anonymizerCollection) | ||
{ | ||
$this->propertyManipulator = $propertyManipulator; | ||
$this->anonymizerCollection = $anonymizerCollection; | ||
} | ||
|
||
/** | ||
* Anonymize the property the annotation is on. | ||
* Takes into account the type specified in the annotation | ||
* | ||
* @param object $object The owner of the property being anonymized | ||
* @param string $property The property being anonymized | ||
* @param Anonymize $annotation The annotation gotten from the object | ||
* | ||
* @return void | ||
*/ | ||
public function anonymizeField($object, $property, Anonymize $annotation) | ||
{ | ||
$anonymizer = $this->anonymizerCollection->getAnonymizer($annotation->type); | ||
|
||
$propertyValue = $this->propertyManipulator->getPropertyValue($object, $property); | ||
|
||
$newPropertyValue = $anonymizer->anonymize($propertyValue, array( | ||
'annotationValue' => $annotation->value, | ||
'object' => $object, | ||
)); | ||
|
||
$this->propertyManipulator->setPropertyValue($object, $property, $newPropertyValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* This file is part of the GDPR bundle. | ||
* | ||
* @category Bundle | ||
* @package Gdpr | ||
* @author SuperBrave <info@superbrave.nl> | ||
* @copyright 2018 SuperBrave <info@superbrave.nl> | ||
* @license https://github.com/superbrave/gdpr-bundle/blob/master/LICENSE MIT | ||
* @link https://www.superbrave.nl/ | ||
*/ | ||
|
||
namespace SuperBrave\GdprBundle\Anonymize\Type; | ||
|
||
/** | ||
* Interface AnonymizerInterface | ||
* | ||
* @package SuperBrave\GdprBundle\Anonymize\Type | ||
*/ | ||
interface AnonymizerInterface | ||
{ | ||
/** | ||
* Anonymizes the given property value. | ||
* | ||
* @param mixed $propertyValue The value of the property. | ||
* @param array $options The options for the anonymizer. | ||
* | ||
* @return mixed | ||
*/ | ||
public function anonymize($propertyValue, array $options = []); | ||
} |
Oops, something went wrong.