Skip to content

Commit

Permalink
Merge pull request #13 from yangyao/queue
Browse files Browse the repository at this point in the history
Add helper function to verify is performance test
  • Loading branch information
yangyao committed Jul 24, 2023
2 parents 28976ac + 1516dcd commit 61627f2
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 110 deletions.
2 changes: 1 addition & 1 deletion Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Constants
public const WEBHOOK_RESOURCE_ORDERS = 'orders';
public const WEBHOOK_RESOURCE_PRODUCTS = 'products';

public const AFTERSHIP_TIKTOK_SHOP_VERSION = '1.0.11';
public const AFTERSHIP_TIKTOK_SHOP_VERSION = '1.0.12';

public const WEBHOOK_CONFIG_SCOPE_PATH = 'aftership/webhooks/webhooks';

Expand Down
40 changes: 40 additions & 0 deletions Helper/CommonHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* TikTokShop CommonHelper
*
* @author AfterShip <[email protected]>
* @copyright 2023 AfterShip
* @license MIT http://opensource.org/licenses/MIT
* @link https://aftership.com
*/

namespace AfterShip\TikTokShop\Helper;

/**
* CommonHelper helper function.
*
* @author AfterShip <[email protected]>
* @license MIT http://opensource.org/licenses/MIT
* @link https://aftership.com
*/
class CommonHelper
{
/**
* Check if is running under PerformanceTest
*
* @return bool
*/
public function isRunningUnderPerformanceTest()
{
$backtrace = debug_backtrace();
foreach ($backtrace as $trace) {
if (isset($trace['function'])
&& isset($trace['file'])
&& (strpos($trace['file'], 'GenerateFixturesCommand') !== false)
) {
return true;
}
}
return false;
}
}
19 changes: 6 additions & 13 deletions Helper/WebhookHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
*/
class WebhookHelper
{

/**
* Webhoks
*
* @var array|mixed
*/
protected $webhooks = [];
/**
* ScopeConfig Instance
*
Expand Down Expand Up @@ -75,11 +68,6 @@ public function __construct(
$this->oauthService = $oauthService;
$this->logger = $logger;
$this->integrationService = $integrationService;
$webhooksJson = $this->scopeConfig->getValue(
Constants::WEBHOOK_CONFIG_SCOPE_PATH,
'default'
);
$this->webhooks = $webhooksJson ? json_decode($webhooksJson) : [];
}

/**
Expand All @@ -92,7 +80,12 @@ public function __construct(
*/
public function makeWebhookRequest($topic, $data)
{
foreach ($this->webhooks as $webhook) {
$webhooksJson = $this->scopeConfig->getValue(
Constants::WEBHOOK_CONFIG_SCOPE_PATH,
'default'
);
$webhooks = $webhooksJson ? json_decode($webhooksJson) : [];
foreach ($webhooks as $webhook) {
if ($webhook->topic !== $topic) {
continue;
}
Expand Down
12 changes: 3 additions & 9 deletions Model/Queue/WebhookConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,9 @@ public function sendProductWebhook($event, $productId)
);
foreach ($parentIds as $parentId) {
$parentProduct = $this->productRepository->getById($parentId);
$this->webhookHelper->makeWebhookRequest(
Constants::WEBHOOK_TOPIC_PRODUCTS_UPDATE,
[
"id" => $parentProduct->getId(),
"type_id" => $parentProduct->getTypeId(),
"sku" => $parentProduct->getSku(),
"visibility" => (string)$parentProduct->getVisibility(),
]
);
$parentProduct->setData('updated_at', date('Y-m-d H:i:s'));
// This will trigger an webhook, no need to send an repeat event.
$this->productRepository->save($parentProduct);
}
}
}
33 changes: 12 additions & 21 deletions Observer/InventoryUpdateObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use AfterShip\TikTokShop\Model\Api\WebhookEvent;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use AfterShip\TikTokShop\Helper\CommonHelper;
use Magento\Authorization\Model\UserContextInterface;
use AfterShip\TikTokShop\Model\Queue\WebhookPublisher;

Expand Down Expand Up @@ -46,21 +47,30 @@ class InventoryUpdateObserver implements ObserverInterface
*/
protected $publisher;

/**
* Common Helper Instance.
* @var CommonHelper
*/
protected $commonHelper;

/**
* Construct
*
* @param UserContextInterface $userContext
* @param LoggerInterface $logger
* @param WebhookPublisher $publisher
* @param CommonHelper $commonHelper
*/
public function __construct(
UserContextInterface $userContext,
LoggerInterface $logger,
WebhookPublisher $publisher
WebhookPublisher $publisher,
CommonHelper $commonHelper
) {
$this->userContext = $userContext;
$this->logger = $logger;
$this->publisher = $publisher;
$this->commonHelper = $commonHelper;
}

/**
Expand All @@ -74,25 +84,6 @@ public function isRestfulApiRequest()
return ($userType === UserContextInterface::USER_TYPE_INTEGRATION);
}

/**
* Check if is running under PerformanceTest
*
* @return bool
*/
public function isRunningUnderPerformanceTest()
{
$backtrace = debug_backtrace();
foreach ($backtrace as $trace) {
if (isset($trace['function'])
&& isset($trace['file'])
&& (strpos($trace['file'], 'GenerateFixturesCommand') !== false)
) {
return true;
}
}
return false;
}

/**
* Execute
*
Expand All @@ -106,7 +97,7 @@ public function execute(Observer $observer)
if (!$this->isRestfulApiRequest()) {
return;
}
if ($this->isRunningUnderPerformanceTest()) {
if ($this->commonHelper->isRunningUnderPerformanceTest()) {
$this->logger->error(
'[AfterShip TikTokShop] InventoryUpdateObserver do not sync inventory during performance test'
);
Expand Down
33 changes: 12 additions & 21 deletions Observer/ProductDeleteObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace AfterShip\TikTokShop\Observer;

use AfterShip\TikTokShop\Constants;
use AfterShip\TikTokShop\Helper\CommonHelper;
use AfterShip\TikTokShop\Model\Api\WebhookEvent;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
Expand Down Expand Up @@ -39,37 +40,27 @@ class ProductDeleteObserver implements ObserverInterface
*/
protected $publisher;

/**
* Common Helper Instance.
* @var CommonHelper
*/
protected $commonHelper;

/**
* Construct
*
* @param LoggerInterface $logger
* @param WebhookPublisher $publisher
* @param CommonHelper $commonHelper
*/
public function __construct(
LoggerInterface $logger,
WebhookPublisher $publisher
WebhookPublisher $publisher,
CommonHelper $commonHelper
) {
$this->logger = $logger;
$this->publisher = $publisher;
}

/**
* Check if is running under PerformanceTest
*
* @return bool
*/
public function isRunningUnderPerformanceTest()
{
$backtrace = debug_backtrace();
foreach ($backtrace as $trace) {
if (isset($trace['function'])
&& isset($trace['file'])
&& (strpos($trace['file'], 'GenerateFixturesCommand') !== false)
) {
return true;
}
}
return false;
$this->commonHelper = $commonHelper;
}

/**
Expand All @@ -82,7 +73,7 @@ public function isRunningUnderPerformanceTest()
public function execute(Observer $observer)
{
try {
if ($this->isRunningUnderPerformanceTest()) {
if ($this->commonHelper->isRunningUnderPerformanceTest()) {
$this->logger->error(
'[AfterShip TikTokShop] ProductDeleteObserver do not sync inventory during performance test'
);
Expand Down
33 changes: 12 additions & 21 deletions Observer/ProductSaveObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace AfterShip\TikTokShop\Observer;

use AfterShip\TikTokShop\Constants;
use AfterShip\TikTokShop\Helper\CommonHelper;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use AfterShip\TikTokShop\Model\Queue\WebhookPublisher;
Expand Down Expand Up @@ -39,37 +40,27 @@ class ProductSaveObserver implements ObserverInterface
*/
protected $publisher;

/**
* Common Helper Instance.
* @var CommonHelper
*/
protected $commonHelper;

/**
* Construct
*
* @param LoggerInterface $logger
* @param WebhookPublisher $publisher
* @param CommonHelper $commonHelper
*/
public function __construct(
LoggerInterface $logger,
WebhookPublisher $publisher
WebhookPublisher $publisher,
CommonHelper $commonHelper
) {
$this->logger = $logger;
$this->publisher = $publisher;
}

/**
* Check if is running under PerformanceTest
*
* @return bool
*/
public function isRunningUnderPerformanceTest()
{
$backtrace = debug_backtrace();
foreach ($backtrace as $trace) {
if (isset($trace['function'])
&& isset($trace['file'])
&& (strpos($trace['file'], 'GenerateFixturesCommand') !== false)
) {
return true;
}
}
return false;
$this->commonHelper = $commonHelper;
}

/**
Expand All @@ -82,7 +73,7 @@ public function isRunningUnderPerformanceTest()
public function execute(Observer $observer)
{
try {
if ($this->isRunningUnderPerformanceTest()) {
if ($this->commonHelper->isRunningUnderPerformanceTest()) {
$this->logger->error(
'[AfterShip TikTokShop] ProductSaveObserver do not sync inventory during performance test'
);
Expand Down
34 changes: 12 additions & 22 deletions Observer/SalesOrderUpdateObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
namespace AfterShip\TikTokShop\Observer;

use AfterShip\TikTokShop\Constants;
use AfterShip\TikTokShop\Helper\CommonHelper;
use AfterShip\TikTokShop\Model\Api\WebhookEvent;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\MessageQueue\PublisherInterface;
use AfterShip\TikTokShop\Model\Queue\WebhookPublisher;
use Psr\Log\LoggerInterface;

Expand All @@ -40,37 +40,27 @@ class SalesOrderUpdateObserver implements ObserverInterface
*/
protected $publisher;

/**
* Common Helper Instance.
* @var CommonHelper
*/
protected $commonHelper;

/**
* Construct
*
* @param LoggerInterface $logger
* @param WebhookPublisher $publisher
* @param CommonHelper $commonHelper
*/
public function __construct(
LoggerInterface $logger,
WebhookPublisher $publisher
WebhookPublisher $publisher,
CommonHelper $commonHelper
) {
$this->logger = $logger;
$this->publisher = $publisher;
}

/**
* Check if is running under PerformanceTest
*
* @return bool
*/
public function isRunningUnderPerformanceTest()
{
$backtrace = debug_backtrace();
foreach ($backtrace as $trace) {
if (isset($trace['function'])
&& isset($trace['file'])
&& (strpos($trace['file'], 'GenerateFixturesCommand') !== false)
) {
return true;
}
}
return false;
$this->commonHelper = $commonHelper;
}

/**
Expand All @@ -83,7 +73,7 @@ public function isRunningUnderPerformanceTest()
public function execute(Observer $observer)
{
try {
if ($this->isRunningUnderPerformanceTest()) {
if ($this->commonHelper->isRunningUnderPerformanceTest()) {
$this->logger->error(
'[AfterShip TikTokShop] SalesOrderUpdateObserver do not sync inventory during performance test'
);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "aftership/tiktok-shop",
"description": "Aftership TikTok Shop extension for Magento 2. Allows connect with TikTok Shop and more.",
"type": "magento2-module",
"version": "1.0.11",
"version": "1.0.12",
"minimum-stability": "stable",
"license": "MIT",
"keywords": ["aftership", "magento", "magento2", "magento-2", "tiktok", "shopping", "shop", "feed"],
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="AfterShip_TikTokShop" setup_version="1.0.11" schema_version="1.0.11">
<module name="AfterShip_TikTokShop" setup_version="1.0.12" schema_version="1.0.12">
</module>
</config>

0 comments on commit 61627f2

Please sign in to comment.