-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added update events for customer, category and product
- Loading branch information
Franck Allimant
committed
Nov 15, 2023
1 parent
a63a66f
commit aedf6d9
Showing
8 changed files
with
191 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Thelia package. | ||
* http://www.thelia.net | ||
* | ||
* (c) OpenStudio <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/* web : https://www.openstudio.fr */ | ||
|
||
/* For the full copyright and license information, please view the LICENSE */ | ||
/* file that was distributed with this source code. */ | ||
|
||
/** | ||
* Created by Franck Allimant, OpenStudio <[email protected]> | ||
* Projet: thelia25 | ||
* Date: 15/11/2023. | ||
*/ | ||
|
||
namespace Brevo\Event; | ||
|
||
use Thelia\Core\Event\ActionEvent; | ||
use Thelia\Model\Category; | ||
|
||
class BrevoCategoryUpdateEvent extends ActionEvent | ||
{ | ||
public function __construct(protected Category $category) | ||
{ | ||
} | ||
|
||
public function getCategory(): Category | ||
{ | ||
return $this->category; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Thelia package. | ||
* http://www.thelia.net | ||
* | ||
* (c) OpenStudio <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/* web : https://www.openstudio.fr */ | ||
|
||
/* For the full copyright and license information, please view the LICENSE */ | ||
/* file that was distributed with this source code. */ | ||
|
||
/** | ||
* Created by Franck Allimant, OpenStudio <[email protected]> | ||
* Projet: thelia25 | ||
* Date: 15/11/2023. | ||
*/ | ||
|
||
namespace Brevo\Event; | ||
|
||
use Thelia\Core\Event\ActionEvent; | ||
use Thelia\Model\Customer; | ||
|
||
class BrevoCustomerUpdateEvent extends ActionEvent | ||
{ | ||
public function __construct(protected Customer $customer) | ||
{ | ||
} | ||
|
||
public function getCustomer(): Customer | ||
{ | ||
return $this->customer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/*************************************************************************************/ | ||
/* Copyright (c) OpenStudio */ | ||
/* web : https://www.openstudio.fr */ | ||
/* */ | ||
/* For the full copyright and license information, please view the LICENSE */ | ||
/* file that was distributed with this source code. */ | ||
/*************************************************************************************/ | ||
|
||
/** | ||
* Created by Franck Allimant, OpenStudio <[email protected]> | ||
* Projet: thelia25 | ||
* Date: 15/11/2023 | ||
*/ | ||
|
||
namespace Brevo\Event; | ||
|
||
class BrevoEvents | ||
{ | ||
public const UPDATE_CUSTOMER = 'brevo.UPDATE_CUSTOMER'; | ||
public const UPDATE_PRODUCT = 'brevo.UPDATE_PRODUCT'; | ||
public const UPDATE_CATEGORY = 'brevo.UPDATE_CATEGORY'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Thelia package. | ||
* http://www.thelia.net | ||
* | ||
* (c) OpenStudio <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/* web : https://www.openstudio.fr */ | ||
|
||
/* For the full copyright and license information, please view the LICENSE */ | ||
/* file that was distributed with this source code. */ | ||
|
||
/** | ||
* Created by Franck Allimant, OpenStudio <[email protected]> | ||
* Projet: thelia25 | ||
* Date: 15/11/2023. | ||
*/ | ||
|
||
namespace Brevo\Event; | ||
|
||
use Thelia\Core\Event\ActionEvent; | ||
use Thelia\Model\Product; | ||
|
||
class BrevoProductUpdateEvent extends ActionEvent | ||
{ | ||
public function __construct(protected Product $product) | ||
{ | ||
} | ||
|
||
public function getProduct(): Product | ||
{ | ||
return $this->product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Thelia package. | ||
* http://www.thelia.net | ||
* | ||
* (c) OpenStudio <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brevo\EventListeners; | ||
|
||
use Brevo\Event\BrevoCategoryUpdateEvent; | ||
use Brevo\Event\BrevoEvents; | ||
use Brevo\Event\BrevoProductUpdateEvent; | ||
use Brevo\Services\BrevoCategoryService; | ||
use Brevo\Services\BrevoProductService; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
@@ -10,24 +23,17 @@ | |
use Thelia\Core\Event\Product\ProductCreateEvent; | ||
use Thelia\Core\Event\Product\ProductUpdateEvent; | ||
use Thelia\Core\Event\TheliaEvents; | ||
use Thelia\Model\Category; | ||
use Thelia\Model\Country; | ||
use Thelia\Model\CountryQuery; | ||
use Thelia\Model\Currency; | ||
use Thelia\Model\CurrencyQuery; | ||
use Thelia\Model\Event\CategoryEvent; | ||
use Thelia\Model\Event\ProductEvent; | ||
use Thelia\Model\Lang; | ||
use Thelia\Model\LangQuery; | ||
|
||
class BrevoUpdateListener implements EventSubscriberInterface | ||
{ | ||
|
||
public function __construct( | ||
private BrevoCategoryService $brevoCategoryService, | ||
private BrevoProductService $brevoProductService | ||
) | ||
{ | ||
|
||
) { | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
|
@@ -37,32 +43,36 @@ public static function getSubscribedEvents() | |
TheliaEvents::PRODUCT_UPDATE => ['updateProduct', 100], | ||
TheliaEvents::CATEGORY_CREATE => ['createCategory', 100], | ||
TheliaEvents::CATEGORY_UPDATE => ['updateCategory', 100], | ||
|
||
BrevoEvents::UPDATE_CATEGORY => ['updateCategory', 128], | ||
BrevoEvents::UPDATE_PRODUCT => ['updateProduct', 128], | ||
]; | ||
} | ||
|
||
public function updateProduct(ProductUpdateEvent $event) | ||
|
||
public function updateProduct(ProductUpdateEvent|BrevoProductUpdateEvent $event): void | ||
{ | ||
$lang = Lang::getDefaultLanguage(); | ||
$currency = Currency::getDefaultCurrency(); | ||
$country = Country::getDefaultCountry(); | ||
$this->brevoProductService->export($event->getProduct(), $lang->getLocale(), $currency, $country); | ||
} | ||
|
||
public function createProduct(ProductCreateEvent $event) | ||
public function createProduct(ProductCreateEvent $event): void | ||
{ | ||
$lang = Lang::getDefaultLanguage(); | ||
$currency = Currency::getDefaultCurrency(); | ||
$country = Country::getDefaultCountry(); | ||
$this->brevoProductService->export($event->getProduct(), $lang->getLocale(), $currency, $country); | ||
} | ||
|
||
public function updateCategory(CategoryUpdateEvent $event) | ||
public function updateCategory(CategoryUpdateEvent|BrevoCategoryUpdateEvent $event): void | ||
{ | ||
$lang = Lang::getDefaultLanguage(); | ||
$this->brevoCategoryService->export($event->getCategory(), $lang->getLocale()); | ||
} | ||
|
||
public function createCategory(CategoryCreateEvent $event) | ||
public function createCategory(CategoryCreateEvent $event): void | ||
{ | ||
$lang = Lang::getDefaultLanguage(); | ||
$this->brevoCategoryService->export($event->getCategory(), $lang->getLocale()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Thelia package. | ||
* http://www.thelia.net | ||
* | ||
* (c) OpenStudio <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brevo\EventListeners; | ||
|
||
use Brevo\Services\BrevoApiService; | ||
|
@@ -11,22 +21,19 @@ | |
use Thelia\Core\Event\Order\OrderEvent; | ||
use Thelia\Core\Event\TheliaEvents; | ||
use Thelia\Log\Tlog; | ||
use Thelia\Model\Cart; | ||
use Thelia\Model\CountryQuery; | ||
use Thelia\Model\Currency; | ||
use Thelia\Model\Customer; | ||
use Thelia\Model\Lang; | ||
use Thelia\Model\Order; | ||
|
||
class CartListener implements EventSubscriberInterface | ||
{ | ||
|
||
public function __construct( | ||
private RequestStack $requestStack, | ||
private BrevoApiService $brevoApiService, | ||
private BrevoProductService $brevoProductService, | ||
private BrevoOrderService $brevoOrderService, | ||
){ | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
|
@@ -36,11 +43,11 @@ public static function getSubscribedEvents() | |
TheliaEvents::CART_DELETEITEM => ['trackUpdateCartEvent', 128], | ||
TheliaEvents::CART_CLEAR => ['trackDeleteCartEvent', 128], | ||
TheliaEvents::ORDER_PAY => ['trackNewOrderEvent', 110], | ||
TheliaEvents::ORDER_UPDATE_STATUS => ['updateStatus', 110] | ||
TheliaEvents::ORDER_UPDATE_STATUS => ['updateStatus', 110], | ||
]; | ||
} | ||
|
||
public function updateStatus(OrderEvent $orderEvent) | ||
public function updateStatus(OrderEvent $orderEvent): void | ||
{ | ||
/** @var Lang $lang */ | ||
$lang = $this->requestStack->getCurrentRequest()?->getSession()->get('thelia.current.lang'); | ||
|
@@ -50,17 +57,17 @@ public function updateStatus(OrderEvent $orderEvent) | |
$this->brevoOrderService->exportOrder($order, $lang->getLocale()); | ||
} | ||
|
||
public function trackUpdateCartEvent (CartEvent $event) | ||
public function trackUpdateCartEvent(CartEvent $event): void | ||
{ | ||
$this->trackCart($event, 'cart_updated'); | ||
} | ||
|
||
public function trackDeleteCartEvent (CartEvent $event) | ||
public function trackDeleteCartEvent(CartEvent $event): void | ||
{ | ||
$this->trackCart($event, 'cart_deleted'); | ||
} | ||
|
||
public function trackNewOrderEvent (OrderEvent $event) | ||
public function trackNewOrderEvent(OrderEvent $event): void | ||
{ | ||
$order = $event->getPlacedOrder(); | ||
|
||
|
@@ -94,8 +101,8 @@ public function trackNewOrderEvent (OrderEvent $event) | |
'total' => $order->getTotalAmount($tax, true, true), | ||
'currency' => $currency->getCode(), | ||
'items' => $this->brevoProductService->getItemsByOrder($order, $lang->getLocale()), | ||
] | ||
] | ||
], | ||
], | ||
]; | ||
|
||
$this->brevoOrderService->exportOrder($order, $lang->getLocale()); | ||
|
@@ -107,7 +114,7 @@ public function trackNewOrderEvent (OrderEvent $event) | |
} | ||
} | ||
|
||
protected function trackCart(CartEvent $event, $eventName) | ||
protected function trackCart(CartEvent $event, $eventName): void | ||
{ | ||
/** @var Customer $customer */ | ||
$customer = $this->requestStack->getCurrentRequest()?->getSession()?->get('thelia.customer_user'); | ||
|
@@ -144,8 +151,8 @@ protected function trackCart(CartEvent $event, $eventName) | |
'total' => $cart->getTaxedAmount($country), | ||
'currency' => $currency->getCode(), | ||
'items' => $this->brevoProductService->getItemsByCart($cart, $lang->getLocale(), $country), | ||
] | ||
] | ||
], | ||
], | ||
]; | ||
|
||
try { | ||
|
@@ -154,5 +161,4 @@ protected function trackCart(CartEvent $event, $eventName) | |
Tlog::getInstance()->error('Brevo track cart error:'.$exception->getMessage()); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters