-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAnonymizeCustomersNotLoggedBeforeProcessor.php
85 lines (67 loc) · 2.73 KB
/
AnonymizeCustomersNotLoggedBeforeProcessor.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
74
75
76
77
78
79
80
81
82
83
84
85
<?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\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Synolia\SyliusGDPRPlugin\Processor\AnonymizerProcessor;
class AnonymizeCustomersNotLoggedBeforeProcessor implements AdvancedActionsFormDataProcessorInterface
{
private EntityManagerInterface $entityManager;
private AnonymizerProcessor $anonymizerProcessor;
private ParameterBagInterface $parameterBag;
private RequestStack $requestStack;
public function __construct(
EntityManagerInterface $entityManager,
AnonymizerProcessor $anonymizerProcessor,
ParameterBagInterface $parameterBag,
RequestStack $requestStack,
) {
$this->entityManager = $entityManager;
$this->anonymizerProcessor = $anonymizerProcessor;
$this->parameterBag = $parameterBag;
$this->requestStack = $requestStack;
}
/** @inheritdoc */
public function process(string $formTypeClass, FormInterface $form): void
{
/** @var string $shopUser */
$shopUser = $this->parameterBag->get('sylius.model.shop_user.class');
/** @var array $data */
$data = $form->getData();
$shopUsers = $this->entityManager
->createQueryBuilder()
->select('su')
->from($shopUser, 'su')
->where('su.lastLogin < :before')
->setParameter('before', $data['anonymize_customers_not_logged_before_date'])
->getQuery()
->execute()
;
if (!is_array($shopUsers)) {
throw new \LogicException('Error with query.');
}
$this->anonymizerProcessor->anonymizeEntities($this->getCustomersFromShopUsers($shopUsers));
$this->requestStack->getSession()->getFlashBag()->add('success', sprintf('%d customers anonymized.', $this->anonymizerProcessor->getAnonymizedEntityCount()));
}
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\AnonymizeCustomersNotLoggedBeforeType'];
}
}