Skip to content

Commit

Permalink
Merge branch 'next-32255/remove-definition-from-write-command' into '…
Browse files Browse the repository at this point in the history
…trunk'

NEXT-32255 - Remove definition from WriteCommand

See merge request shopware/6/product/platform!13513
  • Loading branch information
jozsefdamokos committed Apr 26, 2024
2 parents e90e79b + eb50530 commit c67ed5b
Show file tree
Hide file tree
Showing 54 changed files with 1,104 additions and 693 deletions.
@@ -0,0 +1,10 @@
---
title: Remove definition from WriteCommand
issue: NEXT-32255
author: Jozsef Damokos
author_email: j.damokos@shopware.com
author_github: jozsefdamokos
---
# Core
* Removed `$definition` parameter from `WriteCommand`. This is replaced by `$entityName` parameter.
* Deprecated `getDefinition()` method from `WriteCommand` and its extension classes.
60 changes: 0 additions & 60 deletions phpstan-baseline.neon
Expand Up @@ -815,16 +815,6 @@ parameters:
count: 1
path: src/Core/Content/ImportExport/DataAbstractionLayer/Serializer/SerializerRegistry.php

-
message: "#^Method Shopware\\\\Core\\\\Content\\\\ImportExport\\\\DataAbstractionLayer\\\\SystemDefaultValidator\\:\\:filterSystemDefaults\\(\\) has parameter \\$ids with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Content/ImportExport/DataAbstractionLayer/SystemDefaultValidator.php

-
message: "#^Method Shopware\\\\Core\\\\Content\\\\ImportExport\\\\DataAbstractionLayer\\\\SystemDefaultValidator\\:\\:filterSystemDefaults\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Content/ImportExport/DataAbstractionLayer/SystemDefaultValidator.php

-
message: "#^Method Shopware\\\\Core\\\\Content\\\\ImportExport\\\\Event\\\\ImportExportAfterImportRecordEvent\\:\\:__construct\\(\\) has parameter \\$record with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -4905,56 +4895,6 @@ parameters:
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/LockValidatorTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PostWriteValidationEventTest\\:\\:delete\\(\\) has parameter \\$primaryKey with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PostWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PostWriteValidationEventTest\\:\\:insert\\(\\) has parameter \\$primaryKey with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PostWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PostWriteValidationEventTest\\:\\:testGetDeletedPrimaryKeys\\(\\) has parameter \\$assertions with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PostWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PostWriteValidationEventTest\\:\\:testGetDeletedPrimaryKeysProvider\\(\\) has parameter \\$assertions with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PostWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PostWriteValidationEventTest\\:\\:testGetPrimaryKeys\\(\\) has parameter \\$assertions with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PostWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PreWriteValidationEventTest\\:\\:delete\\(\\) has parameter \\$primaryKey with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PreWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PreWriteValidationEventTest\\:\\:insert\\(\\) has parameter \\$primaryKey with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PreWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PreWriteValidationEventTest\\:\\:testGetDeletedPrimaryKeys\\(\\) has parameter \\$assertions with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PreWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PreWriteValidationEventTest\\:\\:testGetDeletedPrimaryKeysProvider\\(\\) has parameter \\$assertions with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PreWriteValidationEventTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\DataAbstractionLayer\\\\Write\\\\Validation\\\\PreWriteValidationEventTest\\:\\:testGetPrimaryKeys\\(\\) has parameter \\$assertions with no value type specified in iterable type array\\.$#"
count: 1
path: src/Core/Framework/Test/DataAbstractionLayer/Write/Validation/PreWriteValidationEventTest.php

-
message: "#^Cannot call method get\\(\\) on Shopware\\\\Core\\\\Framework\\\\Struct\\\\ArrayEntity\\|null\\.$#"
count: 4
Expand Down
Expand Up @@ -61,7 +61,7 @@ public function deleteOrder(PreWriteValidationEvent $event): void

