Completed
Push — master ( 1c0463...6ee2a7 )
by Rafael
34:09
created

getControllerContext()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 2
cts 2
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 6
nop 0
crap 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers;
3
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
20
use ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
21
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrViewHelper;
22
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23
24
/**
25
 * Class AbstractViewHelper
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 * @package ApacheSolrForTypo3\Solr\ViewHelpers
30
 */
31
abstract class AbstractSolrFrontendViewHelper extends AbstractSolrViewHelper
32
{
33
    /**
34
     * @var SolrControllerContext
35
     */
36
    protected $controllerContext;
37
38
    /**
39
     * @return TypoScriptConfiguration
40
     */
41
    protected function getTypoScriptConfiguration()
42
    {
43
        return $this->getControllerContext()->getTypoScriptConfiguration();
44
    }
45
46
    /**
47
     * @return SearchResultSet
48
     */
49
    protected function getSearchResultSet()
50
    {
51
        return $this->getControllerContext()->getSearchResultSet();
52
    }
53
54
    /**
55
     * @todo The fallback on $this->controllerContext is only needed for TYPO3 8 backwards compatibility and can be dropped when TYPO3 8 is not supported anymore
56
     * @return SolrControllerContext
57
     * @throws \InvalidArgumentException
58 33
     */
59
    protected function getControllerContext()
60 33
    {
61 33
        $controllerContext = null;
62
        if (!is_null($this->controllerContext)) {
63
            $controllerContext = $this->controllerContext;
64
        } elseif (method_exists($this->renderingContext, 'getControllerContext')) {
65
            $controllerContext = $this->renderingContext->getControllerContext();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3Fluid\Fluid\Core\Re...nderingContextInterface as the method getControllerContext() does only exist in the following implementations of said interface: Nimut\TestingFramework\R...RenderingContextFixture, TYPO3\CMS\Fluid\Core\Rendering\RenderingContext, TYPO3\CMS\Fluid\Tests\Un...RenderingContextFixture, TYPO3\CMS\Fluid\Tests\Un...RenderingContextFixture.

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...
66
        }
67
68
        if (!$controllerContext instanceof SolrControllerContext) {
69
            throw new \InvalidArgumentException('No valid SolrControllerContext found', 1512998673);
70
        }
71
72
        return $controllerContext;
73
    }
74
75
    /**
76
     * @param RenderingContextInterface $renderingContext
77
     * @return SearchResultSet
78
     */
79
    protected static function getUsedSearchResultSetFromRenderingContext(RenderingContextInterface $renderingContext)
80
    {
81
        $resultSet = $renderingContext->getVariableProvider()->get('resultSet');
82
        return $resultSet;
83
    }
84
}
85