Files
cake-carts/src/Controller/Component/ShoppingCartComponent.php

171 lines
4.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace CakeCarts\Controller\Component;
use Cake\Controller\Component;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\EventInterface;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use CakeCarts\Model\Enum\CartTypeId;
use CakeCarts\Model\Table\CartsTable;
/**
* ShoppingCart component
*/
2026-01-23 20:32:22 -08:00
class ShoppingCartComponent extends Component {
protected string $userIdField;
2026-01-23 20:32:22 -08:00
protected CartsTable|Table $Carts;
2026-01-23 20:32:22 -08:00
/**
2026-01-23 20:42:17 -08:00
* @param array $config
2026-01-23 20:32:22 -08:00
* @return void
*/
public function initialize(array $config): void {
parent::initialize($config); // TODO: Change the autogenerated stub
2026-01-23 20:32:22 -08:00
$this->userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'uuid' ? 'user_id_uuid' : 'user_id';
$this->Carts = TableRegistry::getTableLocator()->get(Configure::readOrFail('CakeCarts.Carts.table'));
}
2026-01-23 20:32:22 -08:00
/**
* Default configuration.
*
* @var array<string, mixed>
*/
2026-01-23 20:32:22 -08:00
protected array $_defaultConfig = [];
2026-01-23 20:32:22 -08:00
/**
2026-01-23 20:42:17 -08:00
* @param \Cake\Event\EventInterface $event
2026-01-23 20:32:22 -08:00
* @return void
*/
public function beforeFilter(EventInterface $event): void {
if (!$this->_isActionEnabled()) {
return;
}
2026-01-23 20:32:22 -08:00
$sessionId = $this->getSessionId();
$cart = $this->findExistingCartOrCreate($sessionId);
2026-01-23 20:32:22 -08:00
$this->getController()->set(compact('cart'));
}
2026-01-23 20:32:22 -08:00
/**
* @param string $cartId
*
2026-01-23 20:42:17 -08:00
* @return \Cake\Datasource\EntityInterface|\CakeCarts\Model\Entity\Cart
*/
2026-01-23 20:32:22 -08:00
public function getCartForUserById(string $cartId) {
$identity = $this->getController()->getRequest()->getAttribute('identity');
$sessionId = $this->getSessionId();
$cartsQ = $this->Carts
->find()
->contain(['CartItems'])
->where(['Carts.id' => $cartId]);
if ($identity) {
$cartsQ->where([$this->userIdField => $identity->getIdentifier()]);
} else {
$cartsQ->where(['session_id' => $sessionId]);
}
2026-02-11 19:27:09 -08:00
2026-01-23 20:32:22 -08:00
return $cartsQ->firstOrFail();
}
2026-01-23 20:42:17 -08:00
/**
* @param string $sessionId
* @param int|null $cartTypeId
* @return EntityInterface|\CakeCarts\Model\Entity\Cart
*/
2026-01-23 20:32:22 -08:00
public function findExistingCartOrCreate(string $sessionId, int|null $cartTypeId = null) {
$identity = $this->getController()->getRequest()->getAttribute('identity');
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
$cart = $this->Carts
->findBySessionId($sessionId)
->contain(['CartItems'])
->where(['cart_type_id' => $cartTypeId])
->first();
if (isset($cart) && isset($identity) && !isset($cart[$this->userIdField])) {
$cart = $this->Carts->patchEntity($cart, [
$this->userIdField => $identity->getIdentifier(),
]);
$cart = $this->Carts->saveOrFail($cart);
}
if (!isset($cart)) {
$cart = $this->Carts->newEntity([
'cart_type_id' => $cartTypeId,
'session_id' => $sessionId,
$this->userIdField => isset($identity) ? $identity->getIdentifier() : null,
'num_items' => 0,
'cart_items' => [],
]);
$cart = $this->Carts->saveOrFail($cart);
}
return $cart;
}
2026-01-23 20:42:17 -08:00
/**
* @return string
*/
2026-01-23 20:32:22 -08:00
public function getUserIdField() {
return $this->userIdField;
}
/**
* @return string
*/
2026-01-23 20:32:22 -08:00
public function getSessionId(): string {
if (!$this->getController()->getRequest()->getSession()->started()) {
$this->getController()->getRequest()->getSession()->start();
}
if (!$this->getController()->getRequest()->getSession()->check('CakeCarts.session_id')) {
$this->getController()->getRequest()->getSession()->write('CakeCarts.session_id', $this->getController()->getRequest()->getSession()->id());
}
return $this->getController()->getRequest()->getSession()->read('CakeCarts.session_id');
}
/**
* @param \Cake\Datasource\EntityInterface $cart
* @throws \Cake\Datasource\Exception\RecordNotFoundException
*
* @return void
*/
2026-01-23 20:32:22 -08:00
public function checkIfIsOwnCart(EntityInterface $cart): void {
$identity = $this->getController()->getRequest()->getAttribute('identity');
if (!isset($identity) && isset($cart->session_id) && ($cart->session_id != $this->getSessionId())) {
throw new RecordNotFoundException();
}
if (isset($identity) && $identity->getIdentifier() != $cart->get($this->getUserIdField())) {
throw new RecordNotFoundException();
}
}
/**
* @return bool
*/
2026-01-23 20:32:22 -08:00
protected function _isActionEnabled(): bool {
$actions = $this->getConfig('actions');
if (is_bool($actions)) {
return $actions;
}
return in_array($this->getController()->getRequest()->getParam('action'), (array)$actions, true);
}
}