CatalogManagerController   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 13
Bugs 1 Features 5
Metric Value
wmc 36
lcom 1
cbo 6
dl 0
loc 280
ccs 0
cts 217
cp 0
rs 8.8
c 13
b 1
f 5

21 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A persist() 0 18 4
A getViewHelper() 0 4 1
A categorySearchChildrenAction() 0 8 1
A newProductAction() 0 6 1
A categoryTreePreviewAction() 0 9 1
A productsAction() 0 18 3
A categoriesAction() 0 6 1
A productAction() 0 9 1
A updateProductAction() 0 12 2
A updateRecordAction() 0 16 2
A updateFormAction() 0 9 1
B findAction() 0 37 4
A foundAction() 0 19 3
A sortAction() 0 19 3
A removeChildAction() 0 18 2
A getService() 0 5 1
A newPartialAction() 0 16 1
A partialView() 0 8 1
A dash() 0 5 1
A camel() 0 5 1
1
<?php
2
3
namespace SpeckCatalog\Controller;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\View\Model\ViewModel;
7
use Zend\Paginator\Paginator;
8
use Zend\Paginator\Adapter\ArrayAdapter as ArrayAdapter;
9
use Zend\Stdlib\Hydrator\ClassMethods as Hydrator;
10
11
class CatalogManagerController extends AbstractActionController
12
{
13
    protected $partialDir = '/speck-catalog/catalog-manager/partial/';
14
15
    public function init()
16
    {
17
        $this->subLayout('layout/catalog-manager');
0 ignored issues
show
Bug introduced by
The method subLayout() does not exist on SpeckCatalog\Controller\CatalogManagerController. Did you maybe mean layout()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
18
    }
19
20
    //find categories/products that match search terms
21
    public function categorySearchChildrenAction()
22
    {
23
        $type = $this->params('type');
24
        $children = $this->getService($type)->getAll();
25
26
        $viewVars = array('children' => $children, 'type' => $type);
27
        return $this->partialView('category-search-children', $viewVars);
28
    }
29
30
    public function newProductAction()
31
    {
32
        $this->init();
33
        $product = $this->getService('product')->getModel();
34
        return new ViewModel(array('product' => $product));
35
    }
36
37
    public function categoryTreePreviewAction()
38
    {
39
        $siteId = $this->params('siteid');
40
        $categoryService = $this->getService('category');
41
        $categories = $categoryService->getCategoriesForTreePreview($siteId);
42
43
        $viewVars = array('categories' => $categories);
44
        return $this->partialView('category-tree', $viewVars);
45
    }
46
47
    public function productsAction()
48
    {
49
        $service = $this->getService('product');
50
51
        $this->init();
52
        $config = array(
53
            'p' => $this->params('p') ?: 1,
54
            'n' => 40,
55
        );
56
        $service->usePaginator($config);
57
        $query = $this->params()->fromQuery('query');
58
        if ($query) {
59
            $products = $service->search($query);
60
        } else {
61
            $products = $service->getAll();
62
        }
63
        return new ViewModel(array('products' => $products, 'query' => $query));
64
    }
65
66
    public function categoriesAction()
67
    {
68
        $this->init();
69
        $sites = $this->getService('sites')->getAll();
70
        return new ViewModel(array('sites' => $sites));
71
    }
72
73
    public function productAction()
74
    {
75
        $this->init();
76
        $productService = $this->getService('product');
77
        $product = $productService->getFullProduct($this->params('id'));
78
79
        $vars = array('product' => $product);
80
        return new ViewModel($vars);
81
    }
82
83
84
    //returns main view variable(product/option/etc)
85
    protected function persist($class, $form)
86
    {
87
        $service = $this->getService($class);
88
89
        if (method_exists($service, 'persist')) {
90
            return $service->persist($form);
91
        }
92
93
        $originalData = $form->getOriginalData();
94
        $data = $form->getData();
95
96
        if (count($originalData) && $service->find($originalData)) {
97
            $service->update($data, $originalData);
98
            return $service->find($data, true, true);
99
        }
100
101
        return $service->insert($data);
102
    }
103
104
    public function updateProductAction()
105
    {
106
        $formData = $this->params()->fromPost();
107
        $form = $this->getService('form')->getForm('product', null, $formData);
108
109
        if ($form->isValid()) {
110
            $product = $this->persist('product', $form);
111
            return $this->getResponse()->setContent($product->getProductId());
112
        }
113
114
        return $this->partialView('product', array('product' => $product));
0 ignored issues
show
Bug introduced by
The variable $product seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
115
    }
116
117
    public function updateRecordAction()
118
    {
119
        $class = $this->params('class');
120
        $formData = $this->params()->fromPost();
121
        $form = $this->getService('form')->getForm($class, null, $formData);
122
123
        if ($form->isValid()) {
124
            $entity = $this->persist($class, $form);
125
        } else {
126
            $entity = $this->getService($class)->getEntity($formData);
127
        }
128
129
        $partial = $this->dash($class);
130
        $viewVars = array(lcfirst($this->camel($class)) => $entity);
131
        return $this->partialView($partial, $viewVars);
132
    }
133
134
    public function updateFormAction()
135
    {
136
        $data   = $this->params()->fromPost();
137
        $form   = $this->getService('form')->getForm($this->params('class'), null, $data);
138
        $helper = $this->getViewHelper('speckCatalogForm');
139
140
        $html   = $helper->renderFormMessages($form);
141
        return $this->getResponse()->setContent($html);
142
    }
143
144
    public function getViewHelper($helperName)
145
    {
146
        return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName);
147
    }
