Timezone::collection()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
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