Completed
Push — symfony4 ( b0a2f3...7515a9 )
by
unknown
31:27
created

ExposedRoutesExtractor::isRouteExposed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Bundle\EzPublishCoreBundle\Routing\JsRouting;
7
8
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
9
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Routing\Route;
12
13
/**
14
 * Decorator of FOSJsRouting routes extractor.
15
 * Ensures that base URL contains the SiteAccess part when applicable.
16
 */
17
class ExposedRoutesExtractor implements ExposedRoutesExtractorInterface
18
{
19
    /**
20
     * @var ExposedRoutesExtractorInterface
21
     */
22
    private $innerExtractor;
23
24
    /**
25
     * @var Request
26
     */
27
    private $masterRequest;
28
29
    public function __construct(ExposedRoutesExtractorInterface $innerExtractor, Request $masterRequest)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $masterRequest. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
30
    {
31
        $this->innerExtractor = $innerExtractor;
32
        $this->masterRequest = $masterRequest;
33
    }
34
35
    public function getRoutes()
36
    {
37
        return $this->innerExtractor->getRoutes();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * Will add the SiteAccess if configured in the URI.
44
     *
45
     * @return string
46
     */
47
    public function getBaseUrl()
48
    {
49
        $baseUrl = $this->innerExtractor->getBaseUrl();
50
        $siteAccess = $this->masterRequest->attributes->get('siteaccess');
51
        if ($siteAccess instanceof SiteAccess && $siteAccess->matcher instanceof SiteAccess\URILexer) {
52
            $baseUrl .= $siteAccess->matcher->analyseLink('');
53
        }
54
55
        return $baseUrl;
56
    }
57
58
    public function getPrefix($locale)
59
    {
60
        return $this->innerExtractor->getPrefix($locale);
61
    }
62
63
    public function getHost()
64
    {
65
        return $this->innerExtractor->getHost();
66
    }
67
68
    public function getScheme()
69
    {
70
        return $this->innerExtractor->getScheme();
71
    }
72
73
    public function getCachePath($locale)
74
    {
75
        return $this->innerExtractor->getCachePath($locale);
76
    }
77
78
    public function getResources()
79
    {
80
        return $this->innerExtractor->getResources();
81
    }
82
83
    public function getExposedRoutes()
84
    {
85
        return $this->innerExtractor->getExposedRoutes();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FOS\JsRoutingBundle\Extr...outesExtractorInterface as the method getExposedRoutes() does only exist in the following implementations of said interface: eZ\Bundle\EzPublishCoreB...\ExposedRoutesExtractor.

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...
86
    }
87
88
    public function getPort()
89
    {
90
        return $this->innerExtractor->getPort();
91
    }
92
93
    public function isRouteExposed(Route $route, $name)
94
    {
95
        return $this->innerExtractor->isRouteExposed($route, $name);
96
    }
97
}
98