Completed
Push — dev-master ( ee40ad...39856d )
by Derek Stephen
01:52
created

I18nHandler::removeI18n()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.004
1
<?php declare(strict_types=1);
2
3
namespace Bone\Server;
4
5
use Bone\Mvc\Router\NotFoundException;
6
use Locale;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Zend\I18n\Translator\Translator;
12
13
class I18nHandler
14
{
15
    const REGEX_LOCALE = '#^/(?P<locale>[a-z]{2}[-_][a-zA-Z]{2})(?:/|$)#';
16
17
    /** @var Translator$translator */
18
    private $translator;
19
20
    /** @var array $supportedLocales */
21
    private $supportedLocales;
22
23
    /**
24
     * InternationalisationMiddleware constructor.
25
     * @param  $helper
26
     * @param string|null $defaultLocale
0 ignored issues
show
Bug introduced by
There is no parameter named $defaultLocale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
27
     */
28 3
    public function __construct(Translator $translator, array $supportedLocales)
29
    {
30 3
        $this->translator = $translator;
31 3
        $this->supportedLocales = $supportedLocales;
32 3
    }
33
34
    /**
35
     * @param ServerRequestInterface $request
36
     * @return ServerRequestInterface
37
     * @throws NotFoundException
38
     */
39 2
    public function handleI18n(ServerRequestInterface $request): ServerRequestInterface
40
    {
41 2
        $uri = $request->getUri();
42 2
        $path = $uri->getPath();
43
44 2
        if (! preg_match(self::REGEX_LOCALE, $path, $matches)) {
45
            $path = '/' . Locale::getDefault() . $path;
46
            $query = $request->getUri()->getQuery();
47
            $query = $query !== null ? '?' . $query : null;
48
            $e = new NotFoundException($path . $query);
49
            $e->setRequest($request);
50
            
51
            throw $e;
52
        }
53
54 2
        $locale = $matches['locale'];
55
56 2
        if (in_array($locale, $this->supportedLocales)) {
57 1
            $locale = Locale::canonicalize($locale);
58 1
            Locale::setDefault($locale);
59 1
            $this->translator->setLocale($locale);
60 1
            $path = substr($path, strlen($locale) + 1);
61 1
            $uri = $uri->withPath($path);
62 1
            $request = $request->withUri($uri);
63
        }
64
65 2
        return $request;
66
    }
67
68
    /**
69
     * @param ServerRequestInterface $request
70
     * @return ServerRequestInterface
71
     */
72 1
    public function removeI18n(ServerRequestInterface $request): ServerRequestInterface
73
    {
74 1
        $uri = $request->getUri();
75 1
        $path = $uri->getPath();
76
77 1
        if (! preg_match(self::REGEX_LOCALE, $path, $matches)) {
78
            return $request;
79
        }
80
81 1
        $locale = $matches['locale'];
82 1
        $path = substr($path, strlen($locale) + 1);
83 1
        $uri = $uri->withPath($path);
84 1
        $request = $request->withUri($uri);
85
86 1
        return $request;
87
    }
88
}