Skip to content

Commit

Permalink
NEXT-25364 - Implement new scalar value flow storer for simply data s…
Browse files Browse the repository at this point in the history
…tore logics in flows
  • Loading branch information
OliverSkroblin committed Mar 23, 2023
1 parent 666394f commit c606529
Show file tree
Hide file tree
Showing 88 changed files with 1,541 additions and 65 deletions.
87 changes: 87 additions & 0 deletions adr/2023-02-02-flow-storer-with-scalar-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: Flow storer with scalar values
date: 2023-02-02
area: core
tags: [flow, storer, scalar, deprecation]
---

## Context
At the moment we have a bunch of different `FlowStorer` implementations. Most of them are used to store scalar values without any restore logic. Each of the Storer class has an own interface which is used to identify if the data of the event should be stored. This leads to much boilerplate code when adding new storer implementations or when plugins want to bypass some for events.

## Decision

We introduce a generic `ScalarValuesAware` interface which can be used to store simple values which should be simply stored and restored one to one:

```php
interface ScalarValuesAware
{
public const STORE_VALUES = 'scalar_values';
/** @return array<string, scalar|null|array> */
public function getValues(): array;
}
```

This event can be used in different events which needs a simple storage logic:

```php
class SomeFlowAwareEvent extends Event implements ScalarStoreAware, FlowEventAware
{
public function __construct(private readonly string $url) { }
public function getValues(): array
{
return ['url' => $this->url];
}
}
```

To store and restore this data, we provide a simple `FlowStorer` implementation:

```php
class ScalarValuesStorer extends FlowStorer
{
public function store(FlowEventAware $event, array $stored): array
{
if (!$event instanceof ScalarValuesAware) return $stored
$stored[ScalarValuesAware::STORE_VALUES] = $event->getValues();
return $stored;
}
public function restore(StorableFlow $storable): void
{
$values = $storable->getStore(ScalarValuesAware::STORE_VALUES);
foreach ($values as $key => $value) {
$storable->setData($key, $value);
}
}
}
```

## Consequences
- It is no more necessary to implement storer classes to just store and restore scalar values.
- We deprecate all current `Aware` interface and `Storer` classes which can simply replaced by this new implementation
- Following storer and interfaces will be deprecated:
- ConfirmUrlStorer > ConfirmUrlAware
- ContactFormDataStorer > ContactFormDataAware
- ContentsStorer > ContentsAware
- ContextTokenStorer > ContextTokenAware
- DataStorer > DataAware
- EmailStorer > Email Aware
- MailStorer > Mail Aware
- NameStorer > NameAware
- RecipientsStorer > RecipientsAware
- ResetUrlStorer > ResetUrlAware
- ReviewFormDataStorer > ReviewFormDataAware
- ShopNameStorer > ShopNameAware
- SubjectStorer > SubjectAware
- TemplateDataStorer > TemplateDataAware
- UrlStorer > UrlAware
- Affected events will be updated to use the new `ScalarStoreAware` interface.
- Existing `*Aware` events will stay in the event implementation and will be marked as deprecated.
- Developers can much easier store and restore values without providing a lot of boilerplate code.
- Deprecated classes will be removed in v6.6.0.0
- All interface and storer logic will remain until the next major and has to be compatible with each other
123 changes: 123 additions & 0 deletions changelog/_unreleased/2023-03-16-minify-flow-storer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Minify flow storer
issue: NEXT-25364
author: Oliver Skroblin
author_email: o.skroblin@shopware.com
---
# Core
* Added new `ScalarValuesAware` and `ScalarValuesStorer` class, which allows to store scalar values for flows.
* Deprecated diverse flow storer classes and interfaces in favor of the new `ScalarValuesStorer` class:
* `ConfirmUrlAware`
* `ContactFormDataAware`
* `ContentsAware`
* `ContextTokenAware`
* `DataAware`
* `EmailAware`
* `MediaUploadedAware`
* `NameAware`
* `RecipientsAware`
* `ResetUrlAware`
* `ReviewFormDataAware`
* `ScalarStoreAware`
* `ShopNameAware`
* `SubjectAware`
* `TemplateDataAware`
* `UrlAware`
* `ConfirmUrlStorer`
* `ContactFormDataStorer`
* `ContentsStorer`
* `ContextTokenStorer`
* `DataStorer`
* `EmailStorer`
* `NameStorer`
* `RecipientsStorer`
* `ResetUrlStorer`
* `ReviewFormDataStorer`
* `ScalarFlowStorer`
* `ScalarValuesStorer`
* `ShopNameStorer`
* `SubjectStorer`
* `TemplateDataStorer`
* `UrlStorer`
* Replaced deprecated flow storer interfaces in the flow event with the new `ScalarValuesAware` interface in the following events:
* `CustomerAccountRecoverRequestEvent`
* `CustomerBeforeLoginEvent`
* `CustomerDoubleOptInRegistrationEvent`
* `CustomerLoginEvent`
* `DoubleOptInGuestOrderEvent`
* `ContactFormEvent`
* `MailBeforeSentEvent`
* `MailBeforeValidateEvent`
* `MailErrorEvent`
* `MailSentEvent`
* `MediaUploadedEvent`
* `NewsletterRegisterEvent`
* `ReviewFormEvent`
* `ProductExportLoggingEvent`
* `UserRecoveryRequestEvent`
___
# Upgrade Information
## Deprecated diverse flow storer classes and interfaces in favor of the new `ScalarValuesStorer` class
We deprecated diverse flow storer interfaces in favor of the new `ScalarValuesAware` class. The new `ScalarValuesAware` class allows to store scalar values much easier for flows without implementing own storer and interface classes.
If you implemented one of the deprecated interfaces or implemented an own interface and storer class to store simple values, you should replace it with the new `ScalarValuesAware` class.

