/**
  * Pagination is HARD. We let the core do the heavy lifting from
  * a simple representation of the pagination.
  *
  * Generated URLs will include the page number, obviously,
  * but also the sort order and the "q" (facets) parameters.
  *
  * @param ProductSearchQuery  $query
  * @param ProductSearchResult $result
  *
  * @return an array that makes rendering the pagination very easy
  */
 protected function getTemplateVarPagination(ProductSearchQuery $query, ProductSearchResult $result)
 {
     $pagination = new Pagination();
     $pagination->setPage($query->getPage())->setPagesCount(ceil($result->getTotalProductsCount() / $query->getResultsPerPage()));
     $totalItems = $result->getTotalProductsCount();
     $itemsShownFrom = $query->getResultsPerPage() * ($query->getPage() - 1) + 1;
     $itemsShownTo = $query->getResultsPerPage() * $query->getPage();
     return array('total_items' => $totalItems, 'items_shown_from' => $itemsShownFrom, 'items_shown_to' => $itemsShownTo <= $totalItems ? $itemsShownTo : $totalItems, 'pages' => array_map(function ($link) {
         $link['url'] = $this->updateQueryString(array('page' => $link['page']));
         return $link;
     }, $pagination->buildLinks()));
 }