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
5 changes: 5 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function getConfig()
return include __DIR__ . '/config/module.config.php';
}

public function getServiceConfig()
{
return include __DIR__ . '/config/service.config.php';
}

public function getAutoloaderConfig()
{
return array(
Expand Down
2 changes: 1 addition & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
),
'controller_plugins' => array (
'factories' => array (
'ShoppingCart' => 'ShoppingCart\Factory\ShoppingCartFactory'
'ShoppingCart' => 'ShoppingCart\Factory\ShoppingCartPluginFactory',
)
)
);
6 changes: 6 additions & 0 deletions config/service.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
return array(
'factories' => array(
'ShoppingCartService' => 'ShoppingCart\Factory\ShoppingCartServiceFactory',
)
);
96 changes: 96 additions & 0 deletions src/ShoppingCart/Controller/Plugin/ShoppingCartPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* ShoppingCart Plugin
* Provides basic functionality of Shopping Cart.
*
* @package ShoppingCart
* @subpackage Plugin
* @author Aleksander Cyrkulewski
*/
namespace ShoppingCart\Controller\Plugin;

use ShoppingCart\Service\ShoppingCartService;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class ShoppingCartPlugin extends AbstractPlugin
{
private $shoppingCartService;

public function __construct (ShoppingCartService $shoppingCartService) {
$this->setShoppingCartService($shoppingCartService);
}

/**
* Add a product(s) to cart
*
* @param array $items
* @return bool
*/
public function insert (array $items) {
return $this->getShoppingCartService()->insert($items);
}

/**
* Delete all items from the cart
*
* @return bool
*/
public function destroy () {
return $this->getShoppingCartService()->destroy();
}

/**
* Remove one item from shopping cart
*
* @param string $token
* @return bool
*/
public function remove ($token) {
return $this->getShoppingCartService()->remove($token);
}

/**
* Counts the total number of items in the cart
*
* @return int
*/
public function total_items () {
return $this->getShoppingCartService()->total_items();
}

/**
* Counts the total sum of the cart
*
* @param int $round
* @param bool $with_vat
* @return number
*/
public function total_sum ($round = 2, $with_vat = false) {
return $this->getShoppingCartService()->total_sum($round, $with_vat);
}

/**
* Return cart content
*
* @return array
*/
public function cart () {
return $this->getShoppingCartService()->cart();
}

/**
* @param ShoppingCartService $shoppingCartService
*/
public function setShoppingCartService($shoppingCartService)
{
$this->shoppingCartService = $shoppingCartService;
}

/**
* @return ShoppingCartService
*/
public function getShoppingCartService()
{
return $this->shoppingCartService;
}
}
29 changes: 29 additions & 0 deletions src/ShoppingCart/Factory/ShoppingCartPluginFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* ShoppingCart Factory
* Initializate Shopping Cart
*
* @package ShoppingCart
* @subpackage Factory
* @author Aleksander Cyrkulewski
*/
namespace ShoppingCart\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Session\Container;
use ShoppingCart\Controller\Plugin\ShoppingCartPlugin;
use ShoppingCart\Hydrator\ShoppingCartHydrator;
use ShoppingCart\Entity\ShoppingCartEntity;

