Skip to content

Commit

Permalink
Fix internal codestyle.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Feb 15, 2022
1 parent dc03af6 commit 562837e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 61 deletions.
7 changes: 3 additions & 4 deletions src/CmsOrderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@


use Baraja\Cms\Search\SearchablePlugin;
use Baraja\Doctrine\EntityManager;
use Baraja\Plugin\BasePlugin;
use Baraja\Plugin\SimpleComponent\Button;
use Baraja\Shop\Order\Entity\Order;
use Baraja\Shop\Order\Repository\OrderRepository;
use Baraja\Url\Url;
use Doctrine\ORM\EntityManagerInterface;

final class CmsOrderPlugin extends BasePlugin implements SearchablePlugin
{
private OrderRepository $orderRepository;


public function __construct(
private EntityManager $entityManager,
) {
public function __construct(EntityManagerInterface $entityManager)
{
$orderRepository = $entityManager->getRepository(Order::class);
assert($orderRepository instanceof OrderRepository);
$this->orderRepository = $orderRepository;
Expand Down
157 changes: 104 additions & 53 deletions src/Command/CheckOrderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use Baraja\BankTransferAuthorizator\Transaction;
use Baraja\Doctrine\EntityManager;
use Baraja\Doctrine\EntityManagerException;
use Baraja\Shop\Currency\CurrencyManager;
use Baraja\Shop\Order\Entity\Order;
use Baraja\Shop\Order\Entity\OrderStatus;
use Baraja\Shop\Order\OrderStatusManager;
use Baraja\Shop\Order\Payment\OrderPaymentClient;
use Baraja\Shop\Order\Repository\OrderRepository;
use Baraja\Shop\Order\Status\OrderWorkflow;
use Baraja\Shop\Order\OrderPaymentManager;
use Nette\Application\UI\InvalidLinkException;
Expand All @@ -22,14 +24,21 @@

final class CheckOrderCommand extends Command
{
private OrderRepository $orderRepository;


public function __construct(
private EntityManager $entityManager,
private OrderStatusManager $orderStatusManager,
private OrderPaymentManager $tm,
private OrderPaymentClient $orderPaymentClient,
private CurrencyManager $currencyManager,
private OrderWorkflow $workflow,
) {
parent::__construct();
$orderRepository = $entityManager->getRepository(Order::class);
assert($orderRepository instanceof OrderRepository);
$this->orderRepository = $orderRepository;
}


Expand All @@ -45,34 +54,19 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var Order[] $orders */
$orders = $this->entityManager->getRepository(Order::class)
->createQueryBuilder('orderEntity')
->leftJoin('orderEntity.status', 'status')
->where('orderEntity.paid = FALSE')
->andWhere('status.code = :code')
->setParameter('code', OrderStatus::STATUS_NEW)
->getQuery()
->getResult();
$orders = $this->orderRepository->getOrderForCheckPayment();

/** @var array<string, float> $unauthorizedVariables */
$unauthorizedVariables = [];
/** @var array<numeric-string, Order> $orderByVariable */
$orderByVariable = [];
/** @var array<int, int> $unauthorizedVariablesForCheck */
$unauthorizedVariablesForCheck = [];
$orderByVariable = $this->filterOrderByVariable($orders);
$unauthorizedVariablesForCheck = $this->filterUnauthorizedVariablesForCheck($orders);

foreach ($orders as $order) {
/** @var numeric-string $number */
$number = $order->getNumber();
$numberInt = (int) $number;
$unauthorizedVariables[$number] = (float) $order->getPrice()->getValue();
$orderByVariable[$number] = $order;
$unauthorizedVariablesForCheck[] = $numberInt;

echo $number . ' [' . $order->getPrice() . ' ' . $order->getCurrencyCode() . ']';
echo ' [' . $order->getInsertedDate()->format('Y-m-d H:i:s') . ']';
$this->updateStatusByWorkflow($order);
echo sprintf(
'%s [%s %s] [%s]',
$order->getNumber(),
$order->getPrice(),
$order->getCurrencyCode(),
$order->getInsertedDate()->format('Y-m-d H:i:s'),
);
echo "\n";
}

Expand All @@ -84,14 +78,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->entityManager->flush();
echo "\n\n" . '------' . "\n\n" . 'Authorized:' . "\n\n";

$this->authOrders($unauthorizedVariables, $orderByVariable, $authorizator);
$this->authOrders($orderByVariable, $authorizator);

echo "\n\n";
echo 'Saving...' . "\n\n";
$this->entityManager->flush();
$this->checkSentOrders();
$this->entityManager->flush();

foreach ($this->orderRepository->getOrderForCheckPayment() as $order) {
$this->updateStatusByWorkflow($order);
}

return 0;
}

Expand Down Expand Up @@ -150,37 +148,41 @@ private function checkUnmatchedTransactions(array $unauthorizedVariables, Author


/**
* @param array<string, float> $unauthorizedVariables
* @param array<numeric-string, Order> $orderByVariable
*/
private function authOrders(array $unauthorizedVariables, array $orderByVariable, Authorizator $authorizator): void
private function authOrders(array $orderByVariable, Authorizator $authorizator): void
{
/** @var callable&(callable(Transaction): void)[] $callback */
$callback = function (Transaction $transaction) use ($orderByVariable): void {
assert($transaction instanceof \Baraja\FioPaymentAuthorizator\Transaction);
$entity = null;
if ($this->tm->transactionExist((int) $transaction->getIdTransaction()) === false) {
$entity = $this->tm->storeTransaction($transaction);
}
$variable = (string) $transaction->getVariableSymbol();
if ($variable !== '' && isset($orderByVariable[$variable])) {
$order = $orderByVariable[$variable];
$order->setPaid(true);
$this->orderStatusManager->setStatus($order, OrderStatus::STATUS_PAID);
$entity?->setOrder($order);
foreach ($this->currencyManager->getCurrencies() as $currency) {
$orders = $this->orderRepository->getOrderForCheckPayment($currency->getCode());
$unauthorizedVariables = $this->filterUnauthorizedVariables($orders);

/** @var callable&(callable(Transaction): void)[] $callback */
$callback = function (Transaction $transaction) use ($orderByVariable): void {
assert($transaction instanceof \Baraja\FioPaymentAuthorizator\Transaction);
$entity = null;
if ($this->tm->transactionExist((int) $transaction->getIdTransaction()) === false) {
$entity = $this->tm->storeTransaction($transaction);
}
$variable = (string) $transaction->getVariableSymbol();
if ($variable !== '' && isset($orderByVariable[$variable])) {
$order = $orderByVariable[$variable];
$order->setPaid(true);
$this->orderStatusManager->setStatus($order, OrderStatus::STATUS_PAID);
$entity?->setOrder($order);
}
$this->entityManager->flush();
};

try {
$authorizator->authOrders(
$unauthorizedVariables,
$callback,
$currency->getCode(),
0.25,
);
} catch (\Throwable $e) {
throw new \RuntimeException(sprintf('Can not authorize orders: %s', $e->getMessage()), 500, $e);
}
$this->entityManager->flush();
};

try {
$authorizator->authOrders(
$unauthorizedVariables,
$callback,
'CZK',
0.25,
);
} catch (\Throwable $e) {
throw new \RuntimeException(sprintf('Can not authorize orders: %s', $e->getMessage()), 500, $e);
}
}

Expand All @@ -202,4 +204,53 @@ private function checkSentOrders(): void
$this->orderStatusManager->setStatus($order, OrderStatus::STATUS_DONE, force: true);
}
}


/**
* @param array<int, Order> $orders
* @return array<numeric-string, float>
*/
private function filterUnauthorizedVariables(array $orders): array
{
$return = [];
foreach ($orders as $order) {
/** @var numeric-string $number */
$number = $order->getNumber();
$return[$number] = (float) $order->getPrice()->getValue();
}

return $return;
}


/**
* @param array<int, Order> $orders
* @return array<numeric-string, Order>
*/
private function filterOrderByVariable(array $orders): array
{
$return = [];
foreach ($orders as $order) {
/** @var numeric-string $number */
$number = $order->getNumber();
$return[$number] = $order;
}

return $return;
}


/**
* @param array<int, Order> $orders
* @return array<int, int>
*/
private function filterUnauthorizedVariablesForCheck(array $orders): array
{
$return = [];
foreach ($orders as $order) {
$return[] = (int) $order->getNumber();
}

return $return;
}
}
10 changes: 6 additions & 4 deletions src/Entity/OrderFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OrderFile implements OrderDocument
private Order $order;

#[ORM\Column(type: 'string', length: 64, nullable: true)]
private ?string $number = null;
private ?string $number;

#[ORM\Column(type: 'string', length: 128)]
private string $label;
Expand Down Expand Up @@ -55,9 +55,11 @@ public function getId(): int

public static function getRelativePath(self $orderFile): string
{
return 'order-file/' . $orderFile->getInsertedDate()->format('Y-m-d')
. '/' . $orderFile->getOrder()->getHash()
. '/' . $orderFile->getFilename();
return sprintf('order-file/%s/%s/%s',
$orderFile->getInsertedDate()->format('Y-m-d'),
$orderFile->getOrder()->getHash(),
$orderFile->getFilename(),
);
}


Expand Down

0 comments on commit 562837e

Please sign in to comment.