Completed
Push — master ( 9819c1...f2d6e9 )
by Rafael
06:19
created

ResultPaginateController::buildPagination()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.3035

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 15
cp 0.7332
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 8
nop 0
crap 4.3035
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
18
19
20
/**
21
 * Class ResultPaginateController
22
 *
23
 * @author Frans Saris <[email protected]>
24
 * @author Timo Hund <[email protected]>
25
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller
26
 */
27
class ResultPaginateController extends AbstractPaginateWidgetController
28
{
29
30
    /**
31
     * @var SearchResultSet
32
     */
33
    protected $resultSet;
34
35
    /**
36
     * @return void
37
     */
38
    public function initializeAction()
39
    {
40
        parent::initializeAction();
41
42
        $this->resultSet = $this->widgetConfiguration['resultSet'];
43
        $this->configuration['itemsPerPage'] = $this->getItemsPerPage();
44
45
        $this->numberOfPages = (int)ceil($this->resultSet->getUsedSearch()->getNumberOfResults() / $this->configuration['itemsPerPage']);
46
    }
47
48
    /**
49
     * Determines the number of results per page. When nothing is configured 10 will be returned.
50
     *
51
     * @return int
52
     */
53
    protected function getItemsPerPage()
54
    {
55
        $perPage = (int)$this->resultSet->getUsedSearch()->getQuery()->getResultsPerPage();
56
        return $perPage > 0 ? $perPage : 10;
57
    }
58
59
    /**
60
     * @param \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext $controllerContext
61
     * @return \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
62
     */
63
    protected function setActiveSearchResultSet($controllerContext) {
64
        $controllerContext->setSearchResultSet($this->resultSet);
65
        return $controllerContext;
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public function indexAction()
72
    {
73
        // set current page
74
        $this->currentPage = $this->resultSet->getUsedPage();
75
        if ($this->currentPage < 1) {
76
            $this->currentPage = 1;
77
        }
78
        $this->view->assign('contentArguments', [$this->widgetConfiguration['as'] => $this->resultSet->getSearchResults(), 'pagination' => $this->buildPagination()]);
79
        $this->view->assign('configuration', $this->configuration);
80 27
        $this->view->assign('resultSet', $this->resultSet);
81
        if (!empty($this->templatePath)) {
82 27
            $this->view->setTemplatePathAndFilename($this->templatePath);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3\CMS\Extbase\Mvc\View\ViewInterface as the method setTemplatePathAndFilename() does only exist in the following implementations of said interface: TYPO3\CMS\Fluid\View\StandaloneView, TYPO3\CMS\Fluid\View\TemplateView.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
83
        }
84 27
    }
85
}
86