class ShoppingCartPluginFactory implements FactoryInterface
{

public function createService(ServiceLocatorInterface $servicelocator)
{
$shoppingCartService = $servicelocator->getServiceLocator()->get('ShoppingCartService');

$cart = new ShoppingCartPlugin($shoppingCartService);
return $cart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Session\Container;
use ShoppingCart\Controller\Plugin\ShoppingCart;
use ShoppingCart\Service\ShoppingCartService;
use ShoppingCart\Hydrator\ShoppingCartHydrator;
use ShoppingCart\Entity\ShoppingCartEntity;

class ShoppingCartFactory implements FactoryInterface
class ShoppingCartServiceFactory implements FactoryInterface
{

public function createService(ServiceLocatorInterface $servicelocator)
public function createService(ServiceLocatorInterface $serviceLocator)
{
$allServices = $servicelocator->getServiceLocator();
$config = $allServices->get('ServiceManager')->get('Configuration');
$config = $serviceLocator->get('ServiceManager')->get('Configuration');

if (! isset($config['shopping_cart'])) {
throw new \Exception('Configuration ShoppingCart not set.');
}

$cart = new ShoppingCart();
$cart = new ShoppingCartService();
$cart->setHydrator(new ShoppingCartHydrator());
$cart->setEntityPrototype(new ShoppingCartEntity());
$cart->setSession(new Container($config['shopping_cart']['session_name']));
Expand Down
117 changes: 56 additions & 61 deletions ...ngCart/Controller/Plugin/ShoppingCart.php → ...ppingCart/Service/ShoppingCartService.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
<?php
/**
* ShoppingCart Plugin
* Provides basic functionality of Shopping Cart.
*
* @package ShoppingCart
* @subpackage Plugin
* @author Aleksander Cyrkulewski
*/
namespace ShoppingCart\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class ShoppingCart extends AbstractPlugin
{
namespace ShoppingCart\Service;

use Zend\Form\Annotation\Hydrator;

class ShoppingCartService
{
/**
* @var Zend\Session\Container
* @var \Zend\Session\Container
*/
private $session;

Expand All @@ -35,56 +26,21 @@ class ShoppingCart extends AbstractPlugin
private $config;

/**
*
* @param Zend\Session\Container $session
*/
public function setSession($session)
{
$this->session = $session;
}

/**
*
* @param $entityPrototype
*/
public function setEntityPrototype($entityPrototype)
{
$this->entityPrototype = $entityPrototype;
}

/**
*
* @param Hydrator $hydrator
*/
public function setHydrator($hydrator)
{
$this->hydrator = $hydrator;
}

/**
* @param array $config
*/
public function setConfig($config)
{
$this->config = $config;
}

/**
* Add a product(s) to cart
*
* @param array $items
* @return true
* Add product(s) to cart
* @param array $items
* @return bool
* @throws \Exception
*/
public function insert(array $items)
{
if (! is_array($items) or empty($items)) {
throw new \Exception('Only correct values could be saved in Shopping cart.');
}

if (! is_array($this->session['cart'])) {
$this->session['cart'] = array();
}

if ($this->isMultidimention($items)) {
foreach ($items as $item) {
$this->session['cart'][$this->generateToken($item)] = $this->hydrator->hydrate($item, new $this->entityPrototype());
Expand All @@ -98,33 +54,37 @@ public function insert(array $items)
/**
* Generate unique token ID for each item in cart
*
* @param array $item
* @param array $item
* @return string
* @throws \Exception
*/
private function generateToken(array $item)
{
if (! is_array($item) or empty($item)) {
throw new \Exception('The value must be an array.');
}

return sha1($item['id'] . $item['qty'] . time());
}

/**
* Decide if given array is multidimentional array or not
*
* @param array $arr
* @param array $arr
* @return boolean
*/
private function isMultidimention($arr)
{
if (! is_array($arr)) {
return false;
}

foreach ($arr as $elm) {
if (! is_array($elm)) {
return false;
}
}

return true;
}

Expand All @@ -142,7 +102,7 @@ public function destroy()
/**
* Remove one item from shopping cart
*
* @param string $token
* @param string $token
* @return true
*/
public function remove($token)
Expand Down Expand Up @@ -171,8 +131,8 @@ public function total_items()
/**
* Counts the total sum of the cart
*
* @param int $round
* @param bool $with_vat
* @param int $round
* @param bool $with_vat
* @return number
*/
public function total_sum($round = 2, $with_vat = false)
Expand Down Expand Up @@ -204,4 +164,39 @@ public function cart()
}
return $items;
}

/**
*
* @param \Zend\Session\Container $session
*/
public function setSession($session)
{
$this->session = $session;
}

/**
*
* @param $entityPrototype
*/
public function setEntityPrototype($entityPrototype)
{
$this->entityPrototype = $entityPrototype;
}

/**
*
* @param Hydrator $hydrator
*/
public function setHydrator($hydrator)
{
$this->hydrator = $hydrator;
}

/**
* @param array $config
*/
public function setConfig($config)
{
$this->config = $config;
}
}