Localizer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 0
dl 0
loc 113
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A detect() 0 14 4
A store() 0 6 2
A setSupportedLocales() 0 6 1
A isSupportedLocale() 0 4 1
A getInstance() 0 8 2
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