phpcbf and added .gitea workflows

This commit is contained in:
bs
2026-01-23 20:32:22 -08:00
parent 4b45725b33
commit a033c13b8d
24 changed files with 949 additions and 919 deletions
+1 -2
View File
@@ -5,6 +5,5 @@ namespace CakeCarts\Controller;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
class AppController extends BaseController {
}
+108 -111
View File
@@ -4,10 +4,7 @@ declare(strict_types=1);
namespace CakeCarts\Controller;
use Cake\Core\Configure;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Http\Client;
use Cake\Log\Log;
use CakeCarts\Controller\AppController;
/**
* CartItems Controller
@@ -15,148 +12,148 @@ use CakeCarts\Controller\AppController;
* @property \CakeCarts\Model\Table\CartItemsTable $CartItems
* @property \Authorization\Controller\Component\AuthorizationComponent $Authorization
*/
class CartItemsController extends AppController
{
public function initialize(): void
{
parent::initialize(); // TODO: Change the autogenerated stub
class CartItemsController extends AppController {
if (!$this->components()->has('ShoppingCart')) {
$this->loadComponent('CakeCarts.ShoppingCart', [
// This is default config. You can modify "actions" as needed to make
// component work only for specified methods.
'actions' => ['add'],
]);
}
/**
* @return void
*/
public function initialize(): void {
parent::initialize(); // TODO: Change the autogenerated stub
if ($this->components()->has('Authorization')) {
$this->Authorization->skipAuthorization();
}
}
if (!$this->components()->has('ShoppingCart')) {
$this->loadComponent('CakeCarts.ShoppingCart', [
// This is default config. You can modify "actions" as needed to make
// component work only for specified methods.
'actions' => ['add'],
]);
}
/**
if ($this->components()->has('Authorization')) {
$this->Authorization->skipAuthorization();
}
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$this->request->allowMethod(['post', 'put', 'patch']);
public function add() {
$this->request->allowMethod(['post', 'put', 'patch']);
Log::debug(print_r('$this->request->getData()', true));
Log::debug(print_r($this->request->getData(), true));
$cart = $this->viewBuilder()->getVar('cart');
$postData = $this->request->getData();
$postData['cart_id'] = $cart->id;
// get product skus with variants
$price = $this->request->getData('price');
$qty = $this->request->getData('qty', 1);
$postData['price'] = $price;
$postData['subtotal'] = isset($price) ? bcmul("$price", "$qty", 5) : null;
Log::debug(print_r('$this->request->getData()', true));
Log::debug(print_r($this->request->getData(), true));
$cart = $this->viewBuilder()->getVar('cart');
$postData = $this->request->getData();
$postData['cart_id'] = $cart->id;
// get product skus with variants
$price = $this->request->getData('price');
$qty = $this->request->getData('qty', 1);
$postData['price'] = $price;
$postData['subtotal'] = isset($price) ? bcmul("$price", "$qty", 5) : null;
$newCartItem = $this->CartItems->newEntity($postData, [
'validate' => Configure::readOrFail('CakeCarts.CartItems.requirePricing') ? 'requirePricing' : 'default',
]);
$newCartItem = $this->CartItems->newEntity($postData, [
'validate' => Configure::readOrFail('CakeCarts.CartItems.requirePricing') ? 'requirePricing' : 'default',
]);
if ($this->CartItems->save($newCartItem)) {
$this->Flash->success('Added to cart');
if ($this->CartItems->save($newCartItem)) {
$this->Flash->success('Added to cart');
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index',
]));
}
// Log::debug(print_r('$newCartItem->getErrors()', true));
// Log::debug(print_r($newCartItem->getErrors(), true));
$this->Flash->error('Failed to add to cart.');
$this->Flash->error('Failed to add to cart.');
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index',
]));
}
/**
/**
* Edit method
*
* @param string|null $id Cart Item id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
*/
public function edit($id = null)
{
$this->request->allowMethod(['post', 'put', 'patch']);
public function edit($id = null) {
$this->request->allowMethod(['post', 'put', 'patch']);
$cartItem = $this->CartItems->find()
->where(['CartItems.id' => $id])
->contain(['Carts'])
->firstOrFail();
$cartItem = $this->CartItems->find()
->where(['CartItems.id' => $id])
->contain(['Carts'])
->firstOrFail();
$this->ShoppingCart->checkIfIsOwnCart($cartItem->cart);
$this->ShoppingCart->checkIfIsOwnCart($cartItem->cart);
if ($this->request->is(['patch', 'post', 'put'])) {
$postData = $this->request->getData();
$qty = $this->request->getData('qty', 1);
$price = $this->request->getData('price', null);
$subtotal = isset($price) ? bcmul("$qty", "$price", 5) : null;
$postData['subtotal'] = $subtotal;
$cartItem = $this->CartItems->patchEntity($cartItem, $postData, [
'validate' => Configure::readOrFail('CakeCarts.CartItems.requirePricing') ? 'requiresPricing' : 'default',
]);
if ($this->CartItems->save($cartItem)) {
$this->Flash->success(__('The cart item has been saved.'));
if ($this->request->is(['patch', 'post', 'put'])) {
$postData = $this->request->getData();
$qty = $this->request->getData('qty', 1);
$price = $this->request->getData('price', null);
$subtotal = isset($price) ? bcmul("$qty", "$price", 5) : null;
$postData['subtotal'] = $subtotal;
$cartItem = $this->CartItems->patchEntity($cartItem, $postData, [
'validate' => Configure::readOrFail('CakeCarts.CartItems.requirePricing') ? 'requiresPricing' : 'default',
]);
if ($this->CartItems->save($cartItem)) {
$this->Flash->success(__('The cart item has been saved.'));
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
Log::debug(print_r('$cartItem->getErrors()', true));
Log::debug(print_r($cartItem->getErrors(), true));
$this->Flash->error(__('The cart item could not be saved. Please, try again.'));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index',
]));
}
Log::debug(print_r('$cartItem->getErrors()', true));
Log::debug(print_r($cartItem->getErrors(), true));
$this->Flash->error(__('The cart item could not be saved. Please, try again.'));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index',
]));
}
/**
/**
* Delete method
*
* @param string|null $id Cart Item id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null Redirects to index.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$identity = $this->getRequest()->getAttribute('identity');
public function delete($id = null) {
$this->request->allowMethod(['post', 'delete']);
$identity = $this->getRequest()->getAttribute('identity');
// $cart = $this->viewBuilder()->getVar('cart');
$cartItem = $this->CartItems->find()
->where(['CartItems.id' => $id])
->contain(['Carts'])
->firstOrFail();
$cartItem = $this->CartItems->find()
->where(['CartItems.id' => $id])
->contain(['Carts'])
->firstOrFail();
$this->ShoppingCart->checkIfIsOwnCart($cartItem->cart);
$this->ShoppingCart->checkIfIsOwnCart($cartItem->cart);
unset($cartItem->cart);
if ($this->CartItems->delete($cartItem)) {
$this->Flash->success(__('The cart item has been deleted.'));
} else {
$this->Flash->error(__('The cart item could not be deleted. Please, try again.'));
}
unset($cartItem->cart);
if ($this->CartItems->delete($cartItem)) {
$this->Flash->success(__('The cart item has been deleted.'));
} else {
$this->Flash->error(__('The cart item could not be deleted. Please, try again.'));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index',
]));
}
return $this->redirect($this->referer([
'plugin' => 'CakeCarts',
'controller' => 'CartItems',
'action' => 'index'
]));
}
}
+20 -21
View File
@@ -4,37 +4,36 @@ declare(strict_types=1);
namespace CakeCarts\Controller;
use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\Event\EventInterface;
use CakeCarts\Model\Enum\CartTypeId;
/**
* Carts Controller
*
* @property \Authorization\Controller\Component\AuthorizationComponent $Authorization
*/
class CartsController extends AppController
{
public function initialize(): void
{
parent::initialize(); // TODO: Change the autogenerated stub
class CartsController extends AppController {
if (!$this->components()->has('ShoppingCart')) {
$this->loadComponent('CakeCarts.ShoppingCart', [
// This is default config. You can modify "actions" as needed to make
// component work only for specified methods.
'actions' => true,
]);
}
}
/**
* @return void
*/
public function initialize(): void {
parent::initialize(); // TODO: Change the autogenerated stub
/**
if (!$this->components()->has('ShoppingCart')) {
$this->loadComponent('CakeCarts.ShoppingCart', [
// This is default config. You can modify "actions" as needed to make
// component work only for specified methods.
'actions' => true,
]);
}
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
// use cart from beforeFilter
}
public function index() {
// use cart from beforeFilter
}
}
+103 -106
View File
@@ -10,159 +10,156 @@ use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\EventInterface;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use CakeCarts\Model\Entity\CartItem;
use CakeCarts\Model\Enum\CartTypeId;
use CakeCarts\Model\Table\CartsTable;
/**
* ShoppingCart component
*/
class ShoppingCartComponent extends Component
{
/**
class ShoppingCartComponent extends Component {
/**
* @var string $userIdField
*/
protected string $userIdField;
protected string $userIdField;
/**
* @var CartsTable|Table $Carts
/**
* @var \CakeCarts\Model\Table\CartsTable|\Cake\ORM\Table $Carts
*/
protected CartsTable|Table $Carts;
protected CartsTable|Table $Carts;
/**
* @return void
*/
public function initialize(array $config): void {
parent::initialize($config); // TODO: Change the autogenerated stub
public function initialize(array $config): void
{
parent::initialize($config); // TODO: Change the autogenerated stub
$this->userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'uuid' ? 'user_id_uuid' : 'user_id';
$this->Carts = TableRegistry::getTableLocator()->get(Configure::readOrFail('CakeCarts.Carts.table'));
}
$this->userIdField = Configure::readOrFail('CakeCarts.Users.user_id') === 'uuid' ? 'user_id_uuid' : 'user_id';
$this->Carts = TableRegistry::getTableLocator()->get(Configure::readOrFail('CakeCarts.Carts.table'));
}
/**
/**
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [];
protected array $_defaultConfig = [];
public function beforeFilter(EventInterface $event): void
{
if (!$this->_isActionEnabled()) {
return;
}
/**
* @return void
*/
public function beforeFilter(EventInterface $event): void {
if (!$this->_isActionEnabled()) {
return;
}
$sessionId = $this->getSessionId();
$cart = $this->findExistingCartOrCreate($sessionId);
$sessionId = $this->getSessionId();
$cart = $this->findExistingCartOrCreate($sessionId);
$this->getController()->set(compact('cart'));
}
$this->getController()->set(compact('cart'));
}
/**
/**
* @param string $cartId
*
* @return mixed
*/
public function getCartForUserById(string $cartId)
{
$identity = $this->getController()->getRequest()->getAttribute('identity');
$sessionId = $this->getSessionId();
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]);
}
$cartsQ = $this->Carts
->find()
->contain(['CartItems'])
->where(['Carts.id' => $cartId]);
if ($identity) {
$cartsQ->where([$this->userIdField => $identity->getIdentifier()]);
} else {
$cartsQ->where(['session_id' => $sessionId]);
}
return $cartsQ->firstOrFail();
}
return $cartsQ->firstOrFail();
}
public function findExistingCartOrCreate(string $sessionId, int $cartTypeId = null)
{
$identity = $this->getController()->getRequest()->getAttribute('identity');
public function findExistingCartOrCreate(string $sessionId, int|null $cartTypeId = null) {
$identity = $this->getController()->getRequest()->getAttribute('identity');
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
$cartTypeId = $cartTypeId ?? CartTypeId::Cart->value;
$cart = $this->Carts
->findBySessionId($sessionId)
->contain(['CartItems'])
->where(['cart_type_id' => $cartTypeId])
->first();
$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(),
]);
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);
}
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);
}
$cart = $this->Carts->saveOrFail($cart);
}
return $cart;
}
return $cart;
}
public function getUserIdField()
{
return $this->userIdField;
}
public function getUserIdField() {
return $this->userIdField;
}
/**
/**
* @return string
*/
public function getSessionId(): string
{
if (!$this->getController()->getRequest()->getSession()->started()) {
$this->getController()->getRequest()->getSession()->start();
}
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());
}
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');
}
return $this->getController()->getRequest()->getSession()->read('CakeCarts.session_id');
}
/**
* @param EntityInterface $cart
* @throws RecordNotFoundException
/**
* @param \Cake\Datasource\EntityInterface $cart
* @throws \Cake\Datasource\Exception\RecordNotFoundException
*
* @return void
*/
public function checkIfIsOwnCart(EntityInterface $cart): void
{
$identity = $this->getController()->getRequest()->getAttribute('identity');
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();
}
}
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
*/
protected function _isActionEnabled(): bool
{
$actions = $this->getConfig('actions');
if (is_bool($actions)) {
return $actions;
}
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);
}
return in_array($this->getController()->getRequest()->getParam('action'), (array)$actions, true);
}
}