Completed
Push — dev-master ( 3d2c20...b7e24f )
by Derek Stephen
08:18 queued 03:13
created

I18nHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 70
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleI18n() 0 20 2
A removeI18n() 0 19 2
1
<?php declare(strict_types=1);
2
3
namespace Bone\Server;
4
5
use League\Route\Http\Exception\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
    public function __construct(Translator $translator, array $supportedLocales)
29
    {
30
        $this->translator = $translator;
31
        $this->supportedLocales = $supportedLocales;
32
    }
33
34
    /**
35
     * @param ServerRequestInterface $request
36
     * @return ServerRequestInterface
37
     */
38
    public function handleI18n(ServerRequestInterface $request): ServerRequestInterface
39
    {
40
        $uri = $request->getUri();
41
        $path = $uri->getPath();
42
43
        if (! preg_match(self::REGEX_LOCALE, $path, $matches)) {
44
            $path = '/' . Locale::getDefault() . $path;
45
            throw new NotFoundException($path);
46
        }
47
48
        $locale = $matches['locale'];
49
        $locale = Locale::canonicalize($locale);
50
        Locale::setDefault($locale);
51
        $this->translator->setLocale($locale);
52
        $path = substr($path, strlen($locale) + 1);
53
        $uri = $uri->withPath($path);
54
        $request = $request->withUri($uri);
55
56
        return $request;
57
    }
58
59
    /**
60
     * @param ServerRequestInterface $request
61
     * @return ServerRequestInterface
62
     */
63
    public function removeI18n(ServerRequestInterface $request): ServerRequestInterface
64
    {
65
        $uri = $request->getUri();
66
        $path = $uri->getPath();
67
68
        if (! preg_match(self::REGEX_LOCALE, $path, $matches)) {
69
            return $request;
70
        }
71
72
        $locale = $matches['locale'];
73
        $locale = Locale::canonicalize($locale);
74
        Locale::setDefault($locale);
75
        $this->translator->setLocale($locale);
76
        $path = substr($path, strlen($locale) + 1);
77
        $uri = $uri->withPath($path);
78
        $request = $request->withUri($uri);
79
80
        return $request;
81
    }
82
}