first commit
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KupShop\AgeVerifyBundle\EventSubscribers;
|
||||
|
||||
use KupShop\AgeVerifyBundle\Utils\AdultoUtil;
|
||||
use KupShop\KupShopBundle\Context\UserContext;
|
||||
use KupShop\OrderingBundle\Event\CartEvent;
|
||||
use KupShop\OrderingBundle\Exception\CartValidationException;
|
||||
use Query\Operator;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Contracts\Service\Attribute\Required;
|
||||
|
||||
class CartSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
|
||||
{
|
||||
#[Required]
|
||||
public UserContext $userContext;
|
||||
#[Required]
|
||||
public AdultoUtil $adultoUtil;
|
||||
|
||||
#[Required]
|
||||
public RequestStack $requestStack;
|
||||
|
||||
protected int $lastStepIndex = 2;
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
CartEvent::CHECK_CART_ITEMS => [
|
||||
['checkLegalAge', 200],
|
||||
],
|
||||
CartEvent::CHECK_CART => [
|
||||
['checkCartLegalAge', 200],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function checkCartLegalAge(CartEvent $event)
|
||||
{
|
||||
$cfg = \Settings::getDefault();
|
||||
if (($cfg['age_verify']['enabled'] ?? 'N') == 'N') {
|
||||
return;
|
||||
}
|
||||
$cart = $event->getCart();
|
||||
|
||||
if ($cart->getActualStepIndex() != $this->lastStepIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($cart->getData('age_verification')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$mainRequest = $this->requestStack->getMainRequest();
|
||||
|
||||
if (!empty($cfg['age_verify']['adulto']['enabled'])) {
|
||||
$result = $this->adultoUtil->checkAdultoUid($mainRequest->get('adultocz-uid') ?? '');
|
||||
if (!$result['success']) {
|
||||
throw new CartValidationException(translate('registered_with_legal_age', 'AgeVerify'));
|
||||
} else {
|
||||
$cart->setData('age_verification', $result['data']);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->checkAgeRegisteredUser($cart);
|
||||
}
|
||||
|
||||
public function checkLegalAge(CartEvent $event)
|
||||
{
|
||||
$cfg = \Settings::getDefault();
|
||||
if (($cfg['age_verify']['enabled'] ?? 'N') == 'N') {
|
||||
return;
|
||||
}
|
||||
|
||||
$cart = $event->getCart();
|
||||
|
||||
if ($cart->getActualStepIndex() != $this->lastStepIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($cart->getData('age_verification')) {
|
||||
return;
|
||||
}
|
||||
if (!empty($cfg['age_verify']['adulto']['enabled'])) {
|
||||
return;
|
||||
}
|
||||
$this->checkAgeRegisteredUser($cart);
|
||||
}
|
||||
|
||||
protected function checkAgeRegisteredUser(\Cart $cart): void
|
||||
{
|
||||
$user = $this->userContext->getActive();
|
||||
|
||||
if (!$user) {
|
||||
throw new CartValidationException(translate('registered_with_legal_age', 'AgeVerify'));
|
||||
}
|
||||
|
||||
$legalAge = sqlQueryBuilder()->select('legal_age')->from('users_age_verification')->where(Operator::equals(['id_user' => $user->id]))->execute()->fetchOne();
|
||||
|
||||
if (empty($legalAge)) {
|
||||
throw new CartValidationException(translate('dont_verify', 'AgeVerify'));
|
||||
} elseif ($legalAge == 'N') {
|
||||
throw new CartValidationException(translate('not_legal_age', 'AgeVerify'));
|
||||
}
|
||||
|
||||
if ($deliveryBirthdate = $cart->getData('delivery_birthdate')) {
|
||||
$date = \DateTime::createFromFormat('Y-m-d', $deliveryBirthdate)->add(\DateInterval::createFromDateString('+18YEARS'));
|
||||
if ($date > (new \DateTime())) {
|
||||
throw new CartValidationException(translate('delivery_legal_age', 'AgeVerify'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KupShop\AgeVerifyBundle\EventSubscribers;
|
||||
|
||||
use KupShop\OrderingBundle\Event\OrderDeliveryEvent;
|
||||
use Query\Operator;
|
||||
|
||||
class OrderEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
OrderDeliveryEvent::class => [
|
||||
['updateLegalAge', 200],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function updateLegalAge(OrderDeliveryEvent $event)
|
||||
{
|
||||
$orderId = $event->getOrder()->id;
|
||||
sqlQueryBuilder()->update('users_age_verification')
|
||||
->directValues(['legal_age' => 'Y'])
|
||||
->where(Operator::equals(['id_order' => $orderId]))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user