CreateSorting::getSorting()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
ccs 5
cts 5
cp 1
cc 3
nc 4
nop 3
crap 3
1
<?php
2
3
namespace mQueue\Controller\ActionHelper;
4
5
use Zend_Controller_Action_Helper_Abstract;
6
7
class CreateSorting extends Zend_Controller_Action_Helper_Abstract
8
{
9
    /**
10
     * Initialize the view for sorting based on $sortParameterName and store/retrieve values from session
11
     * It will look for parameter "{name}Key" and "{name}Order" in request.
12
     * It will add variables to the view:
13
     *    - sortParameterName
14
     *  - sortSelectedKey
15
     *  - sortSelectedOrder
16
     *
17
     * @param string $sortParameterName used to look for parameters in request and as key in $_SESSION
18
     * @param array $allowedKeys
19
     *
20
     * @return string similar to 'name DESC'
21
     */
22 1
    public function createSorting($sortParameterName, array $allowedKeys)
23
    {
24 1
        $sortOrderParameterName = $sortParameterName . 'Order';
25 1
        $key = $this->getRequest()->getParam($sortParameterName);
26 1
        $order = $this->getRequest()->getParam($sortOrderParameterName);
27
28 1
        $view = $this->getActionController()->view;
29 1
        $view->sortParameterName = $sortParameterName;
30 1
        $view->sortSelectedKey = $key;
31 1
        $view->sortSelectedOrder = $order;
32
33 1
        if (is_array($view->permanentParams)) {
0 ignored issues
show
Bug introduced by
Accessing permanentParams on the interface Zend_View_Interface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
34 1
            $view->permanentParams[$sortParameterName] = $key;
35 1
            $view->permanentParams[$sortOrderParameterName] = $order;
36
        }
37
38 1
        return self::getSorting($key, $order, $allowedKeys);
39
    }
40
41
    /**
42
     * Returns a valid SQL sorting snippet
43
     *
44
     * @param string $key
45
     * @param string $order
46
     * @param array $allowedKeys
47
     *
48
     * @return string similar to 'name DESC'
49
     */
50 1
    private static function getSorting($key, $order, array $allowedKeys)
51
    {
52 1
        if (!in_array($key, $allowedKeys)) {
53 1
            $key = reset($allowedKeys);
54
        }
55
56 1
        $order = strcasecmp($order, 'desc') == 0 ? 'DESC' : 'ASC';
57
58 1
        return $key . ' ' . $order;
59
    }
60
61
    /**
62
     * Strategy pattern: call helper as broker method
63
     *
64
     * @param string $sortParameterName used to look for parameters in request and as key in $_SESSION
65
     * @param array $allowedKeys
66
     *
67
     * @return string similar to 'name DESC'
68
     */
69 1
    public function direct($sortParameterName, array $allowedKeys)
70
    {
71 1
        return $this->createSorting($sortParameterName, $allowedKeys);
72
    }
73
}
74