forked from synolia/SyliusGDPRPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnonymizeCustomerNotLoggedBeforeProcessor.php
73 lines (57 loc) · 2.18 KB
/
AnonymizeCustomerNotLoggedBeforeProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace Synolia\SyliusGDPRPlugin\Processor\AdvancedActions;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\Form\FormInterface;
use Synolia\SyliusGDPRPlugin\Processor\AnonymizerProcessor;
class AnonymizeCustomerNotLoggedBeforeProcessor implements AdvancedActionsFormDataProcessorInterface
{
private EntityManagerInterface $entityManager;
private AnonymizerProcessor $anonymizerProcessor;
public function __construct(EntityManagerInterface $entityManager, AnonymizerProcessor $anonymizerProcessor)
{
$this->entityManager = $entityManager;
$this->anonymizerProcessor = $anonymizerProcessor;
}
/** @inheritdoc */
public function process(string $formTypeClass, FormInterface $form): void
{
/** @var array $formData */
$formData = $form->getData();
/** @var \DateTime $before */
$before = $formData['before_date'];
$beforeClean = new \DateTime($before->format('Y-m-d'));
$shopUsers = $this->entityManager
->createQueryBuilder()
->select('su')
->from(ShopUserInterface::class, 'su')
->where('su.lastLogin < :before')
->setParameter('before', $beforeClean)
->getQuery()
->execute()
;
if (!is_array($shopUsers)) {
throw new \LogicException('Error with query.');
}
$this->anonymizerProcessor->anonymizeEntities($this->getCustomersFromShopUsers($shopUsers));
}
private function getCustomersFromShopUsers(array $shopUsers): array
{
$customers = [];
/** @var ShopUserInterface $shopUser */
foreach ($shopUsers as $shopUser) {
$customer = $shopUser->getCustomer();
if (!$customer instanceof CustomerInterface) {
continue;
}
$customers[] = $customer;
}
return $customers;
}
public function getFormTypesClass(): array
{
return ['Synolia\SyliusGDPRPlugin\Form\Type\Actions\AnonymizeCustomerNotLoggedBeforeType'];
}
}