TimezoneLocaleComposer::compose()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 46
rs 8.6316
cc 4
eloc 28
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Composers;
13
14
use DateTime;
15
use DateTimeZone;
16
use Illuminate\Contracts\View\View;
17
use Illuminate\Support\Facades\Config;
18
19
class TimezoneLocaleComposer
20
{
21
    /**
22
     * Timezones and Locales composer.
23
     *
24
     * @param \Illuminate\Contracts\View\View $view
25
     */
26
    public function compose(View $view)
27
    {
28
        $enabledLangs = Config::get('langs');
29
30
        $langs = array_map(function ($lang) use ($enabledLangs) {
31
            $locale = basename($lang);
32
33
            return [$locale => $enabledLangs[$locale]];
34
        }, glob(base_path('resources/lang').'/*'));
35
36
        $langs = call_user_func_array('array_merge', $langs);
37
38
        $regions = [
39
            'Africa' => DateTimeZone::AFRICA,
40
            'America' => DateTimeZone::AMERICA,
41
            'Antarctica' => DateTimeZone::ANTARCTICA,
42
            'Asia' => DateTimeZone::ASIA,
43
            'Atlantic' => DateTimeZone::ATLANTIC,
44
            'Australia' => DateTimeZone::AUSTRALIA,
45
            'Europe' => DateTimeZone::EUROPE,
46
            'Indian' => DateTimeZone::INDIAN,
47
            'Pacific' => DateTimeZone::PACIFIC,
48
            'UTC' => DateTimeZone::UTC,
49
        ];
50
51
        $timezones = [];
52
53
        foreach ($regions as $name => $mask) {
54
            $zones = DateTimeZone::listIdentifiers($mask);
55
56
            foreach ($zones as $timezone) {
57
                // Lets sample the time there right now
58
                $time = new DateTime(null, new DateTimeZone($timezone));
59
60
                $ampm = $time->format('H') > 12 ? ' ('.$time->format('g:i a').')' : '';
61
62
                // Remove region name and add a sample time
63
                $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1).' - '.$time->format('H:i').$ampm;
64
65
                $timezones[$name] = str_replace('_', ' ', $timezones[$name]);
66
            }
67
        }
68
69
        $view->withTimezones($timezones);
0 ignored issues
show
Bug introduced by
The method withTimezones() does not seem to exist on object<Illuminate\Contracts\View\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        $view->withLangs($langs);
0 ignored issues
show
Bug introduced by
The method withLangs() does not seem to exist on object<Illuminate\Contracts\View\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
}
73