Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store id as function params #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Config/AbstractConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ public function __construct(
) {
}

public function isSetFlag(string $xpath, int|null|string $store = null): bool
public function isSetFlag(string $xpath, int|null|string $storeId = null): bool
{
return $this->scopeConfig->isSetFlag(
$xpath,
ScopeInterface::SCOPE_STORE,
scopeCode: $store
scopeCode: $storeId
);
}

protected function getConfig(string $xpath, int|null|string $store = null): mixed
protected function getConfig(string $xpath, int|null|string $storeId = null): mixed
{
return $this->scopeConfig->getValue(
$xpath,
ScopeInterface::SCOPE_STORE,
$store
$storeId
);
}

public function getConfigAsString(string $xpath, int|null|string $store = null): ?string
public function getConfigAsString(string $xpath, int|null|string $storeId = null): ?string
{
return (string)$this->getConfig($xpath, $store);
return (string)$this->getConfig($xpath, $storeId);
}

public function getConfigAsInt(string $xpath, int|null|string $store = null): int
public function getConfigAsInt(string $xpath, int|null|string $storeId = null): int
{
return (int)$this->getConfig($xpath, $store);
return (int)$this->getConfig($xpath, $storeId);
}
}
91 changes: 55 additions & 36 deletions Config/ModuleConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,80 +27,95 @@ class ModuleConfiguration extends AbstractConfigProvider
public const XPATH_DEBUG_MODE = 'google/serverside_analytics/developer/debug';
public const XPATH_ENABLE_LOGGING = 'google/serverside_analytics/developer/logging';

public function isDebugMode(int|null|string $store = null): bool
public function ensureStoreId(int|null|string $storeId = null): int|string
{
return $this->isSetFlag(static::XPATH_DEBUG_MODE, $store);
return $this->storeManager->getStore($storeId)->getId();
}

public function isLogging(int|null|string $store = null): bool
public function isDebugMode(int|null|string $storeId = null): bool
{
return $this->isSetFlag(static::XPATH_ENABLE_LOGGING, $store);
return $this->isSetFlag(static::XPATH_DEBUG_MODE, $storeId);
}

public function getCurrencySource(int|null|string $store = null): ?int
public function isLogging(int|null|string $storeId = null): bool
{
return $this->getConfigAsInt(static::XPATH_CURRENCY_SOURCE, $store);
return $this->isSetFlag(static::XPATH_ENABLE_LOGGING, $storeId);
}

public function getTaxDisplayType(int|null|string $store = null): ?int
public function getCurrencySource(int|null|string $storeId = null): ?int
{
return $this->getConfigAsInt(static::XPATH_TAX_DISPLAY_TYPE, $store);
return $this->getConfigAsInt(static::XPATH_CURRENCY_SOURCE, $storeId);
}

public function getFallbackGenerationMode(int|null|string $store = null): ?int
public function getTaxDisplayType(int|null|string $storeId = null): ?int
{
return $this->getConfigAsInt(static::XPATH_FALLBACK_GENERATION_MODE, $store);
return $this->getConfigAsInt(static::XPATH_TAX_DISPLAY_TYPE, $storeId);
}

public function getFallbackSessionIdPrefix(int|null|string $store = null): ?string
public function getFallbackGenerationMode(int|null|string $storeId = null): ?int
{
return $this->getConfigAsString(static::XPATH_FALLBACK_SESSION_ID_PREFIX, $store);
return $this->getConfigAsInt(static::XPATH_FALLBACK_GENERATION_MODE, $storeId);
}

public function getFallbackSessionId(int|null|string $store = null): ?string
public function getFallbackSessionIdPrefix(int|null|string $storeId = null): ?string
{
return $this->getConfigAsString(static::XPATH_FALLBACK_SESSION_ID, $store);
return $this->getConfigAsString(static::XPATH_FALLBACK_SESSION_ID_PREFIX, $storeId);
}

public function shouldTriggerOnPayment(int|null|string $store = null, ?string $paymentMethodCode = null): ?bool
public function getFallbackSessionId(int|null|string $storeId = null): ?string
{
return $this->shouldTriggerOn($store, TriggerMode::PAYED, $paymentMethodCode);
return $this->getConfigAsString(static::XPATH_FALLBACK_SESSION_ID, $storeId);
}

public function shouldTriggerOnPayment(int|null|string $storeId = null, ?string $paymentMethodCode = null): ?bool
{
return $this->shouldTriggerOn(
mode: TriggerMode::PAYED,
storeId: $storeId,
paymentMethodCode: $paymentMethodCode
);
}

protected function shouldTriggerOn(
int|null|string $store = null,
int $mode,
int|null|string $storeId = null,
?string $paymentMethodCode = null
): ?bool {
return $this->isReadyForUse() && $this->shouldTriggerOnMode($store, $mode, $paymentMethodCode);
return $this->isReadyForUse($storeId) && $this->shouldTriggerOnMode(
mode: $mode,
storeId: $storeId,
paymentMethodCode: $paymentMethodCode
);
}

