| Conditions | 11 |
| Paths | 26 |
| Total Lines | 56 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 60 | public function __invoke($paginatorName, $defaultParams = array(), $usePostParams = false) |
||
| 61 | { |
||
| 62 | if (is_bool($defaultParams)) { |
||
| 63 | $usePostParams = $defaultParams; |
||
| 64 | $defaultParams = array(); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (!is_array($defaultParams) && !$defaultParams instanceof \Traversable) { |
||
| 68 | throw new \InvalidArgumentException('$defaultParams must be an array or implement \Traversable'); |
||
| 69 | } |
||
| 70 | |||
| 71 | /* @var $controller \Zend\Mvc\Controller\AbstractController |
||
| 72 | * @var $paginators \Core\Paginator\PaginatorService |
||
| 73 | * @var $request \Zend\Http\Request |
||
| 74 | */ |
||
| 75 | $controller = $this->getController(); |
||
| 76 | $paginators = $this->serviceManager->get('Core/PaginatorService'); |
||
| 77 | $request = $controller->getRequest(); |
||
| 78 | $params = $usePostParams |
||
| 79 | ? $request->getPost()->toArray() |
||
| 80 | : $request->getQuery()->toArray(); |
||
| 81 | |||
| 82 | // We allow \Traversable so we cannot simply merge. |
||
| 83 | foreach ($defaultParams as $key => $val) { |
||
| 84 | if (!isset($params[$key])) { |
||
| 85 | $params[$key] = $val; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /* try to create $paginator from event listener */ |
||
| 90 | /* @var ResponseCollection $response */ |
||
| 91 | /* @var EventManager $eventManager */ |
||
| 92 | /* @var $paginator \Zend\Paginator\Paginator */ |
||
| 93 | $eventManager = $this->serviceManager->get('EventManager'); |
||
| 94 | $response = $eventManager->trigger( |
||
| 95 | static::EVENT_CREATE_PAGINATOR, |
||
| 96 | $this, |
||
| 97 | [ |
||
| 98 | 'params' => $params, |
||
| 99 | 'paginatorName' =>$paginatorName, |
||
| 100 | ] |
||
| 101 | ); |
||
| 102 | $paginator = $response->last(); // get the last result from listener |
||
| 103 | |||
| 104 | if(!$paginator instanceof Paginator){ |
||
| 105 | // paginator is not created, so we create from service |
||
| 106 | $paginator = $paginators->get($paginatorName, $params); |
||
| 107 | } |
||
| 108 | |||
| 109 | $paginator->setCurrentPageNumber(isset($params['page']) ? $params['page'] : 1) |
||
| 110 | ->setItemCountPerPage(isset($params['count']) ? $params['count'] : 10) |
||
| 111 | ->setPageRange(isset($params['range']) ? $params['range'] : 5); |
||
| 112 | |||
| 113 | return $paginator; |
||
| 114 | |||
| 115 | } |
||
| 116 | |||
| 126 |