Issues (7)

Site/PrefixedPathIdentifiedSiteResolver.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FH\Bundle\MultiSiteBundle\Site;
6
7
use Symfony\Component\Routing\RequestContext;
8
9
/**
10
 * @author Joris van de Sande <[email protected]>
11
 */
12
final class PrefixedPathIdentifiedSiteResolver implements SiteResolverInterface
13
{
14
    private $siteRepository;
15
    private $defaultIdentifier;
16
    private $identifiers;
17
18
    /**
19
     * @param string[] $identifiers
20
     */
21
    public function __construct(SiteRepositoryInterface $siteRepository, array $identifiers, ?string $defaultIdentifier = null)
22
    {
23
        $this->siteRepository = $siteRepository;
24
        $this->defaultIdentifier = $defaultIdentifier;
25
        $this->identifiers = $identifiers;
26
    }
27
28
    public function resolve(RequestContext $requestContext): SiteInterface
29
    {
30
        $matches = [];
31
32
        if (preg_match('#^/([^/]+)/?#', $requestContext->getPathInfo(), $matches) > 0) {
33
            $identifier = $matches[1];
34
        } else {
35
            $identifier = $this->defaultIdentifier;
36
        }
37
38
        if (empty($identifier)) {
39
            throw SiteNotFoundException::withRequestContext($requestContext);
40
        }
41
42
        foreach ($this->siteRepository->findAll() as $site) {
43
            if (!$site instanceof IdentifiedSiteInterface) {
44
                continue;
45
            }
46
47
            if ($site->matches($identifier)) {
48
                return $site;
49
            }
50
51
            if ($site->matches($this->defaultIdentifier)) {
0 ignored issues
show
It seems like $this->defaultIdentifier can also be of type null; however, parameter $identifier of FH\Bundle\MultiSiteBundl...iteInterface::matches() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            if ($site->matches(/** @scrutinizer ignore-type */ $this->defaultIdentifier)) {
Loading history...
52
                $defaultSite = $site;
53
            }
54
        }
55
56
        if (!empty($defaultSite) && $defaultSite instanceof IdentifiedSiteInterface) {
57
            return $defaultSite;
58
        }
59
60
        throw SiteNotFoundException::withRequestContext($requestContext);
61
    }
62
}
63