forked from opengento/magento2-gdpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEraseEntityChecker.php
76 lines (64 loc) · 2.55 KB
/
EraseEntityChecker.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
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);
namespace Opengento\Gdpr\Model;
use Magento\Framework\Exception\NoSuchEntityException;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Api\EraseEntityCheckerInterface;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Opengento\Gdpr\Model\Entity\EntityCheckerFactory;
final class EraseEntityChecker implements EraseEntityCheckerInterface
{
private EraseEntityRepositoryInterface $eraseRepository;
/**
* @var EntityCheckerFactory
*/
private EntityCheckerFactory $entityCheckerFactory;
public function __construct(
EraseEntityRepositoryInterface $eraseRepository,
EntityCheckerFactory $entityCheckerFactory
) {
$this->eraseRepository = $eraseRepository;
$this->entityCheckerFactory = $entityCheckerFactory;
}
public function exists(int $entityId, string $entityType): bool
{
try {
return (bool) $this->eraseRepository->getByEntity($entityId, $entityType)->getEraseId();
} catch (NoSuchEntityException $e) {
return false;
}
}
public function canCreate(int $entityId, string $entityType): bool
{
$entityChecker = $this->entityCheckerFactory->get($entityType);
return !$this->exists($entityId, $entityType) && $entityChecker->canErase($entityId);
}
public function canCancel(int $entityId, string $entityType): bool
{
try {
$entity = $this->eraseRepository->getByEntity($entityId, $entityType);
} catch (NoSuchEntityException $e) {
return false;
}
return $entity->getState() === EraseEntityInterface::STATE_PENDING
&& $entity->getStatus() === EraseEntityInterface::STATUS_READY;
}
public function canProcess(int $entityId, string $entityType): bool
{
try {
$entity = $this->eraseRepository->getByEntity($entityId, $entityType);
} catch (NoSuchEntityException $e) {
return false;
}
$entityChecker = $this->entityCheckerFactory->get($entityType);
return (($entity->getState() === EraseEntityInterface::STATE_PENDING
&& $entity->getStatus() === EraseEntityInterface::STATUS_READY)
|| ($entity->getState() === EraseEntityInterface::STATE_PROCESSING
&& $entity->getStatus() === EraseEntityInterface::STATUS_FAILED))
&& $entityChecker->canErase($entityId);
}
}