Completed
Push — master ( 02549d...8f92fc )
by João
03:05
created

Localize   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSupportedLocales() 0 6 2
A prefixFromRequest() 0 6 1
A getLocaleFromUrl() 0 4 1
A parseUrl() 0 12 2
A isValidLocale() 0 4 1
A url() 0 12 3
A cleanUrl() 0 11 2
A pathFromParsedUrl() 0 12 3
1
<?php
2
3
namespace Presspack\Framework\Support\Localization;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Facades\App;
7
8
class Localize
9
{
10
    protected $request;
11
    public $supportedLocales;
12
13
    public function __construct()
14
    {
15
        $this->request = app()['request'];
16
        $this->supportedLocales = $this->getSupportedLocales();
17
    }
18
19
    public function getSupportedLocales()
20
    {
21
        return null === config('presspack.supported_locales')
22
            ? DB::table('icl_languages')->where('active', '1')->pluck('code')->all()
23
            : config('presspack.supported_locales');
24
    }
25
26
    public function prefixFromRequest(int $segment = 0): ?string
27
    {
28
        $url = $this->request->getUri();
29
30
        return $this->getLocaleFromUrl($url, $segment);
31
    }
32
33
    public function getLocaleFromUrl(string $url, int $segment = 0): ?string
34
    {
35
        return $this->parseUrl($url, $segment)['locale'];
36
    }
37
38
    protected function parseUrl(string $url, int $segment = 0)
39
    {
40
        $parsedUrl = parse_url($url);
41
        $parsedUrl['segments'] = array_values(array_filter(explode('/', $parsedUrl['path']), 'strlen'));
42
        $localeCandidate = array_get($parsedUrl['segments'], $segment, false);
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated with message: Arr::get() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
43
        $parsedUrl['locale'] = \in_array($localeCandidate, $this->supportedLocales, true) ? $localeCandidate : null;
44
        $parsedUrl['query'] = array_get($parsedUrl, 'query', false);
0 ignored issues
show
Security Bug introduced by
It seems like $parsedUrl defined by parse_url($url) on line 40 can also be of type false; however, array_get() does only seem to accept object<ArrayAccess>|array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Deprecated Code introduced by
The function array_get() has been deprecated with message: Arr::get() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
45
        $parsedUrl['fragment'] = array_get($parsedUrl, 'fragment', false);
0 ignored issues
show
Security Bug introduced by
It seems like $parsedUrl defined by parse_url($url) on line 40 can also be of type false; however, array_get() does only seem to accept object<ArrayAccess>|array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Deprecated Code introduced by
The function array_get() has been deprecated with message: Arr::get() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
46
        unset($parsedUrl['path']);
47
48
        return $parsedUrl;
49
    }
50
51
    public function isValidLocale(string $locale): bool
52
    {
53
        return \in_array($locale, $this->supportedLocales, true);
54
    }
55
56
    public function url(string $url, string $locale = null, int $segment = 0): string
57
    {
58
        $locale = $locale ?: App::getLocale();
59
        $cleanUrl = $this->cleanUrl($url, $segment);
60
        $parsedUrl = $this->parseUrl($cleanUrl, $segment);
61
        // Check if there are enough segments, if not return url unchanged:
62
        if (\count($parsedUrl['segments']) >= $segment) {
63
            array_splice($parsedUrl['segments'], $segment, 0, $locale);
64
        }
65
66
        return url($this->pathFromParsedUrl($parsedUrl));
0 ignored issues
show
Security Bug introduced by
It seems like $parsedUrl defined by $this->parseUrl($cleanUrl, $segment) on line 60 can also be of type false; however, Presspack\Framework\Supp...ze::pathFromParsedUrl() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
67
    }
68
69
    /**
70
     * Removes the domain and locale (if present) of a given url.
71
     *
72
     * Ex:
73
     * http://www.domain.com/locale/random => /random,
74
     * https://www.domain.com/random => /random, http://domain.com/random?param=value => /random?param=value.
75
     */
76
    public function cleanUrl(string $url, int $segment = 0): string
77
    {
78
        $parsedUrl = $this->parseUrl($url, $segment);
79
        // Remove locale from segments:
80
        if ($parsedUrl['locale']) {
81
            unset($parsedUrl['segments'][$segment]);
82
            $parsedUrl['locale'] = false;
83
        }
84
85
        return $this->pathFromParsedUrl($parsedUrl);
0 ignored issues
show
Security Bug introduced by
It seems like $parsedUrl defined by $this->parseUrl($url, $segment) on line 78 can also be of type false; however, Presspack\Framework\Supp...ze::pathFromParsedUrl() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
86
    }
87
88
    /**
89
     * Returns the uri for the given parsed url based on its segments, query and fragment.
90
     */
91
    protected function pathFromParsedUrl(array $parsedUrl): string
92
    {
93
        $path = '/'.implode('/', $parsedUrl['segments']);
94
        if ($parsedUrl['query']) {
95
            $path .= "?{$parsedUrl['query']}";
96
        }
97
        if ($parsedUrl['fragment']) {
98
            $path .= "#{$parsedUrl['fragment']}";
99
        }
100
101
        return $path;
102
    }
103
}
104