Timezone   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 32
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTimezone() 0 7 1
A collection() 0 7 2
1
<?php
2
3
namespace Ianrizky\MoslemPray\Support;
4
5
use Illuminate\Support\LazyCollection;
6
use Illuminate\Support\Str;
7
8
class Timezone
9
{
10
    /**
11
     * Return collection of city with its timezone value.
12
     *
13
     * @return \Illuminate\Support\LazyCollection
14
     */
15 2
    public static function collection(): LazyCollection
16
    {
17 2
        return LazyCollection::make(function () {
18 2
            $handle = fopen(__DIR__ . '/../../storage/timezone.ndjson', 'r');
19
20 2
            while (($line = fgets($handle)) !== false) {
21 2
                yield json_decode($line, true);
22
            }
23 2
        });
24
    }
25
26
    /**
27
     * Return timezone value based on given city name.
28
     *
29
     * @param  string  $city
30
     * @param  string|null  $default
31
     * @return string|null
32
     */
33 2
    public static function getTimezone(string $city, string $default = null): ?string
34
    {
35 2
        $timezone = static::collection()->first(function (array $timezone) use ($city) {
36 2
            return Str::contains($timezone['city'], Str::lower($city));
37 2
        }, []);
38
39 2
        return $timezone['timezone'] ?? $default;
40
    }
41
}
42