ProductController::imagesAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SpeckCatalog\Controller;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\View\Model\ViewModel;
7
8
class ProductController extends AbstractActionController
9
{
10
    protected $services = array(
11
        'product'       => 'speckcatalog_product_service',
12
        'product_uom'   => 'speckcatalog_product_uom_service',
13
        'builder'       => 'speckcatalog_builder_product_service',
14
        'configure_buy' => 'speckcatalog_configure_buy_service',
15
        'cart'          => 'catalog_cart_service',
16
        'renderer'      => 'zendviewrendererphprenderer',
17
        'option'        => 'speckcatalog_option_service',
18
        'product_image' => 'speckcatalog_product_image_service',
19
    );
20
21
    public function getService($name)
22
    {
23
        if (!array_key_exists($name, $this->services)) {
24
            throw new \Exception('invalid service name');
25
        }
26
        if (is_string($this->services[$name])) {
27
            $this->services[$name] = $this->getServiceLocator()->get($this->services[$name]);
28
        }
29
        return $this->services[$name];
30
    }
31
32
    public function indexAction()
33
    {
34
        $cartItemId     = $this->params('cartItemId');
35
        $cartService    = $this->getService('cart');
36
        $product        = $this->getService('product')
37
            ->setEnabledOnly(true)
38
            ->getFullProduct($this->params('id'));
39
        if (!$product) {
40
            throw new \Exception('no product for that id');
41
        }
42
43
        $this->layout()->crumbs = $this->getService('product')->getCrumbs($product);
44
45
        $vars = array(
46
            'product'     => $product,
47
            'editingCart' => ($cartItemId ? true : false),
48
            'cartItem'    => ($cartItemId ? $cartService->findItemById($cartItemId) : false),
49
        );
50
51
        return new ViewModel($vars);
52
    }
53
54
    public function imagesAction()
55
    {
56
        $productId = $this->params('id');
57
        $images    = $this->getService('product_image')->getImages('product', $productId);
58
59
        return new ViewModel(array('images' => $images));
60
    }
61
62
    public function getViewHelper($helperName)
63
    {
64
        return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName);
65
    }
66
67
    public function uomsPartialAction()
68
    {
69
        $post = $this->params()->fromPost();
70
        $pid  = $post['product_id'];
71
        $builderPid = isset($post['builder_product_id']) ? $post['builder_product_id'] : null;
72
73
        $helper  = $this->getViewHelper('speckCatalogUomsToCart');
74
        $content = $helper->__invoke($pid, $builderPid);
75
76
        return $this->getResponse()->setContent($content);
77
    }
78
79
    public function optionsPartialAction()
80
    {
81
        $postParams = $this->params()->fromPost();
82
        $productId  = $postParams['product_id'];
83
84
        $options = $this->getService('option')->getByProductId($productId, true, true);
85
86
        $renderer = $this->getService('renderer');
87
        foreach ($options as $option) {
88
            $vars     = array('option' => $option);
89
            $html    .= $renderer->render('/catalog/product/option', $vars);
0 ignored issues
show
Bug introduced by
The variable $html does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
90
        }
91
92
        return $html;
93
    }
94
}
95