Skip to content

Commit

Permalink
Optimize widget load times
Browse files Browse the repository at this point in the history
ISSUE: SC1-2
  • Loading branch information
ristokekovic committed Dec 26, 2024
1 parent c91502a commit 2f53950
Show file tree
Hide file tree
Showing 7 changed files with 607 additions and 20 deletions.
48 changes: 48 additions & 0 deletions Controller/Adminhtml/Configuration/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI;
use SeQura\Core\BusinessLogic\AdminAPI\PaymentMethods\Responses\PaymentMethodsResponse;
use Sequra\Core\DataAccess\Entities\PaymentMethod;
use Sequra\Core\DataAccess\Entities\PaymentMethods as PaymentMethodsEntity;
use SeQura\Core\Infrastructure\Http\Exceptions\HttpRequestException;
use SeQura\Core\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException;
use SeQura\Core\Infrastructure\ORM\QueryFilter\Operators;
use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryFilter;
use SeQura\Core\Infrastructure\ORM\RepositoryRegistry;

/**
* Class PaymentMethods
Expand All @@ -31,12 +39,52 @@ public function __construct(Context $context, JsonFactory $jsonFactory)
* Returns active connection data.
*
* @return Json
*
* @throws HttpRequestException
* @throws RepositoryNotRegisteredException
* @throws \Exception
*/
protected function getPaymentMethods(): Json
{
$data = AdminAPI::get()->paymentMethods($this->storeId)->getPaymentMethods($this->identifier);
$this->savePaymentMethods($data);

$this->addResponseCode($data);

return $this->result->setData($data->toArray());
}

/**
* Saves payment methods to the integration database.
*
* @param PaymentMethodsResponse $data
*
* @return void
*
* @throws RepositoryNotRegisteredException
*/
private function savePaymentMethods(PaymentMethodsResponse $data): void
{
$apiPaymentMethods = PaymentMethod::fromBatch($data->toArray());

$paymentMethodsRepository = RepositoryRegistry::getRepository(PaymentMethodsEntity::CLASS_NAME);

$filter = new QueryFilter();
$filter->where('storeId', Operators::EQUALS, $this->storeId)
->where('merchantId', Operators::EQUALS, $this->identifier);
$paymentMethods = $paymentMethodsRepository->selectOne($filter);

if ($paymentMethods === null) {
$paymentMethods = new PaymentMethodsEntity();

$paymentMethods->setStoreId($this->storeId);
$paymentMethods->setMerchantId($this->identifier);
$paymentMethods->setPaymentMethods($apiPaymentMethods);

$paymentMethodsRepository->save($paymentMethods);
} else {
$paymentMethods->setPaymentMethods($apiPaymentMethods);
$paymentMethodsRepository->update($paymentMethods);
}
}
}
329 changes: 329 additions & 0 deletions DataAccess/Entities/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
<?php

namespace Sequra\Core\DataAccess\Entities;

use SeQura\Core\Infrastructure\ORM\Configuration\EntityConfiguration;
use SeQura\Core\Infrastructure\ORM\Configuration\IndexMap;
use SeQura\Core\Infrastructure\ORM\Entity;

/**
* Class PaymentMethod
*
* @package Sequra\Core\DataAccess\Entities
*/
class PaymentMethod extends Entity
{

/**
* Fully qualified name of this class.
*/
public const CLASS_NAME = __CLASS__;

/**
* @var string
*/
protected $product;
/**
* @var string
*/
protected $title;
/**
* @var string
*/
protected $longTitle;
/**
* @var string
*/
protected $startsAt;
/**
* @var string
*/
protected $endsAt;
/**
* @var string
*/
protected $campaign;
/**
* @var string
*/
protected $claim;
/**
* @var string
*/
protected $description;
/**
* @var string
*/
protected $icon;
/**
* @var string
*/
protected $costDescription;
/**
* @var int
*/
protected $minAmount;
/**
* @var int
*/
protected $maxAmount;
/**
* @inheritDoc
*/
public function getConfig(): EntityConfiguration
{
$indexMap = new IndexMap();

$indexMap->addStringIndex('product');

return new EntityConfiguration($indexMap, 'PaymentMethod');
}

/**
* @inheritDoc
*/
public function inflate(array $data)
{
parent::inflate($data);

$this->product = $data['product'];
$this->title = $data['title'];
$this->longTitle = $data['longTitle'];
$this->startsAt = $data['startsAt'];
$this->endsAt = $data['endsAt'];
$this->campaign = $data['campaign'];
$this->claim = $data['claim'];
$this->description = $data['description'];
$this->icon = $data['icon'];
$this->costDescription = $data['costDescription'];
$this->minAmount = $data['minAmount'];
$this->maxAmount = $data['maxAmount'];
}

/**
* @inheritDoc
*/
public function toArray(): array
{
$data = parent::toArray();

$data['product'] = $this->product;
$data['title'] = $this->title;
$data['longTitle'] = $this->longTitle;
$data['startsAt'] = $this->startsAt;
$data['endsAt'] = $this->endsAt;
$data['campaign'] = $this->campaign;
$data['claim'] = $this->claim;
$data['description'] = $this->description;
$data['icon'] = $this->icon;
$data['costDescription'] = $this->costDescription;
$data['minAmount'] = $this->minAmount;
$data['maxAmount'] = $this->maxAmount;

return $data;
}

/**
* @return string|null
*/
public function getProduct(): ?string
{
return $this->product;
}

/**
* @param string $product
* @return void
*/
public function setProduct(string $product): void
{
$this->product = $product;
}

/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}

/**
* @param string $title
* @return void
*/
public function setTitle(string $title): void
{
$this->title = $title;
}

/**
* @return string|null
*/
public function getLongTitle(): ?string
{
return $this->longTitle;
}

/**
* @param string $longTitle
* @return void
*/
public function setLongTitle(string $longTitle): void
{
$this->longTitle = $longTitle;
}

/**
* @return string|null
*/
public function getStartsAt(): ?string
{
return $this->startsAt;
}

/**
* @param string $startsAt
* @return void
*/
public function setStartsAt(string $startsAt): void
{
$this->startsAt = $startsAt;
}

/**
* @return string|null
*/
public function getEndsAt(): ?string
{
return $this->endsAt;
}

/**
* @param string $endsAt
* @return void
*/
public function setEndsAt(string $endsAt): void
{
$this->endsAt = $endsAt;
}

/**
* @return string|null
*/
public function getCampaign(): ?string
{
return $this->campaign;
}

/**
* @param string $campaign
* @return void
*/
public function setCampaign(string $campaign): void
{
$this->campaign = $campaign;
}

/**
* @return string|null
*/
public function getClaim(): ?string
{
return $this->claim;
}

/**
* @param string $claim
* @return void
*/
public function setClaim(string $claim): void
{
$this->claim = $claim;
}

/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}

/**
* @param string $description
* @return void
*/
public function setDescription(string $description): void
{
$this->description = $description;
}

/**
* @return string|null
*/
public function getIcon(): ?string
{
return $this->icon;
}

/**
* @param string $icon
* @return void
*/
public function setIcon(string $icon): void
{
$this->icon = $icon;
}

/**
* @return string|null
*/
public function getCostDescription(): ?string
{
return $this->costDescription;
}

/**
* @param string $costDescription
* @return void
*/
public function setCostDescription(string $costDescription): void
{
$this->costDescription = $costDescription;
}

/**
* @return int|null
*/
public function getMinAmount(): ?int
{
return $this->minAmount;
}

/**
* @param int $minAmount
* @return void
*/
public function setMinAmount(int $minAmount): void
{
$this->minAmount = $minAmount;
}

/**
* @return int|null
*/
public function getMaxAmount(): ?int
{
return $this->maxAmount;
}

/**
* @param int $maxAmount
* @return void
*/
public function setMaxAmount(int $maxAmount): void
{
$this->maxAmount = $maxAmount;
}
}
Loading

0 comments on commit 2f53950

Please sign in to comment.