Completed
Push — dev-master ( 39856d...09323b )
by Derek Stephen
01:46
created

I18nHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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
    /** @var string $defaultLocale */
24
    private $defaultLocale;
25
26
    /**
27
     * InternationalisationMiddleware constructor.
28
     * @param  $helper
29
     * @param string|null $defaultLocale
30
     */
31
    public function __construct(Translator $translator, array $supportedLocales, string $defaultLocale)
32
    {
33
        $this->translator = $translator;
34
        $this->supportedLocales = $supportedLocales;
35
        $this->defaultLocale = $defaultLocale;
36
    }
37
38
    /**
39
     * @param ServerRequestInterface $request
40
     * @return ServerRequestInterface
41
     * @throws NotFoundException
42
     */
43
    public function handleI18n(ServerRequestInterface $request): ServerRequestInterface
44
    {
45
        $uri = $request->getUri();
46
        $path = $uri->getPath();
47
48
        if (! preg_match(self::REGEX_LOCALE, $path, $matches)) {
49
            $locale = Locale::canonicalize($this->defaultLocale);
0 ignored issues
show
Unused Code introduced by
$locale is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
            Locale::setDefault($this->defaultLocale);
51
52
            return $request;
53
        }
54
55
        $locale = $matches['locale'];
56
57
        if (in_array($locale, $this->supportedLocales)) {
58
            $locale = Locale::canonicalize($locale);
59
            Locale::setDefault($locale);
60
            $this->translator->setLocale($locale);
61
            $path = substr($path, strlen($locale) + 1);
62
            $uri = $uri->withPath($path);
63
            $request = $request->withUri($uri);
64
        }
65
66
        return $request;
67
    }
68
69
    /**
70
     * @param ServerRequestInterface $request
71
     * @return ServerRequestInterface
72
     */
73
    public function removeI18n(ServerRequestInterface $request): ServerRequestInterface
74
    {
75
        $uri = $request->getUri();
76
        $path = $uri->getPath();
77
78
        if (! preg_match(self::REGEX_LOCALE, $path, $matches)) {
79
            return $request;
80
        }
81
82
        $locale = $matches['locale'];
83
        $path = substr($path, strlen($locale) + 1);
84
        $uri = $uri->withPath($path);
85
        $request = $request->withUri($uri);
86
87
        return $request;
88
    }
89
}