Issues (238)

src/Http/Middleware/TranslationMiddleware.php (16 issues)

1
<?php
2
namespace Translation\Http\Middleware;
3
4
use Closure;
5
use Illuminate\Config\Repository as Config;
0 ignored issues
show
The type Illuminate\Config\Repository 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...
6
use Illuminate\Foundation\Application;
0 ignored issues
show
The type Illuminate\Foundation\Application 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...
7
use Illuminate\View\Factory as ViewFactory;
0 ignored issues
show
The type Illuminate\View\Factory 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...
8
use Translation\Repositories\LanguageRepository;
9
use Translation\UriLocalizer;
0 ignored issues
show
The type Translation\UriLocalizer 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
11
class TranslationMiddleware
12
{
13
    /**
14
     *  Constructor
15
     *
16
     * @param Translation\UriLocalizer                    $uriLocalizer
0 ignored issues
show
The type Translation\Http\Middlew...ranslation\UriLocalizer 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...
17
     * @param Translation\Repositories\LanguageRepository $languageRepository
0 ignored issues
show
The type Translation\Http\Middlew...ries\LanguageRepository was not found. Did you mean Translation\Repositories\LanguageRepository? If so, make sure to prefix the type with \.
Loading history...
18
     * @param Illuminate\Config\Repository                $config             Laravel config
0 ignored issues
show
The type Translation\Http\Middlew...inate\Config\Repository 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...
19
     * @param Illuminate\View\Factory                     $viewFactory
0 ignored issues
show
The type Translation\Http\Middlew...Illuminate\View\Factory 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...
20
     * @param Illuminate\Foundation\Application           $app
0 ignored issues
show
The type Translation\Http\Middlew...\Foundation\Application 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...
21
     */
22
    public function __construct(UriLocalizer $uriLocalizer, LanguageRepository $languageRepository, Config $config, ViewFactory $viewFactory, Application $app)
23
    {
24
        $this->uriLocalizer       = $uriLocalizer;
0 ignored issues
show
Bug Best Practice introduced by
The property uriLocalizer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
        $this->languageRepository = $languageRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property languageRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
        $this->config             = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        $this->viewFactory        = $viewFactory;
0 ignored issues
show
Bug Best Practice introduced by
The property viewFactory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        $this->app                = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
    }
30
31
    /**
32
     * Handle an incoming request.
33
     *
34
     * @param  \Illuminate\Http\Request $request
0 ignored issues
show
The type Illuminate\Http\Request 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...
35
     * @param  \Closure                 $next
36
     * @param  integer                  $segment Index of the segment containing locale info
37
     * @return mixed
38
     */
39
    public function handle($request, Closure $next, $segment = 0)
40
    {
41
        // Ignores all non GET requests:
42
        if ($request->method() !== 'GET') {
43
            return $next($request);
44
        }
45
46
        $currentUrl    = $request->getUri();
47
        $uriLocale     = $this->uriLocalizer->getLocaleFromUrl($currentUrl, $segment);
48
        $defaultLocale = $this->config->get('app.locale');
49
50
        // If a locale was set in the url:
51
        if ($uriLocale) {
52
            $currentLanguage     = $this->languageRepository->findByLocale($uriLocale);
53
            $selectableLanguages = $this->languageRepository->allExcept($uriLocale);
54
            $altLocalizedUrls    = [];
55
            foreach ($selectableLanguages as $lang) {
56
                $altLocalizedUrls[] = [
57
                    'locale' => $lang->locale,
58
                    'name'   => $lang->name,
59
                    'url'    => $this->uriLocalizer->localize($currentUrl, $lang->locale, $segment),
60
                ];
61
            }
62
63
            // Set app locale
64
            $this->app->setLocale($uriLocale);
65
66
            // Share language variable with views:
67
            $this->viewFactory->share('currentLanguage', $currentLanguage);
68
            $this->viewFactory->share('selectableLanguages', $selectableLanguages);
69
            $this->viewFactory->share('altLocalizedUrls', $altLocalizedUrls);
70
71
            // Set locale in session:
72
            if ($request->hasSession() && $request->session()->get('waavi.translation.locale') !== $uriLocale) {
73
                $request->session()->put('waavi.translation.locale', $uriLocale);
74
            }
75
            return $next($request);
76
        }
77
78
        // If no locale was set in the url, check the session locale
79
        if ($request->hasSession() && $sessionLocale = $request->session()->get('waavi.translation.locale')) {
80
            if ($this->languageRepository->isValidLocale($sessionLocale)) {
81
                return redirect()->to($this->uriLocalizer->localize($currentUrl, $sessionLocale, $segment));
0 ignored issues
show
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

81
                return /** @scrutinizer ignore-call */ redirect()->to($this->uriLocalizer->localize($currentUrl, $sessionLocale, $segment));
Loading history...
82
            }
83
        }
84
85
        // If no locale was set in the url, check the browser's locale:
86
        $browserLocale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
87
        if ($this->languageRepository->isValidLocale($browserLocale)) {
88
            return redirect()->to($this->uriLocalizer->localize($currentUrl, $browserLocale, $segment));
89
        }
90
91
        // If not, redirect to the default locale:
92
        // Keep flash data.
93
        if ($request->hasSession()) {
94
            $request->session()->reflash();
95
        }
96
        return redirect()->to($this->uriLocalizer->localize($currentUrl, $defaultLocale, $segment));
97
    }
98
}
99