Conditions | 11 |
Paths | 26 |
Total Lines | 52 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
121 |