$orderIds = [];
foreach ($event->getCommands() as $command) {
if ($command->getDefinition()->getClass() === OrderDefinition::class
if ($command->getEntityName() === OrderDefinition::ENTITY_NAME
&& $command instanceof DeleteCommand
) {
$orderIds[] = Uuid::fromBytesToHex($command->getPrimaryKey()['id']);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public function detectChangeset(EntityDeleteEvent $event): void
continue;
}

if ($command->getDefinition()->getEntityName() !== ProductReviewDefinition::ENTITY_NAME) {
if ($command->getEntityName() !== ProductReviewDefinition::ENTITY_NAME) {
continue;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Core/Checkout/Promotion/Validator/PromotionValidator.php
Expand Up @@ -79,8 +79,8 @@ public function preValidate(PreWriteValidationEvent $event): void
continue;
}

switch ($command->getDefinition()::class) {
case PromotionDefinition::class:
switch ($command->getEntityName()) {
case PromotionDefinition::ENTITY_NAME:
$promotionId = $command->getPrimaryKey()['id'];

try {
Expand All @@ -98,7 +98,7 @@ public function preValidate(PreWriteValidationEvent $event): void

break;

case PromotionDiscountDefinition::class:
case PromotionDiscountDefinition::ENTITY_NAME:
$discountId = $command->getPrimaryKey()['id'];

try {
Expand Down Expand Up @@ -142,13 +142,13 @@ private function collect(array $writeCommands): void
continue;
}

switch ($command->getDefinition()::class) {
case PromotionDefinition::class:
switch ($command->getEntityName()) {
case PromotionDefinition::ENTITY_NAME:
$promotionIds[] = $command->getPrimaryKey()['id'];

break;

case PromotionDiscountDefinition::class:
case PromotionDiscountDefinition::ENTITY_NAME:
$discountIds[] = $command->getPrimaryKey()['id'];

break;
Expand Down
Expand Up @@ -59,7 +59,7 @@ public function preValidate(PreWriteValidationEvent $event): void
continue;
}

if ($command->getDefinition()->getClass() !== ShippingMethodDefinition::class) {
if ($command->getEntityName() !== ShippingMethodDefinition::ENTITY_NAME) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Core/Content/Category/Validation/EntryPointValidator.php
Expand Up @@ -51,7 +51,7 @@ public function postValidate(PostWriteValidationEvent $event): void
continue;
}

if ($command->getDefinition()->getClass() !== CategoryDefinition::class) {
if ($command->getEntityName() !== CategoryDefinition::ENTITY_NAME) {
continue;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ private function checkTypeChange(WriteCommand $command, PostWriteValidationEvent
private function isCategoryEntryPoint(string $categoryId, PostWriteValidationEvent $event): bool
{
foreach ($event->getCommands() as $salesChannelCommand) {
if ($salesChannelCommand->getDefinition()->getClass() !== SalesChannelDefinition::class) {
if ($salesChannelCommand->getEntityName() !== SalesChannelDefinition::ENTITY_NAME) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Core/Content/DependencyInjection/rule.xml
Expand Up @@ -58,6 +58,7 @@
<argument type="service" id="Shopware\Core\Content\Rule\RuleDefinition"/>
<argument type="service" id="Shopware\Core\Framework\Rule\Collector\RuleConditionRegistry"/>
<argument type="service" id="Shopware\Core\Framework\Adapter\Cache\CacheInvalidator"/>
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
Expand Down
Expand Up @@ -39,7 +39,7 @@ public function preValidate(PreWriteValidationEvent $event): void
$writeCommands = $event->getCommands();

foreach ($writeCommands as $command) {
if ($command->getDefinition()->getClass() === ImportExportProfileDefinition::class
if ($command->getEntityName() === ImportExportProfileDefinition::ENTITY_NAME
&& $command instanceof DeleteCommand
) {
$ids[] = $command->getPrimaryKey()['id'];
Expand All @@ -52,6 +52,11 @@ public function preValidate(PreWriteValidationEvent $event): void
}
}

/**
* @param string[] $ids
*
* @return string[]
*/
private function filterSystemDefaults(array $ids): array
{
if (empty($ids)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Content/ImportExport/ImportExport.php
Expand Up @@ -683,7 +683,7 @@ private function logResults(
continue;
}

$entityName = $writeCommand->getDefinition()->getEntityName();
$entityName = $writeCommand->getEntityName();
$entityResult = $overallResults[$entityName] ?? $defaultTemplate;
$operation = $writeCommand->getEntityExistence()->exists()
? EntityWriteResult::OPERATION_UPDATE
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Content/LandingPage/LandingPageValidator.php
Expand Up @@ -43,7 +43,7 @@ public function preValidate(PostWriteValidationEvent $event): void
$violationList = new ConstraintViolationList();

foreach ($commands as $command) {
if (!($command instanceof InsertCommand) || $command->getDefinition()->getClass() !== LandingPageDefinition::class) {
if (!($command instanceof InsertCommand) || $command->getEntityName() !== LandingPageDefinition::ENTITY_NAME) {
continue;
}

Expand All @@ -66,7 +66,7 @@ private function hasAnotherValidCommand(array $commands, WriteCommand $command):
{
$isValid = false;
foreach ($commands as $searchCommand) {
if ($searchCommand->getDefinition()->getClass() === LandingPageSalesChannelDefinition::class && $searchCommand instanceof InsertCommand) {
if ($searchCommand->getEntityName() === LandingPageSalesChannelDefinition::ENTITY_NAME && $searchCommand instanceof InsertCommand) {
$searchPrimaryKey = $searchCommand->getPrimaryKey();
$searchLandingPageId = $searchPrimaryKey['landing_page_id'] ?? null;

Expand Down
Expand Up @@ -49,7 +49,7 @@ public function preValidate(PreWriteValidationEvent $event): void
$products = $this->findProducts($event->getCommands());

foreach ($event->getCommands() as $command) {
if ($command->getDefinition()->getClass() !== OrderLineItemDefinition::class) {
if ($command->getEntityName() !== OrderLineItemDefinition::ENTITY_NAME) {
continue;
}
if ($command instanceof SetNullOnDeleteCommand) {
Expand Down Expand Up @@ -108,7 +108,7 @@ public function preValidate(PreWriteValidationEvent $event): void
private function findProducts(array $commands): array
{
$ids = array_map(function (WriteCommand $command) {
if ($command->getDefinition()->getClass() !== OrderLineItemDefinition::class) {
if ($command->getEntityName() !== OrderLineItemDefinition::ENTITY_NAME) {
return null;
}

Expand Down
Expand Up @@ -10,6 +10,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\CompiledFieldCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\EntityDefinitionQueryHelper;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\QueryBuilder;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
Expand Down Expand Up @@ -43,7 +44,8 @@ public function __construct(
private readonly Connection $connection,
private readonly RuleDefinition $definition,
private readonly RuleConditionRegistry $conditionRegistry,
private readonly CacheInvalidator $cacheInvalidator
private readonly CacheInvalidator $cacheInvalidator,
private readonly DefinitionInstanceRegistry $definitionRegistry
) {
}

Expand All @@ -60,8 +62,8 @@ public function triggerChangeSet(PreWriteValidationEvent $event): void
$associatedEntities = $this->getAssociationEntities();

foreach ($event->getCommands() as $command) {
$definition = $command->getDefinition();
$entity = $definition->getEntityName();
$entity = $command->getEntityName();
$definition = $this->definitionRegistry->getByEntityName($entity);

if (!$command instanceof ChangeSetAware || !\in_array($entity, $associatedEntities, true)) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Content/Rule/RuleValidator.php
Expand Up @@ -63,7 +63,7 @@ public function preValidate(PreWriteValidationEvent $event): void
$updateQueue = [];

foreach ($commands as $command) {
if ($command->getDefinition()->getClass() !== RuleConditionDefinition::class) {
if ($command->getEntityName() !== RuleConditionDefinition::ENTITY_NAME) {
continue;
}

Expand Down
15 changes: 10 additions & 5 deletions src/Core/Framework/Api/Acl/AclWriteValidator.php
Expand Up @@ -8,6 +8,7 @@
use Shopware\Core\Framework\Api\Context\AdminApiSource;
use Shopware\Core\Framework\Api\Context\AdminSalesChannelApiSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\EntityTranslationDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
Expand All @@ -25,8 +26,10 @@ class AclWriteValidator implements EventSubscriberInterface
/**
* @internal
*/
public function __construct(private readonly EventDispatcherInterface $eventDispatcher)
{
public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
private readonly DefinitionInstanceRegistry $definitionRegistry
) {
}

/**
Expand Down Expand Up @@ -54,15 +57,17 @@ public function preValidate(PreWriteValidationEvent $event): void
$missingPrivileges = [];

foreach ($commands as $command) {
$resource = $command->getDefinition()->getEntityName();
$resource = $command->getEntityName();
$privilege = $command->getPrivilege();

if ($privilege === null) {
continue;
}

if (is_subclass_of($command->getDefinition(), EntityTranslationDefinition::class)) {
$resource = $command->getDefinition()->getParentDefinition()->getEntityName();
$definition = $this->definitionRegistry->getByEntityName($command->getEntityName());

if (is_subclass_of($definition, EntityTranslationDefinition::class)) {
$resource = $definition->getParentDefinition()->getEntityName();

if ($privilege !== AclRoleDefinition::PRIVILEGE_DELETE) {
$privilege = $this->getPrivilegeForParentWriteOperation($command, $commands);
Expand Down
Expand Up @@ -28,7 +28,7 @@ public static function getSubscribedEvents(): array
public function validate(CommandAclValidationEvent $event): void
{
$command = $event->getCommand();
$resource = $command->getDefinition()->getEntityName();
$resource = $command->getEntityName();
$privilege = $command->getPrivilege();

if ($privilege !== AclRoleDefinition::PRIVILEGE_CREATE || $resource !== OrderLineItemDefinition::ENTITY_NAME) {
Expand Down
Expand Up @@ -52,9 +52,7 @@ public function checkWrite(PreWriteValidationEvent $event): void
$violationList = new ConstraintViolationList();

foreach ($event->getCommands() as $command) {
if (!($command->getDefinition() instanceof CustomFieldSetDefinition)
|| $command instanceof InsertCommand
) {
if ($command->getEntityName() !== CustomFieldSetDefinition::ENTITY_NAME || $command instanceof InsertCommand) {
continue;
}

Expand Down Expand Up @@ -105,7 +103,7 @@ private function addViolation(ConstraintViolationList $violationList, WriteComma
$this->buildViolation(
'No permissions to %privilege%".',
['%privilege%' => 'write:custom_field_set'],
'/' . $command->getDefinition()->getEntityName(),
'/' . $command->getEntityName(),
self::VIOLATION_NO_PERMISSION
)
);
Expand Down

0 comments on commit c67ed5b

Please sign in to comment.