Localizer::detect()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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