Completed
Pull Request — develop (#241)
by ANTHONIUS
07:36
created

CreatePaginator::__invoke()   C

Complexity

Conditions 11
Paths 26

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 52
rs 5.9999
cc 11
eloc 28
nc 26
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Core\Listener\Events\CreatePaginatorEvent;
14
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
15
use Zend\Mvc\Controller\PluginManager as ControllerManager;
16
use Zend\Paginator\Paginator;
17
use Zend\ServiceManager\ServiceLocatorInterface;
18
19
/**
20
 * Creates a paginator from the paginator service.
21
 *
22
 * Passing in GET (or POST) request parameters as creation options to the paginator manager.
23
 *
24
 * @author Mathias Gelhausen <[email protected]>
25
 * @author Anthonius Munthi <[email protected]>
26
 */
27
class CreatePaginator extends AbstractPlugin
28
{
29
    const EVENT_CREATE_PAGINATOR = 'core.create_paginator';
30
    /**
31
     * @var ServiceLocatorInterface
32
     */
33
    protected $serviceManager;
34
    
35
    /**
36
     * @param ServiceLocatorInterface $serviceManager
37
     */
38
    public function __construct(ServiceLocatorInterface $serviceManager)
39
    {
40
        $this->serviceManager = $serviceManager;
41
    }
42
    
43
    /**
44
     * Creates a paginator from the paginator service.
45
     *
46
     * Uses query parameters from the request merged with $defaultParams as
47
     * creation options while retrieving the service.
48
     * Please note that a query parameter with the same name as a default parameter
49
     * overrides the default parameter.
50
     *
51
     *
52
     * @param string $paginatorName
53
     * @param array  $defaultParams
54
     * @param bool   $usePostParams if true, the POST parameters from the request are used.
55
     *
56
     * @return \Zend\Paginator\Paginator
57
     * @throws \InvalidArgumentException
58
     */
59
    public function __invoke($paginatorName, $defaultParams = array(), $usePostParams = false)
60
    {
61
        if (is_bool($defaultParams)) {
62
            $usePostParams = $defaultParams;
63
            $defaultParams = array();
64
        }
65
66
        if (!is_array($defaultParams) && !$defaultParams instanceof \Traversable) {
67
            throw new \InvalidArgumentException('$defaultParams must be an array or implement \Traversable');
68
        }
69
70
        /* @var $controller \Zend\Mvc\Controller\AbstractController
71
         * @var $paginators \Core\Paginator\PaginatorService
72
         * @var $request    \Zend\Http\Request
73
         */
74
        $controller = $this->getController();
75
        $paginators = $this->serviceManager->get('Core/PaginatorService');
76
        $request    = $controller->getRequest();
77
        $params     = $usePostParams
78
            ? $request->getPost()->toArray()
79
            : $request->getQuery()->toArray();
80
81
        // We allow \Traversable so we cannot simply merge.
82
        foreach ($defaultParams as $key => $val) {
83
            if (!isset($params[$key])) {
84
                $params[$key] = $val;
85
            }
86
        }
87
88
        /* try to create $paginator from event listener */
89
        /* @var \Core\EventManager\EventManager $events */
90
        /* @var \Zend\Paginator\Paginator $paginator */
91
        /* @var CreatePaginatorEvent $event */
92
        $events = $this->serviceManager->get('Core/CreatePaginator/Events');
93
        $event = $events->getEvent(CreatePaginatorEvent::EVENT_CREATE_PAGINATOR,$this,[
94
            'paginatorParams' => $params,
95
            'paginators' => $paginators,
96
            'paginatorName' => $paginatorName
97
        ]);
98
        $events->trigger($event);
99
        $paginator = $event->getPaginator();
100
        if(!$paginator instanceof Paginator){
101
            // no paginator created by listener, so let's create default paginator
102
            $paginator = $paginators->get($paginatorName,$params);
103
        }
104
        $paginator->setCurrentPageNumber(isset($params['page']) ? $params['page'] : 1)
105
                  ->setItemCountPerPage(isset($params['count']) ? $params['count'] : 10)
106
                  ->setPageRange(isset($params['range']) ? $params['range'] : 5);
107
108
        return $paginator;
109
110
    }
111
    
112
    /**
113
     * @param ControllerManager $controllerManager
114
     * @return CreatePaginator
115
     */
116
    public static function factory(ControllerManager $controllerManager)
117
    {
118
        return new static($controllerManager->getServiceLocator());
119
    }
120
}
121