Skip to content

Commit

Permalink
[Core] Ensure sane repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
BitcoinMitchell committed Feb 20, 2024
1 parent 3ac28d1 commit 6a2ec45
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 267 deletions.
41 changes: 7 additions & 34 deletions modules/btcpay/btcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
use BTCPay\Installer\OrderStates;
use BTCPay\Installer\Tables;
use BTCPay\Installer\Webhook;
use BTCPay\LegacyBitcoinPaymentRepository;
use BTCPay\Repository\BitcoinPaymentRepository;
use BTCPay\Repository\TableRepository;
use BTCPay\Server\Client;
use BTCPayServer\Exception\BTCPayException;
use BTCPayServer\Exception\RequestException;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Adapter\Presenter\Order\OrderPresenter;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;

if (!defined('_PS_VERSION_')) {
Expand All @@ -39,11 +38,6 @@ class BTCPay extends PaymentModule
],
];

/**
* @var BitcoinPaymentRepository
*/
private $repository;

/**
* @var Configuration
*/
Expand Down Expand Up @@ -123,8 +117,8 @@ public function install(): bool
return false;
}

if (null === ($repository = $this->getRepository())) {
$this->addModuleErrors(['Expected a BitcoinPaymentRepository repository, but received NULL']);
if (null === ($repository = $this->get('prestashop.module.btcpay.repository.install')) || false === $repository instanceof TableRepository) {
$this->addModuleErrors(['Expected a TableRepository repository, but received null/something else']);

return false;
}
Expand Down Expand Up @@ -223,11 +217,8 @@ public function hookDisplayAdminOrderMainBottom($params): ?string
return null;
}

// Get legacy repository
$repository = new LegacyBitcoinPaymentRepository();

// Get the order
if (null === ($bitcoinPayment = $repository->getOneByOrderID($params['id_order']))) {
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByOrderID($params['id_order']))) {
return null;
}

Expand Down Expand Up @@ -322,11 +313,8 @@ public function hookDisplayOrderDetail(array $params): ?string
return null;
}

// Get legacy repository
$repository = new LegacyBitcoinPaymentRepository();

// Get the order
if (null === ($bitcoinPayment = $repository->getOneByOrderID($order->id))) {
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByOrderID($order->id))) {
return null;
}

Expand Down Expand Up @@ -381,7 +369,7 @@ public function hookPaymentReturn($params): ?string
}

// Get the order
if (null === ($bitcoinPayment = (new LegacyBitcoinPaymentRepository())->getOneByOrderID($order->id))) {
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByOrderID($order->id))) {
return null;
}

Expand Down Expand Up @@ -478,7 +466,7 @@ public function hookActionCartSave(array $params): void
}

// Get the order
if (null === ($bitcoinPayment = (new LegacyBitcoinPaymentRepository())->getOneByCartID($cart->id))) {
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByCartID($cart->id))) {
return;
}

Expand All @@ -500,21 +488,6 @@ private function addModuleErrors(array $errors): void
}
}

private function getRepository(): ?BitcoinPaymentRepository
{
if (null === $this->repository) {
try {
$this->repository = $this->get('prestashop.module.btcpay.repository');
} catch (Throwable $e) {
if (null !== ($container = SymfonyContainer::getInstance())) {
$this->repository = new BitcoinPaymentRepository($container->get('doctrine.dbal.default_connection'), $container->getParameter('database_prefix'));
}
}
}

return $this->repository;
}

private function displayModuleWarnings(): void
{
// Try and create the client
Expand Down
4 changes: 2 additions & 2 deletions modules/btcpay/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ services:
class: BTCPay

# Repositories
prestashop.module.btcpay.repository:
class: BTCPay\Repository\BitcoinPaymentRepository
prestashop.module.btcpay.repository.install:
class: BTCPay\Repository\TableRepository
arguments:
- '@doctrine.dbal.default_connection'
- '%database_prefix%'
Expand Down
10 changes: 2 additions & 8 deletions modules/btcpay/controllers/front/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use BTCPay\Constants;
use BTCPay\Invoice\Processor;
use BTCPay\LegacyBitcoinPaymentRepository;
use BTCPay\Repository\BitcoinPaymentRepository;
use BTCPay\Server\Client;
use PrestaShop\PrestaShop\Adapter\Configuration;

Expand All @@ -25,17 +25,11 @@ class BTCPayValidationModuleFrontController extends ModuleFrontController
*/
private $configuration;

/**
* @var LegacyBitcoinPaymentRepository
*/
private $repository;

public function __construct()
{
parent::__construct();

$this->configuration = new Configuration();
$this->repository = new LegacyBitcoinPaymentRepository();
}

/**
Expand Down Expand Up @@ -78,7 +72,7 @@ public function postProcess(): void

// Get the passed invoice reference, which we can then use to get the actual order
$invoiceReference = Tools::getValue('invoice_reference', 0);
if (null === ($bitcoinPayment = $this->repository->getOneByInvoiceReference($invoiceReference))) {
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByInvoiceReference($invoiceReference))) {
$this->warning[] = $translator->trans('The passed invoice reference is not valid.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));

Expand Down
3 changes: 1 addition & 2 deletions modules/btcpay/controllers/front/webhook.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use BTCPay\Constants;
use BTCPay\LegacyBitcoinPaymentRepository;
use BTCPay\Server\Client;
use BTCPay\Server\WebhookHandler;
use PrestaShop\PrestaShop\Adapter\Configuration;
Expand Down Expand Up @@ -42,7 +41,7 @@ public function __construct()

$this->configuration = new Configuration();
$this->client = Client::createFromConfiguration($this->configuration);
$this->handler = new WebhookHandler($this->module, $this->context, $this->client, new LegacyBitcoinPaymentRepository());
$this->handler = new WebhookHandler($this->module, $this->context, $this->client);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/btcpay/src/Factory/CustomerMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function create(\CustomerThread $ct, string $message): \CustomerMe
public static function addToOrder(\Shop $shop, \Order $order, string $message): void
{
// Find or create the customer thread
$ct = CustomerThreadRepository::fetchOrCreateByByEmailAndOrder($shop, $order);
$ct = CustomerThreadRepository::fetchOrCreate($shop, $order);

// Ensure the thread is open
$ct->status = 'open';
Expand Down
14 changes: 7 additions & 7 deletions modules/btcpay/src/Installer/Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace BTCPay\Installer;

use BTCPay\Repository\BitcoinPaymentRepository;
use BTCPay\Repository\TableRepository;

class Tables
{
/**
* @var BitcoinPaymentRepository
* @var TableRepository
*/
private $repository;
private $tableRepository;

public function __construct(BitcoinPaymentRepository $repository)
public function __construct(TableRepository $repository)
{
$this->repository = $repository;
$this->tableRepository = $repository;
}

/**
Expand All @@ -22,7 +22,7 @@ public function __construct(BitcoinPaymentRepository $repository)
*/
public function install(): array
{
return $this->repository->createTables();
return $this->tableRepository->createTables();
}

/**
Expand All @@ -31,6 +31,6 @@ public function install(): array
*/
public function uninstall(): array
{
return $this->repository->dropTables();
return $this->tableRepository->dropTables();
}
}
135 changes: 0 additions & 135 deletions modules/btcpay/src/LegacyBitcoinPaymentRepository.php

This file was deleted.

Loading

0 comments on commit 6a2ec45

Please sign in to comment.