feat: clean up and starting documentation

This commit is contained in:
Joel Male
2025-02-25 14:53:49 +10:00
parent 8d6e1bcc26
commit ec99aa0563
9 changed files with 111 additions and 73 deletions
+35 -15
View File
@@ -27,15 +27,38 @@ class Cart
protected $decimals;
protected $decPoint;
public function __construct($session, $events, $instanceName, $sessionKey, $config)
public function __construct($sessionStorage, $events, $instanceName, $sessionKey, $config)
{
$this->validateConfig($config);
$this->events = $events;
$this->session = new CartSession($session, $sessionKey, $config);
$this->session = new CartSession($sessionStorage, $sessionKey, $config);
$this->instanceName = $instanceName;
$this->config = $config;
$this->currentItemId = null;
$this->fireEvent('created');
$this->fireEvent('Created');
}
private function validateConfig($config)
{
if ($config['driver'] == 'database') {
if (! isset($config['storage']['database']['model'])) {
throw new \Exception('You need to set a model for the database driver in the cart configuration.');
}
if (! isset($config['storage']['database']['id'])) {
throw new \Exception('You need to set an id (for the session) for the database driver in the cart configuration.');
}
if (! isset($config['storage']['database']['items'])) {
throw new \Exception('You need to set an items key for the database driver in the cart configuration.');
}
if (! isset($config['storage']['database']['conditions'])) {
throw new \Exception('You need to set a conditions key for the database driver in the cart configuration.');
}
}
}
/**
@@ -145,7 +168,7 @@ public function add(
*/
public function update(int|string $id, array $data): bool
{
if ($this->fireEvent('updating', $data) === false) {
if ($this->fireEvent('Updating', $data) === false) {
return false;
}
@@ -178,7 +201,7 @@ public function update(int|string $id, array $data): bool
$this->save($cart);
$this->fireEvent('updated', $item);
$this->fireEvent('Updated', $item);
return true;
}
@@ -237,7 +260,7 @@ public function remove($id)
{
$cart = $this->getContent();
if ($this->fireEvent('removing', $id) === false) {
if ($this->fireEvent('Removing', $id) === false) {
return false;
}
@@ -245,7 +268,7 @@ public function remove($id)
$this->save($cart);
$this->fireEvent('removed', $id);
$this->fireEvent('Removed', $id);
return true;
}
@@ -257,13 +280,13 @@ public function remove($id)
*/
public function clear()
{
if ($this->fireEvent('clearing') === false) {
if ($this->fireEvent('Clearing') === false) {
return false;
}
$this->session->putItems([]);
$this->fireEvent('cleared');
$this->fireEvent('Cleared');
return true;
}
@@ -691,7 +714,7 @@ protected function validate($item)
*/
protected function addItem($id, $item)
{
if ($this->fireEvent('adding', $item) === false) {
if ($this->fireEvent('Adding', $item) === false) {
return false;
}
@@ -701,7 +724,7 @@ protected function addItem($id, $item)
$this->save($cart);
$this->fireEvent('added', $item);
$this->fireEvent('Added', $item);
return true;
}
@@ -792,12 +815,9 @@ public function setDecimals($decimals)
$this->decimals = $decimals;
}
/**
* @return mixed
*/
protected function fireEvent($name, $value = [])
{
return $this->events->dispatch($this->getInstanceName() . '.' . $name, array_values([$value, $this]), true);
return $this->events->dispatch('LaravelCart.' . $name, array_values([$value, $this]), true);
}
/**
+8 -13
View File
@@ -35,24 +35,19 @@ public function register()
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'cart');
$this->app->singleton('cart', function ($app) {
$storageClass = config('cart.storage');
$eventsClass = config('cart.events');
$config = config('cart');
$events = $app['events'];
$storage = $storageClass ? new $storageClass : $app['session'];
$events = $eventsClass ? new $eventsClass : $app['events'];
$instanceName = 'cart';
// default session or cart identifier. This will be overridden when calling Cart::session($sessionKey)->add() etc..
// like when adding a cart for a specific user name. Session Key can be string or maybe a unique identifier to bind a cart
// to a specific user, this can also be a user ID
$session_key = '4yTlTDKu3oJOfzD';
$storage = $config['driver'] === 'database'
? new $config['storage']['database']['model']
: $app['session'];
return new Cart(
$storage,
$events,
$instanceName,
$session_key,
config('cart')
'cart',
'4yTlTDKu3oJOfzD',
$config
);
});
}
+8 -9
View File
@@ -14,23 +14,22 @@ class CartSession
public function __construct($session, $sessionKey, $config)
{
if ($this->isInternalSession($session)) {
if ($config['driver'] === 'session') {
$this->customStorage = false;
$this->session = $session;
$this->itemsKey = $sessionKey . '_cart_items';
$this->conditionsKey = $sessionKey . '_cart_conditions';
} else {
} elseif ($config['driver'] === 'database') {
$this->customStorage = true;
// @TODO validate these 3 exist
$this->sessionId = $config['storage_id'];
$this->itemsKey = $config['storage_items'];
$this->conditionsKey = $config['storage_conditions'];
$this->sessionId = $config['storage']['database']['id'];
$this->itemsKey = $config['storage']['database']['items'];
$this->conditionsKey = $config['storage']['database']['conditions'];
// get the base class to do findOrCreate
$this->session = $config['storage'];
$this->session = (new $this->session)->firstOrNew([$this->sessionId => $sessionKey]);
// find or create the session in the database
$this->session = $session->firstOrNew([$this->sessionId => $sessionKey]);
// initialize the items and conditions
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
$this->session[$this->conditionsKey] = $this->session[$this->conditionsKey] ?? [];
}