Completed
Push — master ( ec0634...a07099 )
by Ivan
04:05 queued 02:52
created

Localizer::localeIsAcceptable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace CodeZero\Localizer;
4
5
use CodeZero\Localizer\Detectors\Detector;
6
use Illuminate\Support\Facades\App;
7
use Illuminate\Support\Facades\Config;
8
9
class Localizer
10
{
11
    /**
12
     * Supported locales.
13
     *
14
     * @var \Illuminate\Support\Collection|array
15
     */
16
    protected $locales;
17
18
    /**
19
     * \CoderZero\Localizer\Detectors\Detector instances.
20
     *
21
     * @var \Illuminate\Support\Collection|array
22
     */
23
    protected $detectors;
24
25
    /**
26
     * \CoderZero\Localizer\Stores\Store instances.
27
     *
28
     * @var \Illuminate\Support\Collection|array
29
     */
30
    protected $stores;
31
32
    /**
33
     * Create a new Localizer instance.
34
     *
35
     * @param \Illuminate\Support\Collection|array $locales
36
     * @param \Illuminate\Support\Collection|array $detectors
37
     * @param \Illuminate\Support\Collection|array $stores
38
     */
39 15
    public function __construct($locales = null, $detectors = null, $stores = null)
40
    {
41 15
        $this->locales = $locales ?: Config::get('localizer.supported-locales');
42 15
        $this->detectors = $detectors ?: Config::get('localizer.detectors');
43 15
        $this->stores = $stores ?: Config::get('localizer.stores');
44 15
    }
45
46
    /**
47
     * Detect any supported locale and return the first match.
48
     *
49
     * @return string|false
50
     */
51 14
    public function detect()
52
    {
53 14
        foreach ($this->detectors as $detector) {
54 14
            $detector = $this->getInstance($detector);
55 14
            $locales = (array) $detector->detect();
56
57 14
            foreach ($locales as $locale) {
58 14
                if ($this->localeIsAcceptable($locale, $detector)) {
59 14
                    return $locale;
60
                }
61
62
            }
63
        }
64
65 1
        return false;
66
    }
67
68
    /**
69
     * Store the given locale.
70
     *
71
     * @param string $locale
72
     *
73
     * @return void
74
     */
75 9
    public function store($locale)
76
    {
77 9
        foreach ($this->stores as $store) {
78 9
            $this->getInstance($store)->store($locale);
79
        }
80 9
    }
81
82
    /**
83
     * Determine if the given locale may be returned.
84
     *
85
     * @param string $locale
86
     * @param \CodeZero\Localizer\Detectors\Detector $detector
87
     *
88
     * @return bool
89
     */
90 14
    protected function localeIsAcceptable($locale, Detector $detector)
91
    {
92 14
        if (empty($locale)) {
93 1
            return false;
94
        }
95
96 14
        return ! $detector->shouldValidate() || $this->isSupportedLocale($locale);
97
    }
98
99
    /**
100
     * Check if the given locale is supported.
101
     *
102
     * @param mixed $locale
103
     *
104
     * @return bool
105
     */
106 11
    protected function isSupportedLocale($locale)
107
    {
108 11
        return in_array($locale, $this->locales);
109
    }
110
111
    /**
112
     * Get the class from Laravel's IOC container if it is a string.
113
     *
114
     * @param mixed $class
115
     *
116
     * @return mixed
117
     */
118 15
    protected function getInstance($class)
119
    {
120 15
        if (is_string($class)) {
121 8
            return App::make($class);
122
        }
123
124 7
        return $class;
125
    }
126
}
127