Completed
Push — master ( 753e69...36642f )
by Derek Stephen
02:06 queued 13s
created

I18nHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 71
ccs 26
cts 29
cp 0.8966
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleI18n() 0 23 3
A removeI18n() 0 16 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 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
            throw new NotFoundException($path);
47
        }
48
49 2
        $locale = $matches['locale'];
50
51 2
        if (in_array($locale, $this->supportedLocales)) {
52 1
            $locale = Locale::canonicalize($locale);
53 1
            Locale::setDefault($locale);
54 1
            $this->translator->setLocale($locale);
55 1
            $path = substr($path, strlen($locale) + 1);
56 1
            $uri = $uri->withPath($path);
57 1
            $request = $request->withUri($uri);
58
        }
59
60 2
        return $request;
61
    }
62
63
    /**
64
     * @param ServerRequestInterface $request
65
     * @return ServerRequestInterface
66
     */
67 1
    public function removeI18n(ServerRequestInterface $request): ServerRequestInterface
68
    {
69 1
        $uri = $request->getUri();
70 1
        $path = $uri->getPath();
71
72 1
        if (! preg_match(self::REGEX_LOCALE, $path, $matches)) {
73
            return $request;
74
        }
75
76 1
        $locale = $matches['locale'];
77 1
        $path = substr($path, strlen($locale) + 1);
78 1
        $uri = $uri->withPath($path);
79 1
        $request = $request->withUri($uri);
80
81 1
        return $request;
82
    }
83
}