Skip to content

Commit 41784af

Browse files
committed
Fix nullable properties for config source components
1 parent 9350f2f commit 41784af

10 files changed

+87
-224
lines changed

Cron/EraseEntity.php

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,15 @@
2525
*/
2626
class EraseEntity
2727
{
28-
private LoggerInterface $logger;
29-
30-
private Config $config;
31-
32-
private Registry $registry;
33-
34-
private EraseEntityManagementInterface $eraseManagement;
35-
36-
private EraseEntityRepositoryInterface $eraseRepository;
37-
38-
private SearchCriteriaBuilder $criteriaBuilder;
39-
40-
/**
41-
* @var DateTime
42-
*/
43-
private DateTime $dateTime;
44-
4528
public function __construct(
46-
LoggerInterface $logger,
47-
Config $config,
48-
Registry $registry,
49-
EraseEntityManagementInterface $eraseManagement,
50-
EraseEntityRepositoryInterface $eraseRepository,
51-
SearchCriteriaBuilder $criteriaBuilder,
52-
DateTime $dateTime
53-
) {
54-
$this->logger = $logger;
55-
$this->config = $config;
56-
$this->registry = $registry;
57-
$this->eraseManagement = $eraseManagement;
58-
$this->eraseRepository = $eraseRepository;
59-
$this->criteriaBuilder = $criteriaBuilder;
60-
$this->dateTime = $dateTime;
61-
}
29+
private LoggerInterface $logger,
30+
private Config $config,
31+
private Registry $registry,
32+
private EraseEntityManagementInterface $eraseManagement,
33+
private EraseEntityRepositoryInterface $eraseRepository,
34+
private SearchCriteriaBuilder $criteriaBuilder,
35+
private DateTime $dateTime
36+
) {}
6237

