diff --git a/readme.md b/readme.md index e9bca01..c1be2c3 100644 --- a/readme.md +++ b/readme.md @@ -183,7 +183,7 @@ $coupon = new \LukePOLO\LaraCart\Coupons\Fixed($coupon->CouponCode, $coupon->Cou 'description' => $coupon->Description ]); -LaraCart::applyCoupon($coupon); +LaraCart::addCoupon($coupon); // To remove LaraCart::removeCoupon($code); diff --git a/scrutinizer.yml b/scrutinizer.yml new file mode 100644 index 0000000..2b3b6b8 --- /dev/null +++ b/scrutinizer.yml @@ -0,0 +1,6 @@ +tools: + external_code_coverage: false +checks: + php: + code_rating: true + duplication: true \ No newline at end of file diff --git a/src/CartItem.php b/src/CartItem.php index 5e2c0c8..6796dbe 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -4,7 +4,6 @@ use LukePOLO\LaraCart\Exceptions\InvalidPrice; use LukePOLO\LaraCart\Exceptions\InvalidQuantity; -use LukePOLO\LaraCart\Exceptions\UnknownItemProperty; /** * Class CartItem @@ -58,7 +57,7 @@ public function __construct($id, $name, $qty, $price, $options = [], $lineItem = */ public function __get($option) { - if ($option == 'price') { + if ($option == LaraCart::PRICE) { return $this->getPrice(); } else { return array_get($this->options, $option); @@ -112,22 +111,16 @@ public function __isset($option) */ public function generateHash($force = false) { - if ($force === true) { - $this->itemHash = null; - } - if ($this->lineItem === false) { $this->itemHash = null; $cartItemArray = (array)$this; - if (empty($cartItemArray['options']) === false) { - ksort($cartItemArray['options']); - } + ksort($cartItemArray['options']); - $this->itemHash = app('generateCartHash', $cartItemArray); - } elseif (empty($this->itemHash) === true) { - $this->itemHash = app('generateRandomCartItemHash'); + $this->itemHash = app(LaraCart::HASH, $cartItemArray); + } elseif ($force || empty($this->itemHash) === true) { + $this->itemHash = app(LaraCart::RANHASH); } return $this->itemHash; } @@ -182,27 +175,14 @@ public function getPrice($tax = false, $format = true) $price = $this->price; foreach ($this->subItems as $subItem) { - - if (isset($subItem->price)) { - $price += $subItem->price; - } - - if (empty($subItem->items) === false) { - foreach ($subItem->items as $item) { - $price += $item->getPrice($tax, false); - } - } + $price += $subItem->getPrice(false); } if ($tax) { $price += $price * $this->tax; } - if ($format) { - return \App::make('laracart')->formatMoney($price, $this->locale, $this->internationalFormat); - } else { - return $price; - } + return \App::make(LaraCart::SERVICE)->formatMoney($price, $this->locale, $this->internationalFormat, $format); } /** @@ -211,30 +191,26 @@ public function getPrice($tax = false, $format = true) * @param $key * @param $value * - * @throws InvalidQuantity | InvalidPrice | UnknownItemProperty + * @throws InvalidQuantity | InvalidPrice * * @return string $itemHash */ public function update($key, $value) { switch ($key) { - case 'qty': + case LaraCart::QTY: if (is_int($value) === false) { throw new InvalidQuantity(); } break; - case 'price': + case LaraCart::PRICE: if (is_numeric($value) === false || preg_match('/\.(\d){3}/', $value)) { throw new InvalidPrice(); } break; } - if (isset($this->$key) === true) { - $this->$key = $value; - } else { - throw new UnknownItemProperty(); - } + $this->$key = $value; return $this->generateHash(); } @@ -255,12 +231,7 @@ public function subTotal($tax = false, $format = true, $withDiscount = true) $total -= $this->getDiscount(false); } - if ($format) { - - return \App::make('laracart')->formatMoney($total, $this->locale, $this->internationalFormat); - } else { - return $total; - } + return \App::make(LaraCart::SERVICE)->formatMoney($total, $this->locale, $this->internationalFormat, $format); } @@ -277,7 +248,7 @@ public function subItemsTotal($tax = false, $format = true) $total = 0; foreach ($this->subItems as $item) { if (isset($item->price)) { - $total += array_get($item->options, 'price'); + $total += array_get($item->options, LaraCart::PRICE); } } @@ -285,11 +256,7 @@ public function subItemsTotal($tax = false, $format = true) $total = $total + ($total * $this->tax); } - if ($format) { - return \App::make('laracart')->formatMoney($total, $this->locale, $this->internationalFormat); - } else { - return $total; - } + return \App::make(LaraCart::SERVICE)->formatMoney($total, $this->locale, $this->internationalFormat, $format); } /** @@ -302,16 +269,12 @@ public function subItemsTotal($tax = false, $format = true) public function getDiscount($format = true) { // TODO - move to main laracart should not be in here - if (\App::make('laracart')->findCoupon($this->code)) { + if (\App::make(LaraCart::SERVICE)->findCoupon($this->code)) { $discount = $this->discount; } else { $discount = 0; } - if ($format) { - return \App::make('laracart')->formatMoney($discount, $this->locale, $this->internationalFormat); - } else { - return $discount; - } + return \App::make(LaraCart::SERVICE)->formatMoney($discount, $this->locale, $this->internationalFormat, $format); } } diff --git a/src/CartSubItem.php b/src/CartSubItem.php index a6a4170..9bb32bc 100644 --- a/src/CartSubItem.php +++ b/src/CartSubItem.php @@ -9,20 +9,22 @@ */ class CartSubItem { + private $itemHash; + public $locale; - public $price; + public $price = 0; + public $items = []; public $options = []; public $internationalFormat; - private $itemHash; /** * @param $options */ public function __construct($options) { - $this->itemHash = app('generateCartHash', $options); - if (isset($options['price']) === true) { - $this->price = floatval($options['price']); + $this->itemHash = app(LaraCart::HASH, $options); + if (isset($options[LaraCart::PRICE]) === true) { + $this->price = floatval($options[LaraCart::PRICE]); } $this->options = $options; } @@ -52,13 +54,19 @@ public function getHash() /** * Gets the formatted price * - * @return string + * @param bool|true $format + * + * @return mixed */ - public function getPrice() + public function getPrice($format = true) { - if(!empty($this->price)) { - return \App::make('laracart')->formatMoney($this->price, $this->locale, $this->internationalFormat); + $price = $this->price; + + foreach ($this->items as $item) { + $price += $item->getPrice(false, false); } + + return \App::make(LaraCart::SERVICE)->formatMoney($price, $this->locale, $this->internationalFormat, $format); } /** diff --git a/src/LaraCart.php b/src/LaraCart.php index 35734e7..e1ccb22 100644 --- a/src/LaraCart.php +++ b/src/LaraCart.php @@ -12,6 +12,12 @@ */ class LaraCart implements LaraCartContract { + CONST SERVICE = 'laracart'; + CONST QTY = 'qty'; + CONST PRICE = 'price'; + CONST HASH = 'generateCartHash'; + CONST RANHASH = 'generateRandomCartItemHash'; + public $cart; /** @@ -73,25 +79,23 @@ public function update() * @param $number * @param $locale * @param $internationalFormat + * @param $format * * @return string */ - public function formatMoney($number, $locale = null, $internationalFormat = null) + public function formatMoney($number, $locale = null, $internationalFormat = null, $format = true) { - if (empty($locale) === true) { - $locale = config('laracart.locale', 'en_US.UTF-8'); - } + if ($format) { - if (empty($internationalFormat) === true) { - $internationalFormat = config('laracart.international_format', false); - } + setlocale(LC_MONETARY, empty($locale) ? $locale : config('laracart.locale', 'en_US.UTF-8')); - setlocale(LC_MONETARY, $locale); + if (empty($internationalFormat) === true) { + $internationalFormat = config('laracart.international_format', false); + } - if ($internationalFormat) { - return money_format('%i', $number); + return money_format($internationalFormat ? '%i' : '%n', $number); } else { - return money_format('%n', $number); + return number_format($number, 2); } } @@ -354,7 +358,7 @@ public function findCoupon($code) * * @param CouponContract $coupon */ - public function applyCoupon(CouponContract $coupon) + public function addCoupon(CouponContract $coupon) { $this->cart->coupons[$coupon->code] = $coupon; @@ -426,11 +430,11 @@ public function removeFee($name) /** * Gets all the fee totals * - * @param bool|true $formatted + * @param bool|true $format * * @return string */ - public function getFeeTotals($formatted = true) + public function feeTotals($format = true) { $feeTotal = 0; @@ -441,7 +445,7 @@ public function getFeeTotals($formatted = true) } } - if ($formatted) { + if ($format) { return $this->formatMoney($feeTotal); } else { return number_format($feeTotal, 2); @@ -451,18 +455,18 @@ public function getFeeTotals($formatted = true) /** * Gets the total amount discounted * - * @param bool|true $formatted + * @param bool|true $format * * @return int|string */ - public function getTotalDiscount($formatted = true) + public function totalDiscount($format = true) { $total = 0; foreach ($this->cart->coupons as $coupon) { $total += $coupon->discount(); } - if ($formatted) { + if ($format) { return $this->formatMoney($total); } else { return $total; @@ -473,15 +477,15 @@ public function getTotalDiscount($formatted = true) /** * Gets the total tax for the cart * - * @param bool|true $formatted + * @param bool|true $format * * @return string */ - public function taxTotal($formatted = true) + public function taxTotal($format = true) { - $totalTax = $this->total(false, false) - $this->subTotal(false, false, false) - $this->getFeeTotals(false); + $totalTax = $this->total(false, false) - $this->subTotal(false, false, false) - $this->feeTotals(false); - if ($formatted) { + if ($format) { return $this->formatMoney($totalTax); } else { return number_format($totalTax, 2); @@ -492,12 +496,12 @@ public function taxTotal($formatted = true) * Gets the subtotal of the cart with or without tax * * @param bool|false $tax - * @param bool|true $formatted + * @param bool|true $format * @param bool|true $withDiscount * * @return string */ - public function subTotal($tax = false, $formatted = true, $withDiscount = true) + public function subTotal($tax = false, $format = true, $withDiscount = true) { $total = 0; if ($this->count() != 0) { @@ -506,7 +510,7 @@ public function subTotal($tax = false, $formatted = true, $withDiscount = true) } } - if ($formatted) { + if ($format) { return $this->formatMoney($total); } else { return number_format($total, 2); @@ -517,15 +521,15 @@ public function subTotal($tax = false, $formatted = true, $withDiscount = true) /** * Gets the total of the cart with or without tax * - * @param bool|true $formatted + * @param bool|true $format * @param bool|true $withDiscount * @return string */ - public function total($formatted = true, $withDiscount = true) + public function total($format = true, $withDiscount = true) { - $total = $this->subTotal(true, false, $withDiscount) + $this->getFeeTotals(false); + $total = $this->subTotal(true, false, $withDiscount) + $this->feeTotals(false); - if ($formatted) { + if ($format) { return $this->formatMoney($total); } else { return number_format($total, 2); diff --git a/src/LaraCartServiceProvider.php b/src/LaraCartServiceProvider.php index 0afa693..0e98124 100644 --- a/src/LaraCartServiceProvider.php +++ b/src/LaraCartServiceProvider.php @@ -39,12 +39,12 @@ public function register() return $app->make(LaraCartContract::class); }); - $this->app->bind('generateCartHash', function($app, $data) + $this->app->bind(LaraCart::HASH, function($app, $data) { return md5(json_encode($data)); }); - $this->app->bind('generateRandomCartItemHash', function() + $this->app->bind(LaraCart::RANHASH, function() { return str_random(40); }); diff --git a/src/exceptions/UnknownItemProperty.php b/src/exceptions/UnknownItemProperty.php deleted file mode 100644 index 3dbe290..0000000 --- a/src/exceptions/UnknownItemProperty.php +++ /dev/null @@ -1,12 +0,0 @@ -assertEquals(0, $this->laracart->count()); } - public function testApplyCoupon( ) + public function testAddCoupon( ) { $fixedCoupon = New LukePOLO\LaraCart\Coupons\Fixed('10OFF', 10); - $this->laracart->applyCoupon($fixedCoupon); + $this->laracart->addCoupon($fixedCoupon); $this->assertEquals($fixedCoupon, $this->laracart->findCoupon('10OFF')); $percentCoupon = New LukePOLO\LaraCart\Coupons\Percentage('10%OFF', '.1'); - $this->laracart->applyCoupon($percentCoupon); + $this->laracart->addCoupon($percentCoupon); $this->assertEquals($percentCoupon, $this->laracart->findCoupon('10%OFF')); $this->assertCount(2, $this->laracart->getCoupons()); @@ -203,7 +203,7 @@ public function testApplyCoupon( ) public function testRemoveCoupon() { $fixedCoupon = New LukePOLO\LaraCart\Coupons\Fixed('10OFF', 10); - $this->laracart->applyCoupon($fixedCoupon); + $this->laracart->addCoupon($fixedCoupon); $this->assertEquals($fixedCoupon, $this->laracart->findCoupon('10OFF')); $this->laracart->removeCoupon('10OFF'); @@ -221,24 +221,24 @@ public function testAddFee() $this->assertCount(0, $this->laracart->getFees()); } - public function testGetFeeTotals() + public function testFeeTotals() { $this->laracart->addFee('test', 5); $this->laracart->addFee('test_2', 20); - $this->assertEquals('$25.00', $this->laracart->getFeeTotals()); - $this->assertEquals(25, $this->laracart->getFeeTotals(false)); + $this->assertEquals('$25.00', $this->laracart->feeTotals()); + $this->assertEquals(25, $this->laracart->feeTotals(false)); } - public function testGetTotalDiscount() + public function testTotalDiscount() { $fixedCoupon = New LukePOLO\LaraCart\Coupons\Fixed('10OFF', 10); - $this->laracart->applyCoupon($fixedCoupon); + $this->laracart->addCoupon($fixedCoupon); $this->assertEquals($fixedCoupon, $this->laracart->findCoupon('10OFF')); - $this->assertEquals('$10.00', $this->laracart->getTotalDiscount()); - $this->assertEquals(10, $this->laracart->getTotalDiscount(false)); + $this->assertEquals('$10.00', $this->laracart->totalDiscount()); + $this->assertEquals(10, $this->laracart->totalDiscount(false)); } public function testTaxTotal()