Skip to content

Instantly share code, notes, and snippets.

@Xerkus
Last active November 2, 2017 12:29
Show Gist options
  • Save Xerkus/1f4fad126e4c2fff57675b8f9185b5f0 to your computer and use it in GitHub Desktop.
Save Xerkus/1f4fad126e4c2fff57675b8f9185b5f0 to your computer and use it in GitHub Desktop.
<?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;
}
}
<?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
}
@Xerkus
Copy link
Author

Xerkus commented Nov 2, 2017

Line
Responsibility to lookup dependencies is moved out. Controller object is guaranteed to have all required dependencies.

All dependencies are recorded explicitly. Reduced code complexity leads to improved readability and maintainability.

@Xerkus
Copy link
Author

Xerkus commented Nov 2, 2017

Line
No failure point here anymore, code is simpler and as a result more reliable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment