Browse Source

[Backend] Events / Observers : refactoring

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
1ccc9d0f35
11 changed files with 57 additions and 72 deletions
  1. +9
    -1
      common/config/main.php
  2. +2
    -11
      common/logic/Distribution/Distribution/Model/Distribution.php
  3. +26
    -0
      common/logic/Subscription/Subscription/Event/DistributionObserver.php
  4. +0
    -15
      common/logic/Subscription/Subscription/Event/SubscriptionEventSubscriber.php
  5. +1
    -1
      common/logic/Ticket/Ticket/Model/Ticket.php
  6. +0
    -12
      common/logic/User/CreditHistory/Event/CreditHistoryCreateEvent.php
  7. +0
    -11
      common/logic/User/CreditHistory/Model/CreditHistory.php
  8. +1
    -6
      common/logic/User/CreditHistory/Service/CreditHistoryBuilder.php
  9. +0
    -1
      common/logic/User/User/Service/UserUtils.php
  10. +18
    -0
      common/logic/User/UserProducer/Event/CreditHistoryObserver.php
  11. +0
    -14
      common/logic/User/UserProducer/Event/UserProducerEventSubscriber.php

+ 9
- 1
common/config/main.php View File

@@ -37,7 +37,9 @@
*/

use common\components\BusinessLogic;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Ticket\Ticket\Model\Ticket;
use common\logic\User\CreditHistory\Model\CreditHistory;

$serverName = isset($_SERVER['SERVER_NAME']) ?? '';

@@ -127,9 +129,15 @@ return [
'class' => \justcoded\yii2\eventlistener\components\EventListener::class,
'listeners' => [],
'observers' => [
CreditHistory::class => [
common\logic\User\UserProducer\Event\CreditHistoryObserver::class
],
Distribution::class => [
common\logic\Subscription\Subscription\Event\DistributionObserver::class
],
Ticket::class => [
common\logic\User\User\Event\TicketObserver::class,
]
],
],
],
],

+ 2
- 11
common/logic/Distribution/Distribution/Model/Distribution.php View File

@@ -43,11 +43,11 @@ use common\logic\Distribution\PointSaleDistribution\Model\PointSaleDistribution;
use common\logic\Distribution\ProductDistribution\Model\ProductDistribution;
use common\logic\Order\Order\Model\Order;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Subscription\Subscription\Event\SubscriptionEventSubscriber;
use common\logic\Subscription\Subscription\Event\DistributionObserver;

class Distribution extends ActiveRecordCommon
{
const EVENT_ACTIVE = 'distribution.active';
const EVENT_ACTIVE = 'distribution.event.active';

/**
* @inheritdoc
@@ -81,15 +81,6 @@ class Distribution extends ActiveRecordCommon
];
}

public function init()
{
parent::init();

$this->on(Distribution::EVENT_ACTIVE, function($event) {
SubscriptionEventSubscriber::onActiveDistribution($event->distribution);
});
}

/*
* Relations
*/

+ 26
- 0
common/logic/Subscription/Subscription/Event/DistributionObserver.php View File

@@ -0,0 +1,26 @@
<?php

namespace common\logic\Subscription\Subscription\Event;

use common\logic\Distribution\Distribution\Event\DistributionActiveEvent;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Order\Order\Wrapper\OrderManager;
use justcoded\yii2\eventlistener\observers\Observer;

class DistributionObserver extends Observer
{
public function events()
{
return [
Distribution::EVENT_ACTIVE => 'onDistributionActive'
];
}

public static function onDistributionActive(DistributionActiveEvent $event): void
{
$distribution = $event->distribution;
$orderManager = OrderManager::getInstance();

$orderManager->createAllOrdersFromSubscriptions($distribution->date);
}
}

+ 0
- 15
common/logic/Subscription/Subscription/Event/SubscriptionEventSubscriber.php View File

@@ -1,15 +0,0 @@
<?php

namespace common\logic\Subscription\Subscription\Event;

use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Order\Order\Wrapper\OrderManager;

class SubscriptionEventSubscriber
{
public static function onActiveDistribution(Distribution $distribution): void
{
$orderManager = OrderManager::getInstance();
$orderManager->createAllOrdersFromSubscriptions($distribution->date);
}
}

+ 1
- 1
common/logic/Ticket/Ticket/Model/Ticket.php View File

@@ -42,7 +42,7 @@ use common\components\ActiveRecordCommon;
use common\events\EntityManagerEvent;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Subscription\Subscription\Event\SubscriptionEventSubscriber;
use common\logic\Subscription\Subscription\Event\DistributionObserver;
use common\logic\Ticket\TicketMessage\Model\TicketMessage;
use common\logic\Ticket\TicketUser\Model\TicketUser;
use common\logic\User\User\Model\User;

+ 0
- 12
common/logic/User/CreditHistory/Event/CreditHistoryCreateEvent.php View File

@@ -1,12 +0,0 @@
<?php

namespace common\logic\User\CreditHistory\Event;

use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\User\CreditHistory\Model\CreditHistory;
use yii\base\Event;

class CreditHistoryCreateEvent extends Event
{
public CreditHistory $creditHistory;
}

+ 0
- 11
common/logic/User/CreditHistory/Model/CreditHistory.php View File

@@ -47,8 +47,6 @@ use yii\db\ActiveQuery;

class CreditHistory extends ActiveRecordCommon
{
const EVENT_CREATE = 'creditHistory.create';

const TYPE_INITIAL_CREDIT = 'initial-credit';
const TYPE_CREDIT = 'credit';
const TYPE_PAYMENT = 'payment';
@@ -97,15 +95,6 @@ class CreditHistory extends ActiveRecordCommon
];
}

