Completed
Push — develop ( cc8c17...8f8d8e )
by
unknown
07:20
created

CreatePaginator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
c 2
b 0
f 1
lcom 0
cbo 4
dl 0
loc 58
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D __invoke() 0 39 10
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license    MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
10
/**  */
11
namespace Core\Controller\Plugin;
12
13
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
14
use Zend\Stdlib\ArrayUtils;
15
16
/**
17
 * Creates a paginator from the paginator service.
18
 *
19
 * Passing in GET (or POST) request parameters as creation options to the paginator manager.
20
 *
21
 * @author Mathias Gelhausen <[email protected]>
22
 */
23
class CreatePaginator extends AbstractPlugin
24
{
25
    /**
26
     * Creates a paginator from the paginator service.
27
     *
28
     * Uses query parameters from the request merged with $defaultParams as
29
     * creation options while retrieving the service.
30
     * Please note that a query parameter with the same name as a default parameter
31
     * overrides the default parameter.
32
     *
33
     *
34
     * @param string $paginatorName
35
     * @param array  $defaultParams
36
     * @param bool   $usePostParams if true, the POST parameters from the request are used.
37
     *
38
     * @return \Zend\Paginator\Paginator
39
     * @throws \InvalidArgumentException
40
     */
41
    public function __invoke($paginatorName, $defaultParams = array(), $usePostParams = false)
42
    {
43
        if (is_bool($defaultParams)) {
44
            $usePostParams = $defaultParams;
45
            $defaultParams = array();
46
        }
47
48
        if (!is_array($defaultParams) && !$defaultParams instanceof \Traversable) {
49
            throw new \InvalidArgumentException('$defaultParams must be an array or implement \Traversable');
50
        }
51
52
        /* @var $controller \Zend\Mvc\Controller\AbstractController
53
         * @var $paginators \Core\Paginator\PaginatorService
54
         * @var $request    \Zend\Http\Request
55
         */
56
        $controller = $this->getController();
57
        $services   = $controller->getServiceLocator();
58
        $paginators = $services->get('Core/PaginatorService');
59
        $request    = $controller->getRequest();
60
        $params     = $usePostParams
61
            ? $request->getPost()->toArray()
62
            : $request->getQuery()->toArray();
63
64
        // We allow \Traversable so we cannot simply merge.
65
        foreach ($defaultParams as $key => $val) {
66
            if (!isset($params[$key])) {
67
                $params[$key] = $val;
68
            }
69
        }
70
71
        /* @var $paginator \Zend\Paginator\Paginator */
72
        $paginator = $paginators->get($paginatorName, $params);
73
        $paginator->setCurrentPageNumber(isset($params['page']) ? $params['page'] : 1)
74
                  ->setItemCountPerPage(isset($params['count']) ? $params['count'] : 10)
75
                  ->setPageRange(isset($params['range']) ? $params['range'] : 5);
76
77
        return $paginator;
78
79
    }
80
}
81