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

GroupItemPaginateController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 69
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAction() 0 10 1
A getItemsPerPage() 0 5 2
A setActiveSearchResultSet() 0 4 1
A indexAction() 0 18 3
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\Grouping\GroupItem;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
20
21
/**
22
 * Class ResultPaginateController
23
 *
24
 * @author Frans Saris <[email protected]>
25
 * @author Timo Hund <[email protected]>
26
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller
27
 */
28
class GroupItemPaginateController extends AbstractPaginateWidgetController
29
{
30
31
    /**
32
     * @var SearchResultSet
33
     */
34
    protected $resultSet;
35
36
    /**
37
     * @var GroupItem
38
     */
39
    protected $groupItem;
40
41
    /**
42
     * @return void
43
     */
44
    public function initializeAction()
45
    {
46
        parent::initializeAction();
47
48
        $this->resultSet = $this->widgetConfiguration['resultSet'];
49
        $this->groupItem = $this->widgetConfiguration['groupItem'];
50
        $this->configuration['itemsPerPage'] = $this->getItemsPerPage();
51
52
        $this->numberOfPages = (int)ceil($this->groupItem->getNumFound() / $this->configuration['itemsPerPage']);
53
    }
54
55
    /**
56
     * Determines the number of results per page. When nothing is configured 10 will be returned.
57
     *
58
     * @return int
59
     */
60
    protected function getItemsPerPage()
61
    {
62
        $perPage = (int)$this->groupItem->getGroup()->getResultsPerPage();
63
        return $perPage > 0 ? $perPage : 10;
64
    }
65
66
    /**
67
     * @param \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext $controllerContext
68
     * @return \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
69
     */
70
    protected function setActiveSearchResultSet($controllerContext) {
71
        $controllerContext->setSearchResultSet($this->resultSet);
72
        return $controllerContext;
73
    }
74
75
    /**
76
     * @return void
77
     */
78
    public function indexAction()
79
    {
80
        // set current page
81
        $groupName = $this->groupItem->getGroup()->getGroupName();
82
        $groupItemValue = $this->groupItem->getGroupValue();
83
        $this->currentPage = $this->resultSet->getUsedSearchRequest()->getGroupItemPage($groupName, $groupItemValue);
84
        if ($this->currentPage < 1) {
85
            $this->currentPage = 1;
86
        }
87
        $this->view->assign('contentArguments', [$this->widgetConfiguration['as'] => $this->groupItem->getSearchResults(), 'pagination' => $this->buildPagination()]);
88
        $this->view->assign('configuration', $this->configuration);
89
        $this->view->assign('resultSet', $this->resultSet);
90
        $this->view->assign('groupItem', $this->groupItem);
91
92
        if (!empty($this->templatePath)) {
93
            $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...
94
        }
95
    }
96
}
97