MultiLanguageFeatures::languageInPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Ffcms\Core\Network\Request;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Any;
7
use Ffcms\Core\Helper\Type\Arr;
8
use Ffcms\Core\Helper\Type\Str;
9
use Ffcms\Templex\Url\UrlRepository;
0 ignored issues
show
Bug introduced by
The type Ffcms\Templex\Url\UrlRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\HttpFoundation\ParameterBag;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
13
/**
14
 * Trait MultiLanguageFeatures. Multilanguage features for Request controller.
15
 * @package Ffcms\Core\Network\Request
16
 * @property ParameterBag $query
17
 */
18
trait MultiLanguageFeatures
19
{
20
    protected $language;
21
    protected $languageInPath = false;
22
23
    /**
24
     * Build multi language pathway binding.
25
     * @return void
26
     */
27
    private function runMultiLanguage(): void
28
    {
29
        // check if multi-language is enabled
30
        if (!App::$Properties->get('multiLanguage')) {
31
            $this->language = App::$Properties->get('singleLanguage');
32
            return;
33
        }
34
35
        // check if domain-lang binding is enabled
36
        if (Any::isArray(App::$Properties->get('languageDomainAlias'))) {
37
            /** @var array $domainAlias */
38
            $domainAlias = App::$Properties->get('languageDomainAlias');
39
            if (Any::isArray($domainAlias) && !Str::likeEmpty($domainAlias[$this->getHost()])) {
0 ignored issues
show
Bug introduced by
It seems like getHost() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

39
            if (Any::isArray($domainAlias) && !Str::likeEmpty($domainAlias[$this->/** @scrutinizer ignore-call */ getHost()])) {
Loading history...
40
                $this->language = $domainAlias[$this->getHost()];
41
            }
42
            return;
43
        }
44
45
        // try to find language in pathway
46
        foreach (App::$Properties->get('languages') as $lang) {
47
            if (Str::startsWith('/' . $lang, $this->getPathInfo())) {
0 ignored issues
show
Bug introduced by
It seems like getPathInfo() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
            if (Str::startsWith('/' . $lang, $this->/** @scrutinizer ignore-call */ getPathInfo())) {
Loading history...
48
                $this->language = $lang;
49
                $this->languageInPath = true;
50
            }
51
        }
52
53
        // try to find in ?lang get
54
        if (!$this->language && Arr::in($this->query->get('lang'), App::$Properties->get('languages'))) {
0 ignored issues
show
Bug introduced by
It seems like Ffcms\Core\App::Properties->get('languages') can also be of type false; however, parameter $haystack of Ffcms\Core\Helper\Type\Arr::in() does only seem to accept array|null, 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

54
        if (!$this->language && Arr::in($this->query->get('lang'), /** @scrutinizer ignore-type */ App::$Properties->get('languages'))) {
Loading history...
55
            $this->language = $this->query->get('lang');
56
        }
57
58
        // language still not defined?!
59
        if (!$this->language) {
60
            $this->setLanguageFromBrowser();
61
        }
62
    }
63
64
    /**
65
     * Set language from browser headers
66
     * @return void
67
     */
68
    private function setLanguageFromBrowser(): void
69
    {
70
        $userLang = App::$Properties->get('singleLanguage');
71
        $browserAccept = $this->getLanguages();
0 ignored issues
show
Bug introduced by
The method getLanguages() does not exist on Ffcms\Core\Network\Request\MultiLanguageFeatures. Did you maybe mean getLanguage()? ( Ignorable by Annotation )

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

71
        /** @scrutinizer ignore-call */ 
72
        $browserAccept = $this->getLanguages();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
        if (Any::isArray($browserAccept) && count($browserAccept) > 0) {
73
            foreach ($browserAccept as $bLang) {
74
                if (Arr::in($bLang, App::$Properties->get('languages'))) {
0 ignored issues
show
Bug introduced by
It seems like Ffcms\Core\App::Properties->get('languages') can also be of type false; however, parameter $haystack of Ffcms\Core\Helper\Type\Arr::in() does only seem to accept array|null, 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

74
                if (Arr::in($bLang, /** @scrutinizer ignore-type */ App::$Properties->get('languages'))) {
Loading history...
75
                    $userLang = $bLang;
76
                    break; // stop calculating, language is founded in priority
77
                }
78
            }
79
        }
80
81
        // parse query string
82
        $queryString = null;
83
        if (count($this->query->all()) > 0) {
84
            $queryString = '?' . http_build_query($this->query->all());
85
        }
86
87
        // build response with redirect to language-based path
88
        $redirectUrl = $this->getSchemeAndHttpHost() . $this->basePath . '/' . $userLang . $this->getPathInfo() . $queryString;
0 ignored issues
show
Bug introduced by
It seems like getSchemeAndHttpHost() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

88
        $redirectUrl = $this->/** @scrutinizer ignore-call */ getSchemeAndHttpHost() . $this->basePath . '/' . $userLang . $this->getPathInfo() . $queryString;
Loading history...
89
        $response = new RedirectResponse($redirectUrl);
90
        $response->send();
91
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
92
    }
93
94
    /**
95
     * Get current language
96
     * @return string|null
97
     */
98
    public function getLanguage(): ?string
99
    {
100
        return $this->language;
101
    }
102
103
    /**
104
     * Set current language
105
     * @param string $lang
106
     * @return bool
107
     */
108
    public function setLanguage($lang): bool
109
    {
110
        if (Arr::in($lang, App::$Properties->get('languages'))) {
0 ignored issues
show
Bug introduced by
It seems like Ffcms\Core\App::Properties->get('languages') can also be of type false; however, parameter $haystack of Ffcms\Core\Helper\Type\Arr::in() does only seem to accept array|null, 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

110
        if (Arr::in($lang, /** @scrutinizer ignore-type */ App::$Properties->get('languages'))) {
Loading history...
111
            $this->language = $lang;
112
            return true;
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * Check if language used in path
120
     * @return bool
121
     */
122
    public function languageInPath(): bool
123
    {
124
        return (bool)$this->languageInPath;
125
    }
126
}
127