Code formatting & quality

Cleaned up __get methods
This commit is contained in:
Patrick Henninger
2019-01-08 20:53:41 +01:00
parent dbfe5e01fd
commit 52a56768ba
2 changed files with 83 additions and 101 deletions

View File

@@ -110,7 +110,8 @@ class Cart
*/ */
public function add($id, $name = null, $qty = null, $price = null, array $options = []) public function add($id, $name = null, $qty = null, $price = null, array $options = [])
{ {
if ($this->isMulti($id)) { if ($this->isMulti($id))
{
return array_map(function ($item) { return array_map(function ($item) {
return $this->add($item); return $this->add($item);
}, $id); }, $id);
@@ -139,9 +140,8 @@ class Cart
$content = $this->getContent(); $content = $this->getContent();
if ($content->has($item->rowId)) { if ($content->has($item->rowId))
$item->qty += $content->get($item->rowId)->qty; $item->qty += $content->get($item->rowId)->qty;
}
$content->put($item->rowId, $item); $content->put($item->rowId, $item);
@@ -163,31 +163,33 @@ class Cart
{ {
$cartItem = $this->get($rowId); $cartItem = $this->get($rowId);
if ($qty instanceof Buyable) { if ($qty instanceof Buyable)
$cartItem->updateFromBuyable($qty); $cartItem->updateFromBuyable($qty);
} elseif (is_array($qty)) { elseif (is_array($qty))
$cartItem->updateFromArray($qty); $cartItem->updateFromArray($qty);
} else { else
$cartItem->qty = $qty; $cartItem->qty = $qty;
}
$content = $this->getContent(); $content = $this->getContent();
if ($rowId !== $cartItem->rowId) { if ($rowId !== $cartItem->rowId)
{
$content->pull($rowId); $content->pull($rowId);
if ($content->has($cartItem->rowId)) { if ($content->has($cartItem->rowId))
{
$existingCartItem = $this->get($cartItem->rowId); $existingCartItem = $this->get($cartItem->rowId);
$cartItem->setQuantity($existingCartItem->qty + $cartItem->qty); $cartItem->setQuantity($existingCartItem->qty + $cartItem->qty);
} }
} }
if ($cartItem->qty <= 0) { if ($cartItem->qty <= 0)
{
$this->remove($cartItem->rowId); $this->remove($cartItem->rowId);
return; return;
} else {
$content->put($cartItem->rowId, $cartItem);
} }
else
$content->put($cartItem->rowId, $cartItem);
$this->events->fire('cart.updated', $cartItem); $this->events->fire('cart.updated', $cartItem);
@@ -248,10 +250,8 @@ class Cart
*/ */
public function content() public function content()
{ {
if (is_null($this->session->get($this->instance))) { if (is_null($this->session->get($this->instance)))
return new Collection([]); return new Collection([]);
}
return $this->session->get($this->instance); return $this->session->get($this->instance);
} }
@@ -445,9 +445,8 @@ class Cart
*/ */
public function associate($rowId, $model) public function associate($rowId, $model)
{ {
if(is_string($model) && ! class_exists($model)) { if(is_string($model) && ! class_exists($model))
throw new UnknownModelException("The supplied model {$model} does not exist."); throw new UnknownModelException("The supplied model {$model} does not exist.");
}
$cartItem = $this->get($rowId); $cartItem = $this->get($rowId);
@@ -491,7 +490,8 @@ class Cart
$this->taxRate = $taxRate; $this->taxRate = $taxRate;
$content = $this->getContent(); $content = $this->getContent();
if ($content && $content->count()) { if ($content && $content->count())
{
$content->each(function ($item, $key) { $content->each(function ($item, $key) {
$item->setTaxRate($this->taxRate); $item->setTaxRate($this->taxRate);
}); });
@@ -529,7 +529,8 @@ class Cart
$this->discount = $discount; $this->discount = $discount;
$content = $this->getContent(); $content = $this->getContent();
if ($content && $content->count()) { if ($content && $content->count())
{
$content->each(function ($item, $key) { $content->each(function ($item, $key) {
$item->setDiscountRate($this->discount); $item->setDiscountRate($this->discount);
}); });
@@ -546,9 +547,8 @@ class Cart
{ {
$content = $this->getContent(); $content = $this->getContent();
if ($this->storedCartWithIdentifierExists($identifier)) { if ($this->storedCartWithIdentifierExists($identifier))
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored."); throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
}
$this->getConnection()->table($this->getTableName())->insert([ $this->getConnection()->table($this->getTableName())->insert([
'identifier' => $identifier, 'identifier' => $identifier,
@@ -567,9 +567,8 @@ class Cart
*/ */
public function restore($identifier) public function restore($identifier)
{ {
if( ! $this->storedCartWithIdentifierExists($identifier)) { if( ! $this->storedCartWithIdentifierExists($identifier))
return; return;
}
$stored = $this->getConnection()->table($this->getTableName()) $stored = $this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->first(); ->where('identifier', $identifier)->first();
@@ -606,18 +605,16 @@ class Cart
*/ */
public function merge( $identifier, $keepDiscount = false, $keepTax = false ) public function merge( $identifier, $keepDiscount = false, $keepTax = false )
{ {
if( ! $this->storedCartWithIdentifierExists($identifier)) { if( ! $this->storedCartWithIdentifierExists($identifier))
return false; return false;
}
$stored = $this->getConnection()->table($this->getTableName()) $stored = $this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->first(); ->where('identifier', $identifier)->first();
$storedContent = unserialize($stored->content); $storedContent = unserialize($stored->content);
foreach ($storedContent as $cartItem) { foreach ($storedContent as $cartItem)
$this->addCartItem($cartItem, $keepDiscount, $keepTax); $this->addCartItem($cartItem, $keepDiscount, $keepTax);
}
return true; return true;
} }
@@ -630,19 +627,17 @@ class Cart
*/ */
public function __get($attribute) public function __get($attribute)
{ {
if($attribute === 'total') { switch($attribute)
return $this->total(); {
case 'total':
return $this->total();
case 'tax':
return $this->tax();
case 'subtotal':
return $this->subtotal();
default:
return null;
} }
if($attribute === 'tax') {
return $this->tax();
}
if($attribute === 'subtotal') {
return $this->subtotal();
}
return null;
} }
/** /**
@@ -652,11 +647,9 @@ class Cart
*/ */
protected function getContent() protected function getContent()
{ {
$content = $this->session->has($this->instance) if ($this->session->has($this->instance))
? $this->session->get($this->instance) return $this->session->get($this->instance);
: new Collection; return new Collection;
return $content;
} }
/** /**
@@ -694,8 +687,8 @@ class Cart
*/ */
private function isMulti($item) private function isMulti($item)
{ {
if ( ! is_array($item)) return false; if ( ! is_array($item))
return false;
return is_array(head($item)) || head($item) instanceof Buyable; return is_array(head($item)) || head($item) instanceof Buyable;
} }
@@ -715,9 +708,7 @@ class Cart
*/ */
private function getConnection() private function getConnection()
{ {
$connectionName = $this->getConnectionName(); return app(DatabaseManager::class)->connection( $this->getConnectionName() );
return app(DatabaseManager::class)->connection($connectionName);
} }
/** /**
@@ -738,7 +729,6 @@ class Cart
private function getConnectionName() private function getConnectionName()
{ {
$connection = config('cart.database.connection'); $connection = config('cart.database.connection');
return is_null($connection) ? config('database.default') : $connection; return is_null($connection) ? config('database.default') : $connection;
} }
@@ -753,15 +743,14 @@ class Cart
*/ */
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator) private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{ {
if(is_null($decimals)){ if(is_null($decimals))
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals'); $decimals = config('cart.format.decimals', 2);
}
if(is_null($decimalPoint)){ if(is_null($decimalPoint))
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point'); $decimalPoint = config('cart.format.decimal_point', '.');
}
if(is_null($thousandSeperator)){ if(is_null($thousandSeperator))
$thousandSeperator = is_null(config('cart.format.thousand_separator')) ? ',' : config('cart.format.thousand_separator'); $thousandSeperator = config('cart.format.thousand_separator', ',');
}
return number_format($value, $decimals, $decimalPoint, $thousandSeperator); return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
} }

View File

@@ -335,47 +335,43 @@ class CartItem implements Arrayable, Jsonable
return $this->{$attribute}; return $this->{$attribute};
} }
if($attribute === 'discount') { switch($attribute)
return $this->price * ($this->discountRate / 100); {
} case 'discount':
return $this->price * ($this->discountRate / 100);
if($attribute === 'priceTarget') { case 'priceTarget':
return $this->price - $this->discount; return $this->price - $this->discount;
}
if($attribute === 'subtotal') { case 'subtotal':
return $this->qty * $this->priceTarget; return $this->priceTarget * $this->qty;
}
if($attribute === 'tax') { case 'tax':
return $this->priceTarget * ($this->taxRate / 100); return $this->priceTarget * ($this->taxRate / 100);
}
if($attribute === 'priceTax') { case 'priceTax':
return $this->priceTarget + $this->tax; return $this->priceTarget + $this->tax;
}
if($attribute === 'total') { case 'total':
return $this->qty * $this->priceTax; return $this->priceTax * $this->qty;
}
if($attribute === 'taxTotal') { case 'taxTotal':
return $this->tax * $this->qty; return $this->tax * $this->qty;
}
if($attribute === 'discountTotal') { case 'discountTotal':
return $this->discount * $this->qty; return $this->discount * $this->qty;
}
if($attribute === 'weightTotal') { case 'weightTotal':
return $this->qty * $this->weight; return $this->weight * $this->qty;
}
if($attribute === 'model' && isset($this->associatedModel)) { case 'model':
return with(new $this->associatedModel)->find($this->id); if (isset($this->associatedModel))
} return with(new $this->associatedModel)->find($this->id);
return null;
return null; default:
return null;
}
} }
/** /**
@@ -474,17 +470,14 @@ class CartItem implements Arrayable, Jsonable
*/ */
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator) private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{ {
if (is_null($decimals)){ if (is_null($decimals))
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals'); $decimals = config('cart.format.decimals', 2);
}
if (is_null($decimalPoint)){ if (is_null($decimalPoint))
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point'); $decimalPoint = config('cart.format.decimal_point', '.');
}
if (is_null($thousandSeperator)){ if (is_null($thousandSeperator))
$thousandSeperator = is_null(config('cart.format.thousand_separator')) ? ',' : config('cart.format.thousand_separator'); $thousandSeperator = config('cart.format.thousand_separator', ',');
}
return number_format($value, $decimals, $decimalPoint, $thousandSeperator); return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
} }