public function init()
{
$this->on(CreditHistory::EVENT_CREATE, function($event) {
UserProducerEventSubscriber::onCreateCreditHistory($event);
});

parent::init();
}

/*
* Relations
*/

+ 1
- 6
common/logic/User/CreditHistory/Service/CreditHistoryBuilder.php View File

@@ -6,7 +6,6 @@ use common\logic\AbstractBuilder;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Service\OrderSolver;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\User\CreditHistory\Event\CreditHistoryCreateEvent;
use common\logic\User\CreditHistory\Model\CreditHistory;
use common\logic\User\User\Model\User;
use yii\base\Event;
@@ -68,11 +67,7 @@ class CreditHistoryBuilder extends AbstractBuilder

$creditHistory = $this->instanciateCreditHistory($type, $amount, $producer, $user, $userAction, $meanPayment, $order);
$creditHistory->setComment($creditHistory->getComment() . $this->orderSolver->getCreditHistoryComment($creditHistory));
$this->saveCreate($creditHistory);

$creditHistoryCreateEvent = new CreditHistoryCreateEvent();
$creditHistoryCreateEvent->creditHistory = $creditHistory;
$creditHistory->trigger(CreditHistory::EVENT_CREATE, $creditHistoryCreateEvent);
$this->create($creditHistory);

return $creditHistory;
}

+ 0
- 1
common/logic/User/User/Service/UserUtils.php View File

@@ -39,7 +39,6 @@ class UserUtils extends AbstractService implements UtilsInterface
/**
* Envoie un email de bienvenue à l'utilisateur lors de son inscription
* via le backend du site.
*
*/
public function sendMailWelcome(User $user, string $password): void
{

+ 18
- 0
common/logic/User/UserProducer/Event/CreditHistoryObserver.php View File

@@ -0,0 +1,18 @@
<?php

namespace common\logic\User\UserProducer\Event;

use common\logic\User\UserProducer\Wrapper\UserProducerManager;
use justcoded\yii2\eventlistener\observers\ActiveRecordObserver;
use yii\db\AfterSaveEvent;

class CreditHistoryObserver extends ActiveRecordObserver
{
public function inserted(AfterSaveEvent $event)
{
$creditHistory = $event->sender;
$userProducerManager = UserProducerManager::getInstance();

$userProducerManager->updateCredit($creditHistory);
}
}

+ 0
- 14
common/logic/User/UserProducer/Event/UserProducerEventSubscriber.php View File

@@ -1,14 +0,0 @@
<?php

namespace common\logic\User\UserProducer\Event;

use common\logic\User\UserProducer\Wrapper\UserProducerManager;

class UserProducerEventSubscriber
{
public static function onCreateCreditHistory($event)
{
$userProducerManager = UserProducerManager::getInstance();
$userProducerManager->updateCredit($event->creditHistory);
}
}

Loading…
Cancel
Save