├── .gitignore ├── src ├── exception │ ├── UnsupportedStorage.php │ └── ClassNotFoundException.php ├── storage │ ├── StorageInterface.php │ ├── DatabaseStorage.php │ └── SessionStorage.php ├── event │ └── CartActionEvent.php ├── ProductInterface.php ├── model │ ├── CartItemModel.php │ └── CartModel.php ├── ProductTest.php └── Cart.php ├── composer.json ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | /vendor 4 | -------------------------------------------------------------------------------- /src/exception/UnsupportedStorage.php: -------------------------------------------------------------------------------- 1 | getIdentifier(); 20 | } 21 | 22 | public function getPrice() 23 | { 24 | return 10.5; 25 | } 26 | 27 | public function getOriginalPrice() 28 | { 29 | return 11; 30 | } 31 | 32 | public function getQuantity() 33 | { 34 | return rand(1,6); 35 | } 36 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ark Cart - Yii2 Framework (Not yet in production) 2 | ----------------------------------------------- 3 | 4 | [![Build Status](https://scrutinizer-ci.com/g/fg/ark-yii2-cart/badges/build.png?b=master)](https://scrutinizer-ci.com/g/fg/ark-yii2-cart/build-status/master) 5 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fg/ark-yii2-cart/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fg/ark-yii2-cart/?branch=master) 6 | [![Code Climate](https://codeclimate.com/github/fg/ark-yii2-cart/badges/gpa.svg)](https://codeclimate.com/github/fg/ark-yii2-cart) 7 | [![License](https://poser.pugx.org/ark-yii/yii2-cart/license)](https://packagist.org/packages/ark-yii/yii2-cart) 8 | -------------------------------------------------------------------------------- /src/storage/SessionStorage.php: -------------------------------------------------------------------------------- 1 | session->set(self::HASH, $cart); 14 | } 15 | 16 | public function get() 17 | { 18 | if (!Yii::$app->session->has(self::HASH)) { 19 | Yii::$app->session->set(self::HASH, new CartModel()); 20 | } 21 | 22 | return Yii::$app->session->get(self::HASH); 23 | } 24 | 25 | public function destroy() 26 | { 27 | return Yii::$app->session->remove(self::HASH); 28 | } 29 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Fatih GÜRSOY 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /src/Cart.php: -------------------------------------------------------------------------------- 1 | initializeCart(); 38 | } 39 | 40 | /** 41 | * @throws ClassNotFoundException 42 | */ 43 | private function initializeCart() 44 | { 45 | if (!class_exists($this->storageClass)) { 46 | throw new ClassNotFoundException( 47 | sprintf('Class "%s" are not found. Please checkout the correct namespace or autoload?', $this->storageClass) 48 | ); 49 | } 50 | $this->storage = new $this->storageClass(); 51 | $this->cart = $this->storage->get(); 52 | } 53 | 54 | public function addItem(ProductInterface $product, $quantity) 55 | { 56 | $this->cart->addItem($product, $quantity); 57 | } 58 | 59 | public function deleteItem(ProductInterface $product, $quantity) 60 | { 61 | $this->cart->updateItem($product, $quantity); 62 | } 63 | 64 | public function removeItem(ProductInterface $product) 65 | { 66 | $this->cart->deleteItem($product); 67 | } 68 | 69 | /** 70 | * @return boolean 71 | */ 72 | public function destroy() { 73 | return $this->storage->destroy(); 74 | } 75 | 76 | /** 77 | * @return CartModel 78 | */ 79 | public function getCart() 80 | { 81 | return $this->cart; 82 | } 83 | 84 | /** 85 | * @return StorageInterface 86 | */ 87 | public function getStorage() 88 | { 89 | return $this->storage; 90 | } 91 | } -------------------------------------------------------------------------------- /src/model/CartModel.php: -------------------------------------------------------------------------------- 1 | expires_at = (new DateTime('+4 hours'))->format('Y-m-d H:i:s'); 73 | $this->created_at = (new DateTime())->format('Y-m-d H:i:s'); 74 | 75 | $this->on(self::EVENT_CART_UPDATED, [$this, 'afterCartUpdated']); 76 | } 77 | 78 | /** 79 | * @param ProductInterface $product 80 | * @param integer $quantity 81 | */ 82 | public function addItem(ProductInterface $product, $quantity) 83 | { 84 | $itemHash = $this->generateHash($product); 85 | 86 | $cartItemInstance = array_key_exists($itemHash, $this->items) ? $this->items[$itemHash] : new CartItemModel(); 87 | $cartItemInstance->product_id = $product->getId(); 88 | $cartItemInstance->name = $product->getName(); 89 | $cartItemInstance->quantity = $cartItemInstance->quantity + $quantity; 90 | $cartItemInstance->total_price = $product->getPrice(); 91 | $cartItemInstance->total_original_price = $product->getOriginalPrice(); 92 | 93 | $this->items[$itemHash] = $cartItemInstance; 94 | 95 | $this->trigger(self::EVENT_CART_UPDATED, new CartActionEvent(['product' => $product, 'cart' => $this])); 96 | } 97 | 98 | /** 99 | * @param ProductInterface $product 100 | * @param $quantity 101 | */ 102 | public function updateItem(ProductInterface $product, $quantity) 103 | { 104 | $itemHash = $this->generateHash($product); 105 | 106 | if (array_key_exists($itemHash, $this->items)) { 107 | $this->items[$itemHash]->quantity = $quantity; 108 | } 109 | 110 | $this->trigger(self::EVENT_CART_UPDATED, new CartActionEvent(['product' => $product, 'cart' => $this])); 111 | } 112 | 113 | /** 114 | * @param ProductInterface $product 115 | */ 116 | public function deleteItem(ProductInterface $product) 117 | { 118 | $itemHash = $this->generateHash($product); 119 | 120 | if (array_key_exists($itemHash, $this->items)) { 121 | unset($this->items[$itemHash]); 122 | } 123 | 124 | $this->trigger(self::EVENT_CART_UPDATED, new CartActionEvent(['product' => $product, 'cart' => $this])); 125 | } 126 | 127 | /** 128 | * @param ProductInterface $product 129 | * @return string 130 | */ 131 | protected function generateHash($product) { 132 | return md5($product->getIdentifier()); 133 | } 134 | 135 | /** 136 | * @param CartActionEvent $event 137 | */ 138 | protected function afterCartUpdated($event) 139 | { 140 | $this->total_item = count($this->items); 141 | $this->total_quantity = 0; 142 | $this->total_price = (float) 0; 143 | $this->total_original_price = (float) 0; 144 | 145 | /** 146 | * @var string $key 147 | * @var CartItemModel $item 148 | */ 149 | foreach ($this->items as $key => $item) { 150 | $this->total_quantity = $this->total_quantity + $item->quantity; 151 | $this->total_price = $this->total_price + ($item->total_price * $item->quantity); 152 | $this->total_original_price = $this->total_original_price + ($item->total_original_price * $item->quantity); 153 | } 154 | 155 | $this->updated_at = (new DateTime())->format('Y-m-d H:i:s'); 156 | } 157 | } --------------------------------------------------------------------------------