public function isReadyForUse(int|null|string $store = null): bool
public function isReadyForUse(int|null|string $storeId = null): bool
{
return $this->isEnabled($store) && !empty($this->getApiSecret()) && !empty($this->getMeasurementId());
return $this->isEnabled($storeId) &&
!empty($this->getApiSecret($storeId)) &&
!empty($this->getMeasurementId($storeId));
}

public function isEnabled(int|null|string $store = null): bool
public function isEnabled(int|null|string $storeId = null): bool
{
return $this->isSetFlag(static::XPATH_ENABLED, $store);
return $this->isSetFlag(static::XPATH_ENABLED, $storeId);
}

public function getApiSecret(int|null|string $store = null): ?string
public function getApiSecret(int|null|string $storeId = null): ?string
{
return $this->getConfigAsString(static::XPATH_API_SECRET, $store);
return $this->getConfigAsString(static::XPATH_API_SECRET, $storeId);
}

public function getMeasurementId(int|null|string $store = null): ?string
public function getMeasurementId(int|null|string $storeId = null): ?string
{
return $this->getConfigAsString(static::XPATH_MEASUREMENT_ID, $store);
return $this->getConfigAsString(static::XPATH_MEASUREMENT_ID, $storeId);
}

protected function shouldTriggerOnMode(
int|null|string $store = null,
int $mode,
int|null|string $storeId = null,
?string $paymentMethodCode = null
): ?bool {
$triggerMode = $this->getTriggerMode($store);
$triggerMode = $this->getTriggerMode($storeId);
if ($triggerMode === $mode) {
return true;
}
Expand All @@ -113,43 +128,47 @@ protected function shouldTriggerOnMode(
return false;
}

if (in_array($paymentMethodCode, $this->getPaymentMethodsForTrigger($store, $mode))) {
if (in_array($paymentMethodCode, $this->getPaymentMethodsForTrigger(mode: $mode, storeId: $storeId))) {
return true;
}

return true;
}

public function getTriggerMode(int|null|string $store = null): ?int
public function getTriggerMode(int|null|string $storeId = null): ?int
{
$value = $this->getConfigAsInt(static::XPATH_TRIGGER_MODE, $store);
$value = $this->getConfigAsInt(static::XPATH_TRIGGER_MODE, $storeId);
if (in_array($value, TriggerMode::ALL_OPTION_VALUES)) {
return $value;
}

return null;
}

public function getPaymentMethodsForTrigger(int|null|string $store = null, int $mode): array
public function getPaymentMethodsForTrigger(int $mode = null, int|null|string $storeId = null): array
{
switch ($mode) {
case TriggerMode::PLACED:
return explode(
',',
$this->getConfigAsString(self::XPATH_TRIGGER_ON_PLACED_METHODS, $store)
$this->getConfigAsString(self::XPATH_TRIGGER_ON_PLACED_METHODS, $storeId)
);
case TriggerMode::PAYED:
return explode(
',',
$this->getConfigAsString(self::XPATH_TRIGGER_ON_PAYED_METHODS, $store)
$this->getConfigAsString(self::XPATH_TRIGGER_ON_PAYED_METHODS, $storeId)
);
}

return [];
}

public function shouldTriggerOnPlaced(int|null|string $store = null, ?string $paymentMethodCode = null): ?bool
public function shouldTriggerOnPlaced(int|null|string $storeId = null, ?string $paymentMethodCode = null): ?bool
{
return $this->shouldTriggerOn($store, TriggerMode::PLACED, $paymentMethodCode);
return $this->shouldTriggerOn(
mode: TriggerMode::PLACED,
storeId: $storeId,
paymentMethodCode: $paymentMethodCode
);
}
}
32 changes: 21 additions & 11 deletions Model/GAClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

