Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface msCartInterface
*
* @return boolean
*/
public function initialize($ctx = 'web');
public function initialize(string $ctx = 'web'): bool;

/**
* Adds product to cart
Expand All @@ -22,7 +22,7 @@ public function initialize($ctx = 'web');
*
* @return array|string $response
*/
public function add($id, $count = 1, $options = []);
public function add(int $id, int $count = 1, array $options = []);

/**
* Removes product from cart
Expand All @@ -31,7 +31,7 @@ public function add($id, $count = 1, $options = []);
*
* @return array|string $response
*/
public function remove($key);
public function remove(string $key);

/**
* Changes products count in cart
Expand All @@ -41,7 +41,7 @@ public function remove($key);
*
* @return array|string $response
*/
public function change($key, $count);
public function change(string $key, int $count);

/**
* Cleans the cart
Expand All @@ -57,14 +57,14 @@ public function clean();
*
* @return array $status
*/
public function status($data = []);
public function status(array $data = []): array;

/**
* Returns the cart items
*
* @return array $cart
*/
public function get();
public function get(): array;

/**
* Set all the cart items by one array
Expand All @@ -73,5 +73,5 @@ public function get();
*
* @return void
*/
public function set($cart = []);
public function set(array $cart = []): void;
}
62 changes: 43 additions & 19 deletions core/components/minishop2/handlers/mscarthandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(miniShop2 $ms2, array $config = [])
*
* @return bool
*/
public function initialize($ctx = 'web')
public function initialize(string $ctx = 'web') : bool
{
$ms2_cart_context = (bool)$this->modx->getOption('ms2_cart_context', null, '0', true);
if ($ms2_cart_context) {
Expand All @@ -74,7 +74,7 @@ public function initialize($ctx = 'web')
*
* @return array|string
*/
public function add($id, $count = 1, $options = [])
public function add(int $id, int $count = 1, array $options = [])
{
if (empty($id) || !is_numeric($id)) {
return $this->error('ms2_cart_add_err_id');
Expand Down Expand Up @@ -164,12 +164,15 @@ public function add($id, $count = 1, $options = [])
return $this->error($response['message']);
}

$changes = $this->getChanges('add', $count, 0);

return $this->success(
'ms2_cart_add_success',
$this->status([
'key' => $key,
'cart' => $this->cart,
'row' => $this->cart[$key]
'row' => $this->cart[$key],
'changes' => $changes
]),
['count' => $count]
);
Expand All @@ -180,12 +183,14 @@ public function add($id, $count = 1, $options = [])
*
* @return array|string
*/
public function remove($key)
public function remove(string $key)
{
if (!array_key_exists($key, $this->cart)) {
return $this->error('ms2_cart_remove_error');
}

$product = $this->cart[$key];

$response = $this->ms2->invokeEvent('msOnBeforeRemoveFromCart', ['key' => $key, 'cart' => $this]);
if (!$response['success']) {
return $this->error($response['message']);
Expand All @@ -198,11 +203,14 @@ public function remove($key)
return $this->error($response['message']);
}

$changes = $this->getChanges('remove',0, $product['count']);

return $this->success(
'ms2_cart_remove_success',
$this->status([
'cart' => $this->cart,
'row' => $row
'row' => $row,
'changes' => $changes
])
);
}
Expand All @@ -213,11 +221,10 @@ public function remove($key)
*
* @return array|string
*/
public function change($key, $count)
public function change(string $key, int $count)
{
$status = [];
if (!array_key_exists($key, $this->cart)) {
return $this->error('ms2_cart_change_error', $this->status($status));
return $this->error('ms2_cart_change_error', $this->status());
}

if ($count <= 0) {
Expand All @@ -237,6 +244,9 @@ public function change($key, $count)
}

$count = $response['data']['count'];

$changes = $this->getChanges('change', $count, $this->cart[$key]['count']);

$this->cart = $this->storageHandler->change($key, $count);
$response = $this->ms2->invokeEvent(
'msOnChangeInCart',
Expand All @@ -245,10 +255,14 @@ public function change($key, $count)
if (!$response['success']) {
return $this->error($response['message']);
}
$status['key'] = $key;
$status['cost'] = $count * $this->cart[$key]['price'];
$status['cart'] = $this->cart;
$status['row'] = $this->cart[$key];

$status = [
'key' => $key,
'cost' => $count * $this->cart[$key]['price'],
'cart' => $this->cart,
'row' => $this->cart[$key],
'changes' => $changes
];

return $this->success(
'ms2_cart_change_success',
Expand Down Expand Up @@ -282,7 +296,7 @@ public function clean()
*
* @return array
*/
public function status($data = [])
public function status(array $data = []): array
{
$status = [
'total_count' => 0,
Expand Down Expand Up @@ -316,7 +330,7 @@ public function status($data = [])
/**
* @return array
*/
public function get()
public function get() : array
{
$cart = [];
foreach ($this->cart as $key => $item) {
Expand All @@ -331,15 +345,15 @@ public function get()
/**
* @param array $cart
*/
public function set($cart = [])
public function set(array $cart = []) : void
{
$this->cart = $this->storageHandler->set($cart);
}

/**
* Set controller for Cart
*/
protected function storageInit()
protected function storageInit(): void
{
switch ($this->storage) {
case 'session':
Expand All @@ -362,7 +376,7 @@ protected function storageInit()
*
* @return array|string
*/
public function error($message = '', $data = [], $placeholders = [])
public function error(string $message = '', array $data = [], array $placeholders = [])
{
return $this->ms2->error($message, $data, $placeholders);
}
Expand All @@ -376,7 +390,7 @@ public function error($message = '', $data = [], $placeholders = [])
*
* @return array|string
*/
public function success($message = '', $data = [], $placeholders = [])
public function success(string $message = '', array $data = [], array $placeholders = [])
{
return $this->ms2->success($message, $data, $placeholders);
}
Expand All @@ -389,7 +403,7 @@ public function success($message = '', $data = [], $placeholders = [])
* @return string
*
*/
protected function getProductKey(array $product, array $options = [])
protected function getProductKey(array $product, array $options = []): string
{
$key_fields = explode(',', $this->config['cart_product_key_fields']);
$product['options'] = $options;
Expand All @@ -407,4 +421,14 @@ protected function getProductKey(array $product, array $options = [])

return 'ms' . md5($key);
}

protected function getChanges(string $method, int $count, int $oldCount): array
{
return [
'method' => $method,
'count' => $count,
'old_count' => $oldCount,
'delta' => $count - $oldCount
];
}
}
Loading