Last active
November 2, 2017 12:29
-
-
Save Xerkus/1f4fad126e4c2fff57675b8f9185b5f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SpeckCatalog\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\ViewModel; | |
class ProductController extends AbstractActionController | |
{ | |
protected $services = array( | |
'product' => 'speckcatalog_product_service', | |
'product_uom' => 'speckcatalog_product_uom_service', | |
'builder' => 'speckcatalog_builder_product_service', | |
'configure_buy' => 'speckcatalog_configure_buy_service', | |
'cart' => 'catalog_cart_service', | |
'renderer' => 'zendviewrendererphprenderer', | |
'option' => 'speckcatalog_option_service', | |
'product_image' => 'speckcatalog_product_image_service', | |
); | |
public function getService($name) | |
{ | |
if (!array_key_exists($name, $this->services)) { | |
throw new \Exception('invalid service name'); | |
} | |
if (is_string($this->services[$name])) { | |
$this->services[$name] = $this->getServiceLocator()->get($this->services[$name]); | |
} | |
return $this->services[$name]; | |
} | |
public function indexAction() | |
{ | |
$cartItemId = $this->params('cartItemId'); | |
$cartService = $this->getService('cart'); | |
$product = $this->getService('product') | |
->setEnabledOnly(true) | |
->getFullProduct($this->params('id')); | |
if (!$product) { | |
throw new \Exception('no product for that id'); | |
} | |
$this->layout()->crumbs = $this->getService('product')->getCrumbs($product); | |
$vars = array( | |
'product' => $product, | |
'editingCart' => ($cartItemId ? true : false), | |
'cartItem' => ($cartItemId ? $cartService->findItemById($cartItemId) : false), | |
); | |
return new ViewModel($vars); | |
} | |
public function imagesAction() | |
{ | |
$productId = $this->params('id'); | |
$images = $this->getService('product_image')->getImages('product', $productId); | |
return new ViewModel(array('images' => $images)); | |
} | |
public function getViewHelper($helperName) | |
{ | |
return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName); | |
} | |
public function uomsPartialAction() | |
{ | |
$post = $this->params()->fromPost(); | |
$pid = $post['product_id']; | |
$builderPid = isset($post['builder_product_id']) ? $post['builder_product_id'] : null; | |
$helper = $this->getViewHelper('speckCatalogUomsToCart'); | |
$content = $helper->__invoke($pid, $builderPid); | |
return $this->getResponse()->setContent($content); | |
} | |
public function optionsPartialAction() | |
{ | |
$postParams = $this->params()->fromPost(); | |
$productId = $postParams['product_id']; | |
$options = $this->getService('option')->getByProductId($productId, true, true); | |
$renderer = $this->getService('renderer'); | |
foreach ($options as $option) { | |
$vars = array('option' => $option); | |
$html .= $renderer->render('/catalog/product/option', $vars); | |
} | |
return $html; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SpeckCatalog\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\ViewModel; | |
class ProductController extends AbstractActionController | |
{ | |
private $productService; | |
private $cartService; | |
public function __construct(CartService $cartService, ProductService $productService) | |
{ | |
$this->cartService = $cartService; | |
$this->productService = $productService; | |
} | |
public function indexAction() | |
{ | |
$cartItemId = $this->params('cartItemId'); | |
$product = $this->productService | |
->setEnabledOnly(true) | |
->getFullProduct($this->params('id')); | |
if (!$product) { | |
throw new \Exception('no product for that id'); | |
} | |
$this->layout()->crumbs = $this->productService->getCrumbs($product); | |
$vars = array( | |
'product' => $product, | |
'editingCart' => ($cartItemId ? true : false), | |
'cartItem' => ($cartItemId ? $this->cartService->findItemById($cartItemId) : false), | |
); | |
return new ViewModel($vars); | |
} | |
// other actions extracted into separate controllers as they do not share dependencies and responsibilities | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line
No failure point here anymore, code is simpler and as a result more reliable.