6338
public function execute(): void
6439
{
@@ -101,7 +76,7 @@ private function retrieveEraseEntityList(): SearchResultsInterface
10176

10277
try {
10378
$eraseCustomerList = $this->eraseRepository->getList($this->criteriaBuilder->create());
104-
} catch (LocalizedException $e) {
79+
} catch (LocalizedException) {
10580
$eraseCustomerList = [];
10681
}
10782

Cron/EraseEntityScheduler.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,14 @@ class EraseEntityScheduler
2626
{
2727
private const CONFIG_PATH_ERASURE_MAX_AGE = 'gdpr/erasure/entity_max_age';
2828

29-
private LoggerInterface $logger;
30-
31-
private ScopeConfigInterface $scopeConfig;
32-
33-
private Config $config;
34-
35-
/**
36-
* @var EraseEntitySchedulerService
37-
*/
38-
private EraseEntitySchedulerService $eraseEntityScheduler;
39-
40-
private FilterBuilder $filterBuilder;
41-
42-
private EntityTypeList $entityTypeList;
43-
4429
public function __construct(
45-
LoggerInterface $logger,
46-
ScopeConfigInterface $scopeConfig,
47-
Config $config,
48-
EraseEntitySchedulerService $eraseEntityScheduler,
49-
FilterBuilder $filterBuilder,
50-
EntityTypeList $entityTypeList
30+
private LoggerInterface $logger,
31+
private ScopeConfigInterface $scopeConfig,
32+
private Config $config,
33+
private EraseEntitySchedulerService $eraseEntityScheduler,
34+
private FilterBuilder $filterBuilder,
35+
private EntityTypeList $entityTypeList
5136
) {
52-
$this->logger = $logger;
53-
$this->scopeConfig = $scopeConfig;
54-
$this->config = $config;
55-
$this->eraseEntityScheduler = $eraseEntityScheduler;
56-
$this->filterBuilder = $filterBuilder;
57-
$this->entityTypeList = $entityTypeList;
5837
}
5938

6039
public function execute(): void

Cron/ExportEntity.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,13 @@
2121
*/
2222
class ExportEntity
2323
{
24-
private LoggerInterface $logger;
25-
26-
private Config $config;
27-
28-
private ExportEntityRepositoryInterface $exportRepository;
29-
30-
private ExportEntityManagementInterface $exportManagement;
31-
32-
private SearchCriteriaBuilder $criteriaBuilder;
33-
3424
public function __construct(
35-
LoggerInterface $logger,
36-
Config $config,
37-
ExportEntityRepositoryInterface $exportRepository,
38-
ExportEntityManagementInterface $exportManagement,
39-
SearchCriteriaBuilder $criteriaBuilder
40-
) {
41-
$this->logger = $logger;
42-
$this->config = $config;
43-
$this->exportRepository = $exportRepository;
44-
$this->exportManagement = $exportManagement;
45-
$this->criteriaBuilder = $criteriaBuilder;
46-
}
25+
private LoggerInterface $logger,
26+
private Config $config,
27+
private ExportEntityRepositoryInterface $exportRepository,
28+
private ExportEntityManagementInterface $exportManagement,
29+
private SearchCriteriaBuilder $criteriaBuilder
30+
) {}
4731

4832
public function execute(): void
4933
{

Cron/ExportEntityExpired.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,12 @@
2020
*/
2121
class ExportEntityExpired
2222
{
23-
private LoggerInterface $logger;
24-
25-
private Config $config;
26-
27-
private ExportEntityRepositoryInterface $exportRepository;
28-
29-
private SearchCriteriaBuilder $criteriaBuilder;
30-
3123
public function __construct(
32-
LoggerInterface $logger,
33-
Config $config,
34-
ExportEntityRepositoryInterface $exportRepository,
35-
SearchCriteriaBuilder $criteriaBuilder
36-
) {
37-
$this->logger = $logger;
38-
$this->config = $config;
39-
$this->exportRepository = $exportRepository;
40-
$this->criteriaBuilder = $criteriaBuilder;
41-
}
24+
private LoggerInterface $logger,
25+
private Config $config,
26+
private ExportEntityRepositoryInterface $exportRepository,
27+
private SearchCriteriaBuilder $criteriaBuilder
28+
) {}
4229

4330
public function execute(): void
4431
{

Model/Config/Source/EraseComponents.php

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,46 @@
1212
use Magento\Framework\Phrase;
1313

1414
use function array_keys;
15+
use function array_map;
1516
use function array_merge;
1617

1718
class EraseComponents implements OptionSourceInterface
1819
{
19-
/**
20-
* @var ConfigInterface
21-
*/
22-
private ConfigInterface $objectManagerConfig;
23-
24-
/**
25-
* Class must be an instance of `\Opengento\Gdpr\Service\Erase\ProcessorResolverFactory`
26-
*
27-
* @var string
28-
*/
29-
private string $factoryClassName;
30-
31-
/**
32-
* @var string[][]
33-
*/
34-
private array $options = [];
20+
private ?array $options = null;
3521

3622
public function __construct(
37-
ConfigInterface $objectManagerConfig,
38-
string $factoryClassName
39-
) {
40-
$this->objectManagerConfig = $objectManagerConfig;
41-
$this->factoryClassName = $factoryClassName;
42-
}
23+
private ConfigInterface $objectManagerConfig,
24+
private string $factoryClassName
25+
) {}
4326

4427
public function toOptionArray(): array
4528
{
46-
if (!$this->options) {
47-
foreach ($this->retrieveDelegateProcessors() as $delegateProcessor) {
48-
$this->options[] = ['value' => $delegateProcessor, 'label' => new Phrase($delegateProcessor)];
49-
}
50-
}
51-
52-
return $this->options;
29+
return $this->options ??= array_map(
30+
static fn (string $delegateProcessor): array => [
31+
'value' => $delegateProcessor,
32+
'label' => new Phrase($delegateProcessor)
33+
],
34+
$this->retrieveDelegateProcessors()
35+
);
5336
}
5437

5538
/**
5639
* @return string[]
5740
*/
5841
private function retrieveDelegateProcessors(): array
5942
{
60-
$delegateProcessors = [];
61-
/** @var string[] $resolvers */
62-
$resolvers = $this->retrieveArgument($this->factoryClassName, 'processorResolvers', []);
63-
64-
foreach ($resolvers as $resolver) {
65-
$delegateProcessors[] = $this->retrieveArgument($resolver, 'processors');
66-
}
67-
68-
return array_keys(array_merge([], ...$delegateProcessors));
43+
return array_keys(
44+
array_merge(
45+
[],
46+
...array_map(
47+
fn (string $resolver): mixed => $this->retrieveArgument($resolver, 'processors'),
48+
$this->retrieveArgument($this->factoryClassName, 'processorResolvers', [])
49+
)
50+
)
51+
);
6952
}
7053

71-
/**
72-
* Retrieve a construct argument value of a class
73-
*
74-
* @param string $className
75-
* @param string $argumentName
76-
* @param mixed $defaultValue
77-
* @return mixed
78-
*/
79-
private function retrieveArgument(string $className, string $argumentName, $defaultValue = null)
54+
private function retrieveArgument(string $className, string $argumentName, mixed $defaultValue = null): mixed
8055
{
8156
$arguments = $this->objectManagerConfig->getArguments(
8257
$this->objectManagerConfig->getPreference($className)

Model/Config/Source/OrderPendingStates.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,9 @@
1212

1313
class OrderPendingStates implements OptionSourceInterface
1414
{
15-
private CollectionFactory $collectionFactory;
15+
private ?array $options = null;
1616

17-
/**
18-
* @var string[][]
19-
*/
20-
private array $options;
21-
22-
public function __construct(
23-
CollectionFactory $collectionFactory
24-
) {
25-
$this->collectionFactory = $collectionFactory;
26-
}
17+
public function __construct(private CollectionFactory $collectionFactory) {}
2718

2819
public function toOptionArray(): array
2920
{

Model/Config/Source/VirtualArrayArgumentList.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,24 @@
1212
use Magento\Framework\Phrase;
1313

1414
use function array_keys;
15+
use function array_map;
1516

1617
class VirtualArrayArgumentList implements OptionSourceInterface
1718
{
18-
/**
19-
* @var ConfigInterface
20-
*/
21-
private ConfigInterface $objectManagerConfig;
22-
23-
private string $className;
24-
25-
private string $argumentName;
26-
27-
private array $options = [];
19+
private ?array $options = null;
2820

2921
public function __construct(
30-
ConfigInterface $objectManagerConfig,
31-
string $className,
32-
string $argumentName
33-
) {
34-
$this->objectManagerConfig = $objectManagerConfig;
35-
$this->className = $className;
36-
$this->argumentName = $argumentName;
37-
}
22+
private ConfigInterface $objectManagerConfig,
23+
private string $className,
24+
private string $argumentName
25+
) {}
3826

3927
public function toOptionArray(): array
4028
{
41-
if (!$this->options) {
42-
foreach (array_keys($this->retrieveItems()) as $item) {
43-
$this->options[] = ['value' => $item, 'label' => new Phrase($item)];
44-
}
45-
}
46-
47-
return $this->options;
29+
return $this->options ??= array_map(
30+
static fn (string $item): array => ['value' => $item, 'label' => new Phrase($item)],
31+
array_keys($this->retrieveItems())
32+
);
4833
}
4934

5035
/**

Model/Config/Source/VirtualCustomerAttributes.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,36 @@
77

88
namespace Opengento\Gdpr\Model\Config\Source;
99

10+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1011
use Magento\Customer\Api\MetadataInterface;
1112
use Magento\Framework\Data\OptionSourceInterface;
1213
use Magento\Framework\Exception\LocalizedException;
1314

15+
use function array_map;
16+
1417
class VirtualCustomerAttributes implements OptionSourceInterface
1518
{
16-
private MetadataInterface $metadata;
17-
18-
private array $options;
19+
private ?array $options = null;
1920

20-
public function __construct(
21-
MetadataInterface $metadata
22-
) {
23-
$this->metadata = $metadata;
24-
$this->options = [];
25-
}
21+
public function __construct(private MetadataInterface $metadata) {}
2622

2723
public function toOptionArray(): array
2824
{
29-
if (!$this->options) {
30-
try {
31-
$attributes = $this->metadata->getAllAttributesMetadata();
32-
} catch (LocalizedException $e) {
33-
$attributes = [];
34-
}
25+
return $this->options ??= array_map(
26+
static fn (AttributeMetadataInterface $attributeMetadata): array => [
27+
'value' => $attributeMetadata->getAttributeCode(),
28+
'label' => $attributeMetadata->getFrontendLabel(),
29+
],
30+
$this->resolveAttributeMetadataList()
31+
);
32+
}
3533

36-
foreach ($attributes as $attribute) {
37-
$this->options[] = [
38-
'value' => $attribute->getAttributeCode(),
39-
'label' => $attribute->getFrontendLabel(),
40-
];
41-
}
34+
private function resolveAttributeMetadataList(): array
35+
{
36+
try {
37+
return $this->metadata->getAllAttributesMetadata();
38+
} catch (LocalizedException) {
39+
return [];
4240
}
43-
44-
return $this->options;
4541
}
4642
}

0 commit comments

Comments
 (0)