Skip to content

Commit

Permalink
Added update events for customer, category and product
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Nov 15, 2023
1 parent a63a66f commit aedf6d9
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.1.6</version>
<version>1.1.7</version>
<authors>
<author>
<name>Chabreuil Antoine</name>
Expand Down
39 changes: 39 additions & 0 deletions Event/BrevoCategoryUpdateEvent.php
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;
}
}
39 changes: 39 additions & 0 deletions Event/BrevoCustomerUpdateEvent.php
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;
}
}
23 changes: 23 additions & 0 deletions Event/BrevoEvents.php
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';
}
39 changes: 39 additions & 0 deletions Event/BrevoProductUpdateEvent.php
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;
}
}
36 changes: 23 additions & 13 deletions EventListeners/BrevoUpdateListener.php
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;
Expand All @@ -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()
Expand All @@ -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());
Expand Down
38 changes: 22 additions & 16 deletions EventListeners/CartListener.php
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;
Expand All @@ -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()
Expand All @@ -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');
Expand All @@ -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();

Expand Down Expand Up @@ -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());
Expand All @@ -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');
Expand Down Expand Up @@ -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 {
Expand All @@ -154,5 +161,4 @@ protected function trackCart(CartEvent $event, $eventName)
Tlog::getInstance()->error('Brevo track cart error:'.$exception->getMessage());
}
}

}
}
7 changes: 5 additions & 2 deletions EventListeners/CustomerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Brevo\EventListeners;

use Brevo\Api\BrevoClient;
use Brevo\Event\BrevoCustomerUpdateEvent;
use Brevo\Event\BrevoEvents;
use Brevo\Services\BrevoCustomerService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent;
Expand All @@ -24,12 +26,13 @@ public static function getSubscribedEvents()
TheliaEvents::CUSTOMER_CREATEACCOUNT => ['createOrUpdateCustomer', 100],
TheliaEvents::CUSTOMER_UPDATEACCOUNT => ['createOrUpdateCustomer', 100],
TheliaEvents::CUSTOMER_UPDATEPROFILE => ['createOrUpdateCustomer', 100],
BrevoEvents::UPDATE_CUSTOMER => ['createOrUpdateCustomer', 128]
];
}

public function createOrUpdateCustomer(CustomerCreateOrUpdateEvent $event)
public function createOrUpdateCustomer(CustomerCreateOrUpdateEvent|BrevoCustomerUpdateEvent $event)
{
$this->brevoCustomerService->createUpdateContact($event->getCustomer()?->getId());
}

}
}

0 comments on commit aedf6d9

Please sign in to comment.