Key Insights into
Development Design
Patterns for Magento 2
CTO @ GiftsDirect, TheIrishStore
Max Pronko
Today’s Presentation Includes
• Design Patterns and Magento 2
• Aspect Oriented Programming
• Questions and Answers
Design Patterns
What is Design Pattern?
describes a problem which occurs over and over again,
and then describes the core of the solution to that
problem, without ever doing it the same way twice
“
- Christopher Alexander
Composite pattern is used to treat a group of objects in
similar way as a single object uniformly
Composite Pattern
Composite Diagram
BuilderInterface
Common
implements
AddressCreditCard Composite
1
*
MagentoPaymentGateway
BuilderComposite Example
class BuilderComposite implements BuilderInterface
{
/** @var BuilderInterface[] */
private $builders;
public function __construct($builders) {
$this->builders = $builders;
}
public function build(array $buildSubject) {
$result = [];
foreach ($this->builders as $builder) {
$result = array_merge_recursive($result, $builder->build($buildSubject));
}
return $result;
}
}
CompositeBuilder Declaration
<config>
<virtualType name="CaptureBuilderComposite" type="MagentoPaymentGatewayRequestBuilderComposite">
<arguments>
<argument name="builders" xsi:type="array">
<item name="common" xsi:type="object">PronkoPaymentGatewayCommonBuilder</item>
<item name=“credit_card" xsi:type="object">PronkoPaymentGatewayCreditCardBuilder</item>
<item name="address" xsi:type="object">PronkoPaymentGatewayAddressBuilder</item>
</argument>
</arguments>
</virtualType>
</config>
File: PronkoPaymentetcdi.xml
Strategy lets the algorithm vary independently from the
clients that use it
Strategy Pattern
Strategy Pattern Diagram
BuilderInterface
CaptureBuilder
implements
PartialBuilder
CaptureStrategy
GatewayCommand
implements
calls
uses
MagentoPaymentGateway
Strategy Pattern
class CaptureStrategy implements BuilderInterface {
/** @var BuilderInterface */
protected $partial;
/** @var BuilderInterface */
protected $capture;
public function build(array $buildSubject) {
$condition = //set condition
if ($condition) {
return $this->partial->build($buildSubject);
}
return $this->capture->build($buildSubject);
}
}
Define an interface for creating an object, but let
subclasses decide which class to instantiate. Factory
Method lets a class defer instantiation to subclasses
Factory Method Pattern
Factory Method Pattern Diagram
TransferFactory
implements
TransferFactoryInterfaceGatewayCommand
uses
create()
MagentoPaymentGatewayCommand
Transfer
creates
Transfer Factory Example
namespace MagentoBraintreeGatewayHttp;
class TransferFactory implements TransferFactoryInterface
{
private $transferBuilder;
public function __construct(TransferBuilder $transferBuilder) {
$this->transferBuilder = $transferBuilder;
}
public function create(array $request) {
return $this->transferBuilder
->setBody($request)
->build();
}
}
Transfer Factory Example
namespace MagentoBraintreeGatewayHttp;
class TransferFactory implements TransferFactoryInterface
{
private $transferBuilder;
public function __construct(TransferBuilder $transferBuilder) {
$this->transferBuilder = $transferBuilder;
}
public function create(array $request) {
return $this->transferBuilder
->setBody($request)
->build();
}
}
class GatewayCommand implements CommandInterface {
public function execute(array $commandSubject) {
$transferO = $this->transferFactory->create(
$this->requestBuilder->build($commandSubject)
);
$response = $this->client->placeRequest($transferO)
// … code
}
}
Define a one-to-many dependency between objects so
that when one object changes state, all its dependents
are notified and updated automatically
Observer Pattern
Observer Pattern Diagram
Manager Config
implements
ObserverInterface
ManagerInterfaceAdapter
dispatch()
getObservers()
execute()
events.xml
PaymentObserver
implements
MagentoPayment
Dispatching an event
use MagentoFrameworkEventManagerInterface;
class Adapter implements MethodInterface {
/** @var ManagerInterface */
protected $eventManager;
public function assignData(MagentoFrameworkDataObject $data) {
$this->eventManager->dispatch(
'payment_method_assign_data_' . $this->getCode(),
[
AbstractDataAssignObserver::METHOD_CODE => $this,
AbstractDataAssignObserver::MODEL_CODE => $this->getInfoInstance(),
AbstractDataAssignObserver::DATA_CODE => $data
]
);
return $this;
}
}
Observer Configuration
<config>
<event name=“payment_method_assign_data_custom">
<observer name="custom_gateway_data_assign" instance="PronkoPaymentObserverDataAssignObserver" />
</event>
</config>
File: PronkoPaymentetcevents.xml
Observer Implementation
namespace PronkoPaymentObserver;
use MagentoFrameworkEventObserver;
use MagentoPaymentObserverAbstractDataAssignObserver;
class DataAssignObserver extends AbstractDataAssignObserver
{
public function execute(Observer $observer)
{
$data = $this->readDataArgument($observer);
$additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
$payment = $observer->getPaymentModel();
$payment->setCcLast4(substr($additionalData->getData('cc_number'), -4));
}
}
Typical implementation of Dependency Manager. Enables
Inversion of Control support for Magento 2
Object Manager
Object Manager
• Dependency Manager
• Instantiates classes
• Creates objects
• Provides shared objects pool
• Supports lazy Initialisation
• __construct() method injection
Object Manager Implementation
Singleton
Builder
Abstract Factory
Factory Method
Decorator
Value Object
Composite
Strategy
CQRS
Dependency Injection
…
Object Manager Diagram
ObjectManagerInterface
ObjectManager
App/ObjectManager
ObjectManagerFactory App/Bootstrap
[Object]
uses
creates
configures
uses
[Object]
[Object]
MagentoFrameworkObjectManager
Object Manager Usage
Good
• Factory
• Builder
• Proxy
• Application
• generated classes
Bad
• Data Objects
• Business Objects
• Action Controllers
• Mage::getModel like calls
• Blocks
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for
extending functionality
Decorator Pattern
Decorator Pattern Diagram
FactoryInterface
DynamicProduction
implements
AbstractFactory
DynamicDeveloper
ProfilerFactoryDecorator
1
1
MagentoFrameworkObjectManager
Compiled
extends
namespace MagentoFrameworkAppObjectManagerEnvironment;
abstract class AbstractEnvironment implements EnvironmentInterface {
protected function decorate($arguments){
if (isset($arguments['MAGE_PROFILER']) && $arguments['MAGE_PROFILER'] == 2) {
$this->factory = new FactoryDecorator(
$this->factory,
Log::getInstance()
);
}
}
}
Decorator: Profiler Factory
namespace MagentoFrameworkAppObjectManagerEnvironment;
abstract class AbstractEnvironment implements EnvironmentInterface {
protected function decorate($arguments){
if (isset($arguments['MAGE_PROFILER']) && $arguments['MAGE_PROFILER'] == 2) {
$this->factory = new FactoryDecorator(
$this->factory,
Log::getInstance()
);
}
}
}
Decorator: Profiler Factory
class DecoratorFactory implements FactoryInterface {}
class CompiledFactory implements FactoryInterface {}
A proxy is a class functioning as an interface to something
else. It is used for resource consuming objects
Proxy Pattern
Proxy Pattern Diagram
NoninterceptableInterface
AreaList/Proxy
MagentoFrameworkApp
AreaList
implements
extends
Proxy Pattern Declaration
<config>
<type name="MagentoFrameworkConfigScope">
<arguments>
<argument name="areaList" xsi:type="object">MagentoFrameworkAppAreaListProxy</argument>
</arguments>
</type>
</config>
File: app/etc/di.xml
Aspect Oriented Programming
a programming paradigm that aims to increase modularity
by allowing the separation of cross-cutting concerns. The
goal is to achieve loose coupling
Aspect Oriented Programming
AOP Cross Cutting Concerns
Security
Logging
Monitoring
Catalog Sales
Checkout …
Magento AOP Example
Client
PluginPlugin Target
Interceptor
Plugin
Invoke Method
Result
Interception Pipeline
ObjectManager
Result
Invoke
get Result
Types of a Plugin
Method
Before After
Around
Affects input
method argument
Modifies behaviour
Affects method
return value
Check Module Status Plugin
After
Around
namespace MagentoFrameworkModulePlugin;
class DbStatusValidator
{
public function aroundDispatch(
MagentoFrameworkAppFrontController $subject,
Closure $proceed,
MagentoFrameworkAppRequestInterface $request
) {
if (!$this->cache->load('db_is_up_to_date')) {
$errors = $this->dbVersionInfo->getDbVersionErrors();
if ($errors) {
// throw exception
} else {
$this->cache->save('true', 'db_is_up_to_date');
}
}
return $proceed($request);
}
}
Invalidate Cache Plugin
After
Around
namespace MagentoWebapiSecurityModelPlugin;
class CacheInvalidator
{
public function afterAfterSave(
MagentoFrameworkAppConfigValue $subject,
MagentoFrameworkAppConfigValue $result
) {
if (condition) {
$this->cacheTypeList->invalidate(MagentoWebapiModelCacheTypeWebapi::TYPE_IDENTIFIER);
}
return $result;
}
}
Password Security Check Plugin
After
Around
namespace MagentoSecurityModelPlugin;
class AccountManagement
{
public function beforeInitiatePasswordReset(
AccountManagementOriginal $accountManagement,
$email,
$template,
$websiteId = null
) {
$this->securityManager->performSecurityCheck(
PasswordResetRequestEvent::CUSTOMER_PASSWORD_RESET_REQUEST,
$email
);
return [$email, $template, $websiteId];
}
}
Declaring Plugin
After
File: MagentoSecurityetcdi.xml
<config>
<type name="MagentoCustomerModelAccountManagement">
<plugin name="security_check_customer_password_reset_attempt" type="MagentoSecurityModelPluginAccountManag
</type>
</config>
Interception
Limitations
• Final methods / classes
• Static class and __construct() methods
• Virtual types
Benefits
• Public methods
• Auto-generated Decorators
• Flexible approach
• NoninterceptableInterface
Summary
• Solve problems using Design Patterns
• Don’t overuse Design Patterns
• Build your code on abstractions (interfaces)
• Look inside Magento 2 for good practices
Ask Me Anything
Thank you
www.maxpronko.com
max_pronko

Key Insights into Development Design Patterns for Magento 2 - Magento Live UK

  • 2.
    Key Insights into DevelopmentDesign Patterns for Magento 2
  • 3.
    CTO @ GiftsDirect,TheIrishStore Max Pronko
  • 4.
    Today’s Presentation Includes •Design Patterns and Magento 2 • Aspect Oriented Programming • Questions and Answers
  • 5.
  • 6.
    What is DesignPattern? describes a problem which occurs over and over again, and then describes the core of the solution to that problem, without ever doing it the same way twice “ - Christopher Alexander
  • 7.
    Composite pattern isused to treat a group of objects in similar way as a single object uniformly Composite Pattern
  • 8.
  • 9.
    BuilderComposite Example class BuilderCompositeimplements BuilderInterface { /** @var BuilderInterface[] */ private $builders; public function __construct($builders) { $this->builders = $builders; } public function build(array $buildSubject) { $result = []; foreach ($this->builders as $builder) { $result = array_merge_recursive($result, $builder->build($buildSubject)); } return $result; } }
  • 10.
    CompositeBuilder Declaration <config> <virtualType name="CaptureBuilderComposite"type="MagentoPaymentGatewayRequestBuilderComposite"> <arguments> <argument name="builders" xsi:type="array"> <item name="common" xsi:type="object">PronkoPaymentGatewayCommonBuilder</item> <item name=“credit_card" xsi:type="object">PronkoPaymentGatewayCreditCardBuilder</item> <item name="address" xsi:type="object">PronkoPaymentGatewayAddressBuilder</item> </argument> </arguments> </virtualType> </config> File: PronkoPaymentetcdi.xml
  • 11.
    Strategy lets thealgorithm vary independently from the clients that use it Strategy Pattern
  • 12.
  • 13.
    Strategy Pattern class CaptureStrategyimplements BuilderInterface { /** @var BuilderInterface */ protected $partial; /** @var BuilderInterface */ protected $capture; public function build(array $buildSubject) { $condition = //set condition if ($condition) { return $this->partial->build($buildSubject); } return $this->capture->build($buildSubject); } }
  • 14.
    Define an interfacefor creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses Factory Method Pattern
  • 15.
    Factory Method PatternDiagram TransferFactory implements TransferFactoryInterfaceGatewayCommand uses create() MagentoPaymentGatewayCommand Transfer creates
  • 16.
    Transfer Factory Example namespaceMagentoBraintreeGatewayHttp; class TransferFactory implements TransferFactoryInterface { private $transferBuilder; public function __construct(TransferBuilder $transferBuilder) { $this->transferBuilder = $transferBuilder; } public function create(array $request) { return $this->transferBuilder ->setBody($request) ->build(); } }
  • 17.
    Transfer Factory Example namespaceMagentoBraintreeGatewayHttp; class TransferFactory implements TransferFactoryInterface { private $transferBuilder; public function __construct(TransferBuilder $transferBuilder) { $this->transferBuilder = $transferBuilder; } public function create(array $request) { return $this->transferBuilder ->setBody($request) ->build(); } } class GatewayCommand implements CommandInterface { public function execute(array $commandSubject) { $transferO = $this->transferFactory->create( $this->requestBuilder->build($commandSubject) ); $response = $this->client->placeRequest($transferO) // … code } }
  • 18.
    Define a one-to-manydependency between objects so that when one object changes state, all its dependents are notified and updated automatically Observer Pattern
  • 19.
    Observer Pattern Diagram ManagerConfig implements ObserverInterface ManagerInterfaceAdapter dispatch() getObservers() execute() events.xml PaymentObserver implements MagentoPayment
  • 20.
    Dispatching an event useMagentoFrameworkEventManagerInterface; class Adapter implements MethodInterface { /** @var ManagerInterface */ protected $eventManager; public function assignData(MagentoFrameworkDataObject $data) { $this->eventManager->dispatch( 'payment_method_assign_data_' . $this->getCode(), [ AbstractDataAssignObserver::METHOD_CODE => $this, AbstractDataAssignObserver::MODEL_CODE => $this->getInfoInstance(), AbstractDataAssignObserver::DATA_CODE => $data ] ); return $this; } }
  • 21.
    Observer Configuration <config> <event name=“payment_method_assign_data_custom"> <observername="custom_gateway_data_assign" instance="PronkoPaymentObserverDataAssignObserver" /> </event> </config> File: PronkoPaymentetcevents.xml
  • 22.
    Observer Implementation namespace PronkoPaymentObserver; useMagentoFrameworkEventObserver; use MagentoPaymentObserverAbstractDataAssignObserver; class DataAssignObserver extends AbstractDataAssignObserver { public function execute(Observer $observer) { $data = $this->readDataArgument($observer); $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA); $payment = $observer->getPaymentModel(); $payment->setCcLast4(substr($additionalData->getData('cc_number'), -4)); } }
  • 23.
    Typical implementation ofDependency Manager. Enables Inversion of Control support for Magento 2 Object Manager
  • 24.
    Object Manager • DependencyManager • Instantiates classes • Creates objects • Provides shared objects pool • Supports lazy Initialisation • __construct() method injection
  • 25.
    Object Manager Implementation Singleton Builder AbstractFactory Factory Method Decorator Value Object Composite Strategy CQRS Dependency Injection …
  • 26.
    Object Manager Diagram ObjectManagerInterface ObjectManager App/ObjectManager ObjectManagerFactoryApp/Bootstrap [Object] uses creates configures uses [Object] [Object] MagentoFrameworkObjectManager
  • 27.
    Object Manager Usage Good •Factory • Builder • Proxy • Application • generated classes Bad • Data Objects • Business Objects • Action Controllers • Mage::getModel like calls • Blocks
  • 28.
    Attach additional responsibilitiesto an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality Decorator Pattern
  • 29.
  • 30.
    namespace MagentoFrameworkAppObjectManagerEnvironment; abstract classAbstractEnvironment implements EnvironmentInterface { protected function decorate($arguments){ if (isset($arguments['MAGE_PROFILER']) && $arguments['MAGE_PROFILER'] == 2) { $this->factory = new FactoryDecorator( $this->factory, Log::getInstance() ); } } } Decorator: Profiler Factory
  • 31.
    namespace MagentoFrameworkAppObjectManagerEnvironment; abstract classAbstractEnvironment implements EnvironmentInterface { protected function decorate($arguments){ if (isset($arguments['MAGE_PROFILER']) && $arguments['MAGE_PROFILER'] == 2) { $this->factory = new FactoryDecorator( $this->factory, Log::getInstance() ); } } } Decorator: Profiler Factory class DecoratorFactory implements FactoryInterface {} class CompiledFactory implements FactoryInterface {}
  • 32.
    A proxy isa class functioning as an interface to something else. It is used for resource consuming objects Proxy Pattern
  • 33.
  • 34.
    Proxy Pattern Declaration <config> <typename="MagentoFrameworkConfigScope"> <arguments> <argument name="areaList" xsi:type="object">MagentoFrameworkAppAreaListProxy</argument> </arguments> </type> </config> File: app/etc/di.xml
  • 35.
  • 36.
    a programming paradigmthat aims to increase modularity by allowing the separation of cross-cutting concerns. The goal is to achieve loose coupling Aspect Oriented Programming
  • 37.
    AOP Cross CuttingConcerns Security Logging Monitoring Catalog Sales Checkout …
  • 38.
    Magento AOP Example Client PluginPluginTarget Interceptor Plugin Invoke Method Result Interception Pipeline ObjectManager Result Invoke get Result
  • 39.
    Types of aPlugin Method Before After Around Affects input method argument Modifies behaviour Affects method return value
  • 40.
    Check Module StatusPlugin After Around namespace MagentoFrameworkModulePlugin; class DbStatusValidator { public function aroundDispatch( MagentoFrameworkAppFrontController $subject, Closure $proceed, MagentoFrameworkAppRequestInterface $request ) { if (!$this->cache->load('db_is_up_to_date')) { $errors = $this->dbVersionInfo->getDbVersionErrors(); if ($errors) { // throw exception } else { $this->cache->save('true', 'db_is_up_to_date'); } } return $proceed($request); } }
  • 41.
    Invalidate Cache Plugin After Around namespaceMagentoWebapiSecurityModelPlugin; class CacheInvalidator { public function afterAfterSave( MagentoFrameworkAppConfigValue $subject, MagentoFrameworkAppConfigValue $result ) { if (condition) { $this->cacheTypeList->invalidate(MagentoWebapiModelCacheTypeWebapi::TYPE_IDENTIFIER); } return $result; } }
  • 42.
    Password Security CheckPlugin After Around namespace MagentoSecurityModelPlugin; class AccountManagement { public function beforeInitiatePasswordReset( AccountManagementOriginal $accountManagement, $email, $template, $websiteId = null ) { $this->securityManager->performSecurityCheck( PasswordResetRequestEvent::CUSTOMER_PASSWORD_RESET_REQUEST, $email ); return [$email, $template, $websiteId]; } }
  • 43.
    Declaring Plugin After File: MagentoSecurityetcdi.xml <config> <typename="MagentoCustomerModelAccountManagement"> <plugin name="security_check_customer_password_reset_attempt" type="MagentoSecurityModelPluginAccountManag </type> </config>
  • 44.
    Interception Limitations • Final methods/ classes • Static class and __construct() methods • Virtual types Benefits • Public methods • Auto-generated Decorators • Flexible approach • NoninterceptableInterface
  • 45.
    Summary • Solve problemsusing Design Patterns • Don’t overuse Design Patterns • Build your code on abstractions (interfaces) • Look inside Magento 2 for good practices
  • 46.
    Ask Me Anything Thankyou www.maxpronko.com max_pronko

Editor's Notes

  • #7 Big changes compare to M1, everything become possible Solid architectural goals - flexibility, upgradability - time to market - scalable - service contracts - unit simplicity
  • #8 smalltalk book, remember almost nothing.
  • #11 overview of the class - Builds payment gateway request - capture or auth builders array is a builder interface as a result - prepares key value ready to convert to xml/dom/soap request
  • #12 Di.xml config virtual type! - idea Single responsibility later added to transport factory
  • #14 Magento 2 only capture request (no partial) CaptureStrategy to allow send partial request
  • #20 In Magento it is not always enough events
  • #37 auto-generated code
  • #38 Object Manager enables Proxy support for all objects Instantiates object only during first method call Auto-generated class Extends Original class Implements Magento\Framework\ObjectManager\NoninterceptableInterface interface Proxies all calls to original class
  • #41 Additional behavior to existing code (an advice) without modifying the code itself Separately specifying - "log all function calls when the function's name begins with 'set'". This allows behaviors that are not central to the business logic to be added to a program without cluttering the code core to the functionality. AOP forms a basis for aspect-oriented software development.