class GAClient
{
protected ?Service $service;
/** @var Service[] */
protected array $service = [];

protected ?BaseRequest $request;

Expand Down Expand Up @@ -117,7 +118,7 @@ public function addProduct(DataObject $data)
/**
* @throws LocalizedException
*/
public function firePurchaseEvent()
public function firePurchaseEvent(int|string|null $storeId)
{
if (!$this->productCounter) {
throw new LocalizedException(
Expand All @@ -135,22 +136,31 @@ public function firePurchaseEvent()

$send = $this->moduleConfiguration->isDebugMode() ? 'sendDebug' : 'send';

$response = $this->getService()->$send($baseRequest);

$this->createLog('Request: ', [$this->getRequest()->export()]);
$service = $this->getService($storeId);
$response = $this->getService($storeId)->$send($baseRequest);

$this->createLog(
"Request: ",
[
"storeId" => $storeId,
"measurementId" => $service->getMeasurementId(),
"body" => $this->getRequest()->export()
]
);
$this->createLog('Response: ', [$response->getStatusCode()]);
}

public function getService()
public function getService(int|string|null $storeId)
{
if (isset($this->service)) {
return $this->service;
$storeId = $this->moduleConfiguration->ensureStoreId($storeId);
if (isset($this->service[$storeId])) {
return $this->service[$storeId];
}

$this->service = new Service($this->moduleConfiguration->getApiSecret());
$this->service->setMeasurementId($this->moduleConfiguration->getMeasurementId());
$this->service[$storeId] = new Service($this->moduleConfiguration->getApiSecret($storeId));
$this->service[$storeId]->setMeasurementId($this->moduleConfiguration->getMeasurementId($storeId));

return $this->service;
return $this->service[$storeId];
}

public function createLog($message, array $context = [])
Expand Down
20 changes: 11 additions & 9 deletions Model/SendPurchaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function execute(Order $order, string $eventName = 'unknown')

$this->emulation->startEnvironmentEmulation($orderStoreId, 'adminhtml');

if (!$this->moduleConfiguration->isReadyForUse()) {
if (!$this->moduleConfiguration->isReadyForUse($orderStoreId)) {
$this->emulation->stopEnvironmentEmulation();

return;
Expand All @@ -73,7 +73,7 @@ public function execute(Order $order, string $eventName = 'unknown')

if ($elgentosSalesOrder->getData('send_at') !== null) {
$this->emulation->stopEnvironmentEmulation();
if ($this->moduleConfiguration->isLogging()) {
if ($this->moduleConfiguration->isLogging($orderStoreId)) {
$gaclient->createLog(
'The purchase event for order #' .
$order->getIncrementId() . ' was send already by trigger ' .
Expand All @@ -88,7 +88,7 @@ public function execute(Order $order, string $eventName = 'unknown')
return;
}

if ($this->moduleConfiguration->isLogging()) {
if ($this->moduleConfiguration->isLogging($orderStoreId)) {
$gaclient->createLog(
'Got ' . $eventName . ' event for Ga UserID: ' . $elgentosSalesOrder->getGaUserId(),
[
Expand All @@ -113,7 +113,7 @@ public function execute(Order $order, string $eventName = 'unknown')

$transactionDataObject = $this->getTransactionDataObject($order, $elgentosSalesOrder);
$products = $this->collectProducts($order);
$this->sendPurchaseEvent($gaclient, $transactionDataObject, $products, $trackingDataObject);
$this->sendPurchaseEvent($gaclient, $transactionDataObject, $products, $trackingDataObject, $orderStoreId);

$elgentosSalesOrder->setData('trigger', $eventName);
$elgentosSalesOrder->setData('send_at', date('Y-m-d H:i:s'));
Expand Down Expand Up @@ -185,14 +185,15 @@ protected function collectProducts(Order $order): array
*/
private function getPaidProductPrice(Item $orderItem): float
{
return $this->moduleConfiguration->getTaxDisplayType() === Config::DISPLAY_TYPE_EXCLUDING_TAX
return $this->moduleConfiguration
->getTaxDisplayType($orderItem->getOrder()->getStoreId()) === Config::DISPLAY_TYPE_EXCLUDING_TAX
? $orderItem->getBasePrice()
: $orderItem->getBasePriceInclTax();
}

public function getTransactionDataObject(Order $order, $elgentosSalesOrder): DataObject
{
$currency = $this->moduleConfiguration->getCurrencySource() === CurrencySource::GLOBAL ?
$currency = $this->moduleConfiguration->getCurrencySource($order->getStoreId()) === CurrencySource::GLOBAL ?
$order->getGlobalCurrencyCode() :
$order->getBaseCurrencyCode();

Expand All @@ -219,7 +220,7 @@ public function getTransactionDataObject(Order $order, $elgentosSalesOrder): Dat

private function getPaidShippingCosts(Order $order): ?float
{
return $this->moduleConfiguration->getTaxDisplayType() == Config::DISPLAY_TYPE_EXCLUDING_TAX
return $this->moduleConfiguration->getTaxDisplayType($order->getStoreId()) == Config::DISPLAY_TYPE_EXCLUDING_TAX
? $order->getBaseShippingAmount()
: $order->getBaseShippingInclTax();
}
Expand All @@ -228,7 +229,8 @@ public function sendPurchaseEvent(
GAClient $gaclient,
DataObject $transactionDataObject,
array $products,
DataObject $trackingDataObject
DataObject $trackingDataObject,
int|string|null $orderStoreId
): void {
try {
$gaclient->setTransactionData($transactionDataObject);
Expand All @@ -248,7 +250,7 @@ public function sendPurchaseEvent(

$gaclient->setTrackingData($trackingDataObject);

$gaclient->firePurchaseEvent();
$gaclient->firePurchaseEvent($orderStoreId);
} catch (\Exception $e) {
$gaclient->createLog($e);
}
Expand Down
7 changes: 6 additions & 1 deletion Observer/AfterOrderPayed.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public function execute(Observer $observer)
/** @var Order $order */
$order = $payment->getOrder();

if ($this->moduleConfiguration->shouldTriggerOnPayment(paymentMethodCode: $method->getCode())) {
if (
$this->moduleConfiguration->shouldTriggerOnPayment(
storeId: $order->getStoreId(),
paymentMethodCode: $method->getCode()
)
) {
$this->sendPurchaseEvent->execute($order, 'AfterOrderPayed');
}
}
Expand Down
Loading