Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getAfter example
private
function
hydrateRuleIds
(
array
$fields
, EntityWrittenEvent
$nestedEvent
, array
$ruleIds
)
: array
{
foreach
(
$nestedEvent
->
getWriteResults
(
)
as
$result
)
{
$changeSet
=
$result
->
getChangeSet
(
)
;
$payload
=
$result
->
getPayload
(
)
;
foreach
(
$fields
as
$field
)
{
if
(
$changeSet
&&
$changeSet
->
hasChanged
(
$field
->
getStorageName
(
)
)
)
{
$ruleIds
[
]
=
$changeSet
->
getBefore
(
$field
->
getStorageName
(
)
)
;
$ruleIds
[
]
=
$changeSet
->
getAfter
(
$field
->
getStorageName
(
)
)
;
}
if
(
$changeSet
)
{
continue
;
}
if
(
!
empty
(
$payload
[
$field
->
getPropertyName
(
)
]
)
)
{
$ruleIds
[
]
= Uuid::
fromHexToBytes
(
$payload
[
$field
->
getPropertyName
(
)
]
)
;
}
}
}
$command
->
requestChangeSet
(
)
;
}
}
)
;
$result
=
$this
->productRepository->
update
(
[
$update
]
, Context::
createDefaultContext
(
)
)
;
$changeSet
=
$this
->
getChangeSet
(
ProductDefinition::ENTITY_NAME,
$result
)
;
static
::
assertTrue
(
$changeSet
->
hasChanged
(
'stock'
)
)
;
static
::
assertEquals
(
1,
$changeSet
->
getBefore
(
'stock'
)
)
;
static
::
assertEquals
(
100,
$changeSet
->
getAfter
(
'stock'
)
)
;
}
public
function
testUpdateWithSameValue
(
)
: void
{
$id
=
$this
->ids->
get
(
'product'
)
;
$update
=
[
'id' =>
$id
, 'stock' => 1
]
;
$this
->
getContainer
(
)
->
get
(
'event_dispatcher'
)
->
addListener
(
PreWriteValidationEvent::
class
,
function
DPreWriteValidationEvent
$event
)
: void
{
foreach
(
$event
->
getCommands
(
)
as
$command
)
{
$type
=
$changeSet
->
getBefore
(
'type'
)
;
if
(
$type
!== LineItem::PRODUCT_LINE_ITEM_TYPE
)
{
continue
;
}
if
(
!
$changeSet
->
hasChanged
(
'referenced_id'
)
&& !
$changeSet
->
hasChanged
(
'quantity'
)
)
{
continue
;
}
$ids
[
]
=
$changeSet
->
getBefore
(
'referenced_id'
)
;
$ids
[
]
=
$changeSet
->
getAfter
(
'referenced_id'
)
;
}
/** @var list<string> $ids */
$ids
=
array_filter
(
array_unique
(
$ids
)
)
;
if
(
empty
(
$ids
)
)
{
return
;
}
$this
->
update
(
$ids
,
$event
->
getContext
(
)
)
;
}