148
149
    public function findAction()
150
    {
151
        $post = $this->params()->fromPost();
152
153
        if (!isset($post['query'])) {
154
            $search = $this->partialDir . 'search/' . $this->dash($post['child_name']);
155
            $view   = new ViewModel(array(
156
                'fields'        => $post,
157
                'searchPartial' => $search
158
            ));
159
            return $view->setTemplate($this->partialDir . 'search/index')->setTerminal(true);
160
        }
161
162
        $response = array(
163
            'html' => '',
164
        );
165
        $result = $this->getService($post['parent_name'])->search($post);
166
        if (count($result) > 0) {
167
            $partial = $this->getViewHelper('partial');
168
            $rowPartial = $this->partialDir . 'search/row/' . $this->dash($post['child_name']);
169
            foreach ($result as $row) {
170
                $response['html'] .= $partial($rowPartial, array('model' => $row, 'params' => $post));
171
            }
172
        } else {
173
            $response['html'] = 'no result';
174
        }
175
        $json_resp = json_encode($response);
176
        return $this->getResponse()->setContent($json_resp);
177
178
179
180
181
182
183
184
        return $view;
0 ignored issues
show
Unused Code introduced by
return $view; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
185
    }
186
187
    public function foundAction()
188
    {
189
        $postParams = $this->params()->fromPost();
190
191
        $objects = array();
192
193
        if ($postParams['child_name'] === 'builder_product') {
194
            $parentProductId = $postParams['parent']['product_id'];
195
            $productIds = array_keys($postParams['check']);
196
            foreach ($productIds as $productId) {
197
                $objects[] = $this->getService('builder_product')->newBuilderForProduct($productId, $parentProductId);
198
            }
199
        }
200
201
        $helper   = $this->getViewHelper('speckCatalogRenderChildren');
202
        $content  = $helper->__invoke($postParams['child_name'], $objects);
203
        $response = $this->getResponse()->setContent($content);
204
        return $response;
205
    }
206
207
    public function sortAction()
208
    {
209
        $postParams = $this->params()->fromPost();
210
        $childName  = $this->params('type');
211
        $parentName = $this->params('parent');
212
        $parent     = $postParams['parent_key'];
213
214
        $order = explode(',', $postParams['order']);
215
        foreach ($order as $i => $val) {
216
            if (!trim($val)) {
217
                unset($order[$i]);
218
            }
219
        }
220
        $parentService = $this->getService($parentName);
221
        $sortChildren = 'sort' . $this->camel($childName) . 's';
222
        $result = $parentService->$sortChildren($parent, $order);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
223
224
        return $this->getResponse();
225
    }
226
227
    public function removeChildAction()
228
    {
229
        $postParams = $this->params()->fromPost();
230
        $parentName = $postParams['parent_name'];
231
        $childName  = $postParams['child_name'];
232
        $parent     = $postParams['parent'];
233
        $child      = $postParams['child'];
234
235
        $parentService = $this->getService($parentName);
236
237
        $removeChildMethod = 'remove' . $this->camel($childName);
238
        $result = $parentService->$removeChildMethod($parent, $child);
239
240
        if (true === $result) {
241
            return $this->getResponse()->setContent('true');
242
        }
243
        return $this->getResponse()->setContent('false');
244
    }
245
246
    public function getService($name)
247
    {
248
        $serviceName = 'speckcatalog_' . $name . '_service';
249
        return $this->getServiceLocator()->get($serviceName);
250
    }
251
252
    //return the partial for a new record.
253
    public function newPartialAction()
254
    {
255
        $postParams = $this->params()->fromPost();
256
        $parentName = $postParams['parent_name'];
257
        $childName  = $postParams['child_name'];
258
        $parent     = $postParams['parent'];
259
260
        $parent = $this->getService($parentName)->find($parent);
261
        $child  = $this->getService($childName)->getModel();
262
263
        $child->setParent($parent);
264
265
        $partial  = $this->dash($childName);
266
        $viewVars = array(lcfirst($this->camel($childName)) => $child);
267
        return $this->partialView($partial, $viewVars);
268
    }
269
270
    public function partialView($partial, array $viewVars = null)
271
    {
272
        $view = new ViewModel($viewVars);
273
        $view->setTemplate($this->partialDir . $partial);
274
        $view->setTerminal(true);
275
276
        return $view;
277
    }
278
279
    protected function dash($name)
280
    {
281
        $dash = new \Zend\Filter\Word\UnderscoreToDash;
282
        return $dash->__invoke($name);
283
    }
284
285
    protected function camel($name)
286
    {
287
        $camel = new \Zend\Filter\Word\UnderscoreToCamelCase;
288
        return $camel->__invoke($name);
289
    }
290
}
291