Skip to content

Commit

Permalink
Phpcs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Aug 4, 2024
1 parent 91730b5 commit 3b7dec5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mess-detector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: extdn/github-actions-m2/magento-mess-detector/8.2@master
- uses: extdn/github-actions-m2/magento-mess-detector/@master
17 changes: 10 additions & 7 deletions Model/Customer/Anonymize/Processor/CustomerDataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
use Opengento\Gdpr\Model\Customer\OrigDataRegistry;
use Opengento\Gdpr\Service\Anonymize\AnonymizerInterface;
use Opengento\Gdpr\Service\Erase\ProcessorInterface;
use Random\RandomException;

use function mt_rand;
use function random_int;
use function sha1;
use function uniqid;

Expand All @@ -50,18 +51,18 @@ public function __construct(
) {}

/**
* @inheritdoc
* @throws LocalizedException
* @throws RandomException
*/
public function execute(int $customerId): bool
public function execute(int $entityId): bool
{
try {
$this->processCustomerData($customerId);
} catch (NoSuchEntityException $e) {
$this->processCustomerData($entityId);
} catch (NoSuchEntityException) {
return false;
}

$this->sessionCleaner->clearFor($customerId);
$this->sessionCleaner->clearFor($entityId);

return true;
}
Expand All @@ -71,6 +72,7 @@ public function execute(int $customerId): bool
* @throws InputMismatchException
* @throws LocalizedException
* @throws NoSuchEntityException
* @throws RandomException
*/
private function processCustomerData(int $customerId): void
{
Expand Down Expand Up @@ -98,6 +100,7 @@ private function fetchOrdersList(CustomerInterface $customer): OrderSearchResult
* @throws NoSuchEntityException
* @throws InputException
* @throws InputMismatchException
* @throws RandomException
*/
private function anonymizeCustomer(CustomerInterface $customer): void
{
Expand All @@ -106,7 +109,7 @@ private function anonymizeCustomer(CustomerInterface $customer): void
$secureData = $this->customerRegistry->retrieveSecureData($customer->getId());
$dateTime = (new DateTime())->setTimestamp(PHP_INT_MAX);
$secureData->setData('lock_expires', $dateTime->format(DateTimeFormat::DATETIME_PHP_FORMAT));
$secureData->setPasswordHash(sha1(uniqid((string)mt_rand(), true)));
$secureData->setPasswordHash(sha1(uniqid((string)random_int(), true)));

$customer = $this->anonymizer->anonymize($customer);
if ($customer instanceof DataObject) {
Expand Down
31 changes: 10 additions & 21 deletions Service/Anonymize/Anonymizer/Anonymous.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,24 @@

class Anonymous implements AnonymizerInterface
{
private const PHRASE = '%1Anonymous%2';
private const PREFIX_LENGTH = 3;
private const SUFFIX_LENGTH = 2;
public function __construct(private Random $mathRandom) {}

/**
* @var Random
* @throws LocalizedException
*/
private Random $mathRandom;

public function __construct(
Random $mathRandom
) {
$this->mathRandom = $mathRandom;
public function anonymize($value): ?string
{
return $value ? $this->createPhrase()->render() : null;
}

/**
* @inheritdoc
* @throws LocalizedException
*/
public function anonymize($value): ?string
private function createPhrase(): Phrase
{
return $value
? (new Phrase(
self::PHRASE,
[
$this->mathRandom->getRandomString(self::PREFIX_LENGTH),
$this->mathRandom->getRandomString(self::SUFFIX_LENGTH),
]
))->render()
: null;
return new Phrase(
'%1Anonymous%2',
[$this->mathRandom->getRandomString(3), $this->mathRandom->getRandomString(2)]
);
}
}
34 changes: 13 additions & 21 deletions Service/Anonymize/Anonymizer/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,27 @@

class Email implements AnonymizerInterface
{
private const PHRASE = '%1-anonymous-%[email protected]';
private const PREFIX_LENGTH = 3;
private const SUFFIX_LENGTH = 2;
public function __construct(private Random $mathRandom) {}

/**
* @var Random
* @throws LocalizedException
*/
private Random $mathRandom;

public function __construct(
Random $mathRandom
) {
$this->mathRandom = $mathRandom;
public function anonymize($value): ?string
{
return $value ? $this->createPhrase()->render() : null;
}

/**
* @inheritdoc
* @throws LocalizedException
*/
public function anonymize($value): ?string
private function createPhrase(): Phrase
{
return $value
? (new Phrase(
self::PHRASE,
[
$this->mathRandom->getRandomString(self::PREFIX_LENGTH, Random::CHARS_LOWERS),
$this->mathRandom->getRandomString(self::SUFFIX_LENGTH, Random::CHARS_LOWERS),
]
))->render()
: null;
return new Phrase(
'%1-anonymous-%[email protected]',
[
$this->mathRandom->getRandomString(3, Random::CHARS_LOWERS),
$this->mathRandom->getRandomString(2, Random::CHARS_LOWERS)
]
);
}
}
37 changes: 16 additions & 21 deletions Service/Anonymize/Anonymizer/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Exception;
use InvalidArgumentException;
use Magento\Framework\EntityManager\HydratorInterface;
use Magento\Framework\EntityManager\HydratorPool;
use Magento\Framework\EntityManager\TypeResolver;
use Opengento\Gdpr\Model\Entity\DataCollectorInterface;
Expand All @@ -20,37 +21,31 @@

class Entity implements AnonymizerInterface
{
private DataCollectorInterface $dataCollector;

private TypeResolver $typeResolver;

private HydratorPool $hydratorPool;

public function __construct(
DataCollectorInterface $dataCollector,
TypeResolver $typeResolver,
HydratorPool $hydratorPool
) {
$this->dataCollector = $dataCollector;
$this->typeResolver = $typeResolver;
$this->hydratorPool = $hydratorPool;
}
private DataCollectorInterface $dataCollector,
private TypeResolver $typeResolver,
private HydratorPool $hydratorPool
) {}

/**
* @inheritdoc
* @throws Exception
*/
public function anonymize($entity): object
public function anonymize($value): object
{
if (!is_object($entity)) {
if (!is_object($value)) {
throw new InvalidArgumentException(
sprintf('Argument "$entity" must be an object, type "%s" given.', gettype($entity))
sprintf('Argument "$entity" must be an object, type "%s" given.', gettype($value))

Check warning on line 37 in Service/Anonymize/Anonymizer/Entity.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

The use of function gettype() is discouraged
);
}

$hydrator = $this->hydratorPool->getHydrator($this->typeResolver->resolve($entity));
$hydrator->hydrate($entity, $this->dataCollector->collect($entity));
return $this->resolveHydrator($value)->hydrate($value, $this->dataCollector->collect($value));
}

return $entity;
/**
* @throws Exception
*/
private function resolveHydrator(object $entity): HydratorInterface
{
return $this->hydratorPool->getHydrator($this->typeResolver->resolve($entity));
}
}

0 comments on commit 3b7dec5

Please sign in to comment.