FluentHelper   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 139
rs 10
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isClassTranslated() 0 4 1
A withLocale() 0 13 4
A usesFluent() 0 3 1
A withLocales() 0 12 3
A get_lang() 0 9 3
A get_locale() 0 6 2
A get_locale_from_lang() 0 22 6
1
<?php
2
3
namespace LeKoala\EmailTemplates\Helpers;
4
5
use InvalidArgumentException;
6
use SilverStripe\i18n\i18n;
7
use TractorCow\Fluent\Model\Locale;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\Model\Locale 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 TractorCow\Fluent\State\FluentState;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\State\FluentState 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...
9
use TractorCow\Fluent\Extension\FluentExtension;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\Extension\FluentExtension 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
/**
12
 * Helps providing base functionnalities where including
13
 * subsite module is optional and yet provide a consistent api
14
 *
15
 * TODO: externalize this to a specific module
16
 */
17
class FluentHelper
18
{
19
    /**
20
     * @var boolean
21
     */
22
    protected static $previousState;
23
24
    /**
25
     * @var int
26
     */
27
    protected static $previousSubsite;
28
29
    /**
30
     * @var array
31
     */
32
    protected static $locale_cache = [];
33
34
    /**
35
     * Do we have the subsite module installed
36
     * TODO: check if it might be better to use module manifest instead?
37
     *
38
     * @return bool
39
     */
40
    public static function usesFluent()
41
    {
42
        return class_exists(FluentState::class);
43
    }
44
45
    /**
46
     * @param string $class
47
     * @return boolean
48
     */
49
    public static function isClassTranslated($class)
50
    {
51
        $singl = singleton($class);
52
        return $singl->hasExtension(FluentExtension::class);
53
    }
54
55
    /**
56
     * Execute the callback in given subsite
57
     *
58
     * @param string|Locale $locale
59
     * @param callable $cb
60
     * @return mixed callback result
61
     */
62
    public static function withLocale($locale, $cb)
63
    {
64
        if (!self::usesFluent() || !$locale) {
65
            $cb();
66
            return;
67
        }
68
        $state = FluentState::singleton();
69
        return $state->withState(function ($state) use ($locale, $cb) {
70
            if (is_object($locale)) {
71
                $locale = $locale->Locale;
72
            }
73
            $state->setLocale($locale);
74
            return $cb();
75
        });
76
    }
77
78
    /**
79
     * Execute the callback for all locales
80
     *
81
     * @param callable $cb
82
     * @return array an array of callback results
83
     */
84
    public static function withLocales($cb)
85
    {
86
        if (!self::usesFluent()) {
87
            $cb();
88
            return [];
89
        }
90
        $allLocales = Locale::get();
91
        $results = [];
92
        foreach ($allLocales as $locale) {
93
            $results[] = self::withLocale($locale, $cb);
94
        }
95
        return $results;
96
    }
97
98
    /**
99
     * Get a locale from the lang
100
     *
101
     * @param string $lang
102
     * @return string
103
     */
104
    public static function get_locale_from_lang($lang)
105
    {
106
        // Normalize if needed
107
        if (strlen($lang) > 2) {
108
            $lang = self::get_lang($lang);
109
        }
110
111
        // Use fluent data
112
        if (class_exists(Locale::class)) {
113
            if (empty(self::$locale_cache)) {
114
                $fluentLocales = Locale::getLocales();
115
                foreach ($fluentLocales as $locale) {
116
                    self::$locale_cache[self::get_lang($locale->Locale)] = $locale->Locale;
117
                }
118
            }
119
            if (isset(self::$locale_cache[$lang])) {
120
                return self::$locale_cache[$lang];
121
            }
122
        }
123
        // Guess
124
        $localesData = i18n::getData();
125
        return $localesData->localeFromLang($lang);
126
    }
127
128
    /**
129
     * Get the right locale (using fluent data if exists)
130
     *
131
     * @return string
132
     */
133
    public static function get_locale()
134
    {
135
        if (class_exists(FluentState::class)) {
136
            return FluentState::singleton()->getLocale();
137
        }
138
        return i18n::get_locale();
139
    }
140
141
    /**
142
     * Make sure we get a proper two characters lang
143
     *
144
     * @param string|object $lang a string or a fluent locale object
145
     * @return string a two chars lang
146
     */
147
    public static function get_lang($lang = null)
148
    {
149
        if (!$lang) {
150
            $lang = self::get_locale();
151
        }
152
        if (is_object($lang)) {
153
            $lang = $lang->Locale;
154
        }
155
        return substr($lang, 0, 2);
156
    }
157
}
158