54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerCoupon\Block\Account;
|
|
|
|
use Esun\CustomerCoupon\Model\Coupon\ListProvider;
|
|
use Magento\Customer\Model\Session as CustomerSession;
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
|
|
class Coupons extends Template
|
|
{
|
|
private ?array $coupons = null;
|
|
|
|
public function __construct(
|
|
Template\Context $context,
|
|
private readonly CustomerSession $customerSession,
|
|
private readonly StoreManagerInterface $storeManager,
|
|
private readonly ListProvider $listProvider,
|
|
array $data = []
|
|
) {
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
public function getAvailableCoupons(): array
|
|
{
|
|
return array_values(array_filter(
|
|
$this->getCoupons(),
|
|
static fn(array $coupon): bool => $coupon['status'] === 'available'
|
|
));
|
|
}
|
|
|
|
public function getUnavailableCoupons(): array
|
|
{
|
|
return array_values(array_filter(
|
|
$this->getCoupons(),
|
|
static fn(array $coupon): bool => $coupon['status'] !== 'available'
|
|
));
|
|
}
|
|
|
|
private function getCoupons(): array
|
|
{
|
|
if ($this->coupons === null) {
|
|
$this->coupons = $this->listProvider->getForCustomer(
|
|
(int)$this->customerSession->getCustomerId(),
|
|
(int)$this->storeManager->getStore()->getWebsiteId(),
|
|
(int)$this->customerSession->getCustomerGroupId()
|
|
);
|
|
}
|
|
|
|
return $this->coupons;
|
|
}
|
|
}
|