```php

// before
class MyEvent extends Event implements \Shopware\Core\Content\Flow\Dispatching\Aware\UrlAware
{
private string $url;

public function __construct(string $url)
{
$this->url = $url;
}

public function getUrl(): string
{
return $this->url;
}

// ...
}

// after

class MyEvent extends Event implements \Shopware\Core\Content\Flow\Dispatching\Aware\ScalarValuesAware
{
private string $url;

public function __construct(string $url)
{
$this->url = $url;
}

public function getScalarValues(): array
{
return [
'url' => $this->url,
// ...
];
}

// ...
}
```

The deprecated flow storer interfaces are:
* `ConfirmUrlAware`
* `ContactFormDataAware`
* `ContentsAware`
* `ContextTokenAware`
* `DataAware`
* `EmailAware`
* `MediaUploadedAware`
* `NameAware`
* `RecipientsAware`
* `ResetUrlAware`
* `ReviewFormDataAware`
* `ScalarStoreAware`
* `ShopNameAware`
* `SubjectAware`
* `TemplateDataAware`
* `UrlAware`
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6111,11 +6111,6 @@ parameters:
count: 1
path: src/Core/DevOps/Locust/setup.php

-
message: "#^Strict comparison using \\=\\=\\= between PHPStan\\\\Reflection\\\\ClassReflection and null will always evaluate to false\\.$#"
count: 1
path: src/Core/DevOps/StaticAnalyze/PHPStan/Rules/Deprecation/DeprecatedMethodsThrowDeprecationRule.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Adapter\\\\Asset\\\\FallbackUrlPackage\\:\\:applyFallback\\(\\) has parameter \\$baseUrls with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Flow\Dispatching\Aware\CustomerRecoveryAware;
use Shopware\Core\Content\Flow\Dispatching\Aware\ResetUrlAware;
use Shopware\Core\Content\Flow\Dispatching\Aware\ScalarValuesAware;
use Shopware\Core\Content\Flow\Dispatching\Aware\ShopNameAware;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\CustomerAware;
Expand All @@ -22,8 +23,11 @@
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @deprecated tag:v6.6.0 - reason:backward-compatibility - ResetUrlAware and ShopNameAware are deprecated and will be removed in v6.6.0
*/
#[Package('customer-order')]
class CustomerAccountRecoverRequestEvent extends Event implements SalesChannelAware, ShopwareSalesChannelEvent, CustomerAware, MailAware, CustomerRecoveryAware, ResetUrlAware, ShopNameAware
class CustomerAccountRecoverRequestEvent extends Event implements SalesChannelAware, ShopwareSalesChannelEvent, CustomerAware, MailAware, CustomerRecoveryAware, ResetUrlAware, ShopNameAware, ScalarValuesAware
{
public const EVENT_NAME = 'customer.recovery.request';

Expand Down Expand Up @@ -63,6 +67,17 @@ public function __construct(
$this->shopName = $salesChannelContext->getSalesChannel()->getTranslation('name');
}

/**
* @return array<string, scalar|array<mixed>|null>
*/
public function getValues(): array
{
return [
'resetUrl' => $this->resetUrl,
'shopName' => $this->shopName,
];
}

public function getName(): string
{
return self::EVENT_NAME;
Expand Down
16 changes: 15 additions & 1 deletion src/Core/Checkout/Customer/Event/CustomerBeforeLoginEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Shopware\Core\Checkout\Customer\Event;

use Shopware\Core\Content\Flow\Dispatching\Aware\EmailAware;
use Shopware\Core\Content\Flow\Dispatching\Aware\ScalarValuesAware;
use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
Expand All @@ -15,8 +16,11 @@
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @deprecated tag:v6.6.0 - reason:backward-compatibility - EmailAware is deprecated and will be removed in v6.6.0
*/
#[Package('customer-order')]
class CustomerBeforeLoginEvent extends Event implements SalesChannelAware, ShopwareSalesChannelEvent, MailAware, EmailAware
class CustomerBeforeLoginEvent extends Event implements SalesChannelAware, ShopwareSalesChannelEvent, MailAware, EmailAware, ScalarValuesAware
{
final public const EVENT_NAME = 'checkout.customer.before.login';

Expand All @@ -26,6 +30,16 @@ public function __construct(
) {
}

/**
* @return array<string, scalar|array<mixed>|null>
*/
public function getValues(): array
{
return [
'email' => $this->email,
];
}

public function getName(): string
{
return self::EVENT_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Flow\Dispatching\Aware\ConfirmUrlAware;
use Shopware\Core\Content\Flow\Dispatching\Aware\ScalarValuesAware;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\CustomerAware;
use Shopware\Core\Framework\Event\EventData\EntityType;
Expand All @@ -17,8 +18,11 @@
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @deprecated tag:v6.6.0 - reason:backward-compatibility - ConfirmUrlAware is deprecated and will be removed in v6.6.0
*/
#[Package('customer-order')]
class CustomerDoubleOptInRegistrationEvent extends Event implements SalesChannelAware, CustomerAware, MailAware, ConfirmUrlAware
class CustomerDoubleOptInRegistrationEvent extends Event implements SalesChannelAware, CustomerAware, MailAware, ConfirmUrlAware, ScalarValuesAware
{
final public const EVENT_NAME = 'checkout.customer.double_opt_in_registration';

Expand Down Expand Up @@ -54,6 +58,14 @@ public function getMailStruct(): MailRecipientStruct
return $this->mailRecipientStruct;
}

/**
* @return array<string, scalar|array<mixed>|null>
*/
public function getValues(): array
{
return ['confirmUrl' => $this->confirmUrl];
}

public function getCustomer(): CustomerEntity
{
return $this->customer;
Expand Down
16 changes: 15 additions & 1 deletion src/Core/Checkout/Customer/Event/CustomerLoginEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Flow\Dispatching\Aware\ContextTokenAware;
use Shopware\Core\Content\Flow\Dispatching\Aware\ScalarValuesAware;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\CustomerAware;
use Shopware\Core\Framework\Event\EventData\EntityType;
Expand All @@ -18,8 +19,11 @@
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @deprecated tag:v6.6.0 - reason:backward-compatibility - ContextTokenAware is deprecated and will be removed in v6.6.0
*/
#[Package('customer-order')]
class CustomerLoginEvent extends Event implements SalesChannelAware, ShopwareSalesChannelEvent, CustomerAware, MailAware, ContextTokenAware
class CustomerLoginEvent extends Event implements SalesChannelAware, ShopwareSalesChannelEvent, CustomerAware, MailAware, ContextTokenAware, ScalarValuesAware
{
final public const EVENT_NAME = 'checkout.customer.login';

Expand All @@ -30,6 +34,16 @@ public function __construct(
) {
}

/**
* @return array<string, scalar|array<mixed>|null>
*/
public function getValues(): array
{
return [
'contextToken' => $this->contextToken,
];
}

public function getName(): string
{
return self::EVENT_NAME;
Expand Down
Loading

0 comments on commit c606529

Please sign in to comment.