Passed
Push — test ( 7f1499...ccf3d3 )
by Someshwer
06:32
created

TimeZones   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 92.5%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 107
ccs 37
cts 40
cp 0.925
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A timezonesByRegion() 0 13 3
A __construct() 0 4 1
A prepareTimezones() 0 13 2
A timezones() 0 3 1
A timezoneRegions() 0 12 3
A searchTimezone() 0 11 3
1
<?php
2
3
namespace Someshwer\WorldCountries\Lib;
4
5
use Someshwer\WorldCountries\Data\DataRepository;
6
7
/**
8
 * Author: Someshwer Bandapally
9
 * Date: 14-07-2018.
10
 *
11
 * This class provides timezones info
12
 *
13
 * Class TimeZones
14
 */
15
class TimeZones extends Currencies
16
{
17
    /**
18
     * @var DataRepository
19
     */
20
    private $data;
21
22
    /**
23
     * TimeZones constructor.
24
     *
25
     * @param DataRepository $dataRepository
26
     */
27 39
    public function __construct(DataRepository $dataRepository)
28
    {
29 39
        parent::__construct($dataRepository);
30 39
        $this->data = $dataRepository;
31 39
    }
32
33
    /**
34
     * This method prepares timezones.
35
     *
36
     * @return array
37
     */
38 4
    private function prepareTimezones()
39
    {
40 4
        $timezone_identifiers_list = $this->data->timezones();
41 4
        $zones_array = [];
42 4
        $timestamp = time();
43 4
        foreach ($timezone_identifiers_list as $key => $zone) {
44 4
            date_default_timezone_set($zone);
45 4
            $zones_array[$key]['region'] = $zones_array[$key]['region'] = substr($zone, 0, strpos($zone, '/'));
46 4
            $zones_array[$key]['zone'] = $zone;
47 4
            $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT '.date('P', $timestamp);
48
        }
49
50 4
        return $zones_array;
51
    }
52
53
    /**
54
     * Returns all timezones.
55
     *
56
     * @return array
57
     */
58 1
    public function timezones()
59
    {
60 1
        return $this->prepareTimezones();
61
    }
62
63
    /**
64
     * Returns all timezones regions.
65
     *
66
     * @return array
67
     */
68 1
    public function timezoneRegions()
69
    {
70 1
        $timezone_regions = $this->prepareTimezones();
71 1
        $timezone_regions_group = collect($timezone_regions)->groupBy('region')->keys();
72 1
        $regions = [];
73 1
        foreach ($timezone_regions_group as $item) {
74 1
            if ($item != '') {
75 1
                $regions[] = $item;
76
            }
77
        }
78
79 1
        return $regions;
80
    }
81
82
    /**
83
     * Fetches and returns timezones by regions.
84
     *
85
     * @param null $region
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $region is correct as it would always require null to be passed?
Loading history...
86
     *
87
     * @return array|mixed
88
     */
89 1
    public function timezonesByRegion($region = null)
90
    {
91 1
        if (!$region) {
0 ignored issues
show
introduced by
$region is of type null, thus it always evaluated to false.
Loading history...
92
            return [];
93
        }
94 1
        $timezone_regions = $this->prepareTimezones();
95 1
        $timezone_regions_group = collect($timezone_regions)->groupBy('region');
96 1
        $regional_timezones = $timezone_regions_group->get($region);
97 1
        if (!$regional_timezones) {
98
            return [];
99
        }
100
101 1
        return $regional_timezones;
102
    }
103
104
    /**
105
     * Searches timezones by name.
106
     *
107
     * @param $search_key
108
     *
109
     * @return array|static
110
     */
111 1
    public function searchTimezone($search_key)
112
    {
113 1
        if (!$search_key || ($search_key == '/')) {
114
            return [];
115
        }
116 1
        $timezones = $this->prepareTimezones();
117
        $filtered_timezones = collect($timezones)->filter(function ($item) use ($search_key) {
118 1
            return strpos(strtolower($item['zone']), strtolower($search_key)) !== false;
119 1
        })->values();
120
121 1
        return $filtered_timezones;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $filtered_timezones returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Lib\TimeZones|array.
Loading history...
122
    }
123
}
124