Issues (118)

src/Lib/States.php (13 issues)

1
<?php
2
3
namespace Someshwer\WorldCountries\Lib;
4
5
use Illuminate\Encryption\Encrypter;
0 ignored issues
show
The type Illuminate\Encryption\Encrypter 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...
6
use Someshwer\WorldCountries\Data\DataRepository;
7
use Someshwer\WorldCountries\Helpers\MyPaginate;
8
9
/**
10
 * Author: Someshwer Bandapally
11
 * Date: 24-07-2018.
12
 *
13
 * This class provides States data like all state names
14
 *
15
 * Class StdCodes
16
 */
17
class States extends Cities
18
{
19
    /**
20
     * @var DataRepository
21
     */
22
    private $data;
23
24
    /**
25
     * @var string
26
     */
27
    private $en_key = 'Someshwer1@2#BandapallySomeshwer';
28
29
    /**
30
     * @var string
31
     */
32
    private $cipher = 'AES-256-CBC';
33
34
    /**
35
     * States constructor.
36
     *
37
     * @param DataRepository $dataRepository
38
     */
39
    public function __construct(DataRepository $dataRepository)
40
    {
41
        parent::__construct($dataRepository);
42
        $this->data = $dataRepository;
43
    }
44
45
    /**
46
     * Optimize all countries data.
47
     *
48
     * @param $all_countries_data
49
     *
50
     * @return string
51
     */
52
    private function optimizeCountriesData($all_countries_data)
53
    {
54
        $str_length = strlen($all_countries_data) - 4;
55
        $all_countries_trimmed_data = substr($all_countries_data, 0, 2).substr($all_countries_data, 3, $str_length);
56
        $hash = new Encrypter($this->en_key, $this->cipher);
57
58
        return $hash->decrypt($all_countries_trimmed_data);
59
    }
60
61
    /**
62
     * Fetch optimized countries data.
63
     *
64
     * @return string
65
     */
66
    private function getOptimizedCountriesData()
67
    {
68
        $all_countries_data = $this->data->countriesHelper();
69
        $countries_data = $this->optimizeCountriesData($all_countries_data);
70
        // $std_codes_data = json_decode($std_codes_data, true);
71
        return $countries_data;
72
    }
73
74
    /**
75
     * Optimize states data.
76
     *
77
     * @param $all_states_data
78
     *
79
     * @return mixed
80
     */
81
    private function optimizeStatesData($all_states_data)
82
    {
83
        $str_length = strlen($all_states_data) - 15;
84
        $all_states_trimmed_data = substr($all_states_data, 0, 14).substr($all_states_data, 15, $str_length);
85
86
        return unserialize($all_states_trimmed_data);
87
    }
88
89
    /**
90
     * Fetch optimized states data.
91
     *
92
     * @return mixed
93
     */
94
    private function getOptimizedStatesData()
95
    {
96
        $all_states_data = $this->data->states();
97
        $states_data = $this->optimizeStatesData($all_states_data);
98
99
        return $states_data;
100
    }
101
102
    /**
103
     * Format states data.
104
     *
105
     * @return array
106
     */
107
    private function formatStatesData()
108
    {
109
        $countries = $this->getOptimizedCountriesData();
110
        $states = $this->getOptimizedStatesData();
111
        $grouped_countries_collection = collect($countries)->groupBy('id');
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $grouped_countries_collection = /** @scrutinizer ignore-call */ collect($countries)->groupBy('id');
Loading history...
112
        $result = [];
113
        foreach ($states as $state) {
114
            $country = $grouped_countries_collection->get($state['country_id']);
115
            $result[] = [
116
                'state'   => $state['state_name'],
117
                'country' => $country[0]['country_name'],
118
            ];
119
        }
120
121
        return $result;
122
    }
123
124
    /**
125
     * Returns all states.
126
     *
127
     * If pagination is enabled for states in config file
128
     * then the result contains paginated data otherwise all records wil be returned.
129
     *
130
     * @param null $page_number
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $page_number is correct as it would always require null to be passed?
Loading history...
131
     *
132
     * @return array
133
     */
134
    public function states($page_number = null)
135
    {
136
        $page_number = ($page_number == null) ? 1 : $page_number;
0 ignored issues
show
The condition $page_number == null is always true.
Loading history...
137
        $states_data = $this->formatStatesData();
138
        if (config('world.pagination.states') == false) {
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
        if (/** @scrutinizer ignore-call */ config('world.pagination.states') == false) {
Loading history...
139
            return $states_data;
140
        }
141
        $per_page = config('world.pagination.states_per_page');
142
        $ceil_val = ceil(count($states_data) / $per_page);
143
        $request_url = request()->url();
0 ignored issues
show
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
        $request_url = /** @scrutinizer ignore-call */ request()->url();
Loading history...
144
        $total_records = count($states_data);
145
        $pagination_data = MyPaginate::getPagination(
146
            $request_url,
147
            $page_number,
148
            $per_page,
149
            $ceil_val,
150
            $total_records
151
        );
152
        $data = collect($states_data)->forPage($page_number, $per_page)->values();
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        $data = /** @scrutinizer ignore-call */ collect($states_data)->forPage($page_number, $per_page)->values();
Loading history...
153
        $pagination_data['data'] = $data;
154
155
        return $pagination_data;
156
    }
157
158
    /**
159
     * Search state by any name.
160
     *
161
     * @param null $search_key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $search_key is correct as it would always require null to be passed?
Loading history...
162
     *
163
     * @return array
164
     */
165
    public function searchStates($search_key = null)
166
    {
167
        if (is_null($search_key)) {
0 ignored issues
show
The condition is_null($search_key) is always true.
Loading history...
168
            return [];
169
        }
170
        $states = $this->formatStatesData();
171
172
        return array_values(array_filter($states, function ($item) use ($search_key) {
173
            return starts_with(strtolower($item['state']), strtolower($search_key));
0 ignored issues
show
The function starts_with was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
            return /** @scrutinizer ignore-call */ starts_with(strtolower($item['state']), strtolower($search_key));
Loading history...
174
        }));
175
    }
176
177
    /**
178
     * Returns country names for states.
179
     *
180
     * @return array
181
     */
182
    public function countriesForStates()
183
    {
184
        $countries = $this->getOptimizedCountriesData();
185
186
        return array_map(function ($item) {
187
            return $item['country_name'];
188
        }, $countries);
0 ignored issues
show
$countries of type string is incompatible with the type array expected by parameter $arr1 of array_map(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
        }, /** @scrutinizer ignore-type */ $countries);
Loading history...
189
    }
190
191
    /**
192
     * Returns all states belongs to given country name.
193
     *
194
     * @param null $country_name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $country_name is correct as it would always require null to be passed?
Loading history...
195
     *
196
     * @return array
197
     */
198
    public function getStatesByCountry($country_name = null)
199
    {
200
        if (is_null($country_name)) {
0 ignored issues
show
The condition is_null($country_name) is always true.
Loading history...
201
            return [];
202
        }
203
        $states = $this->formatStatesData();
204
205
        return array_values(array_filter($states, function ($item) use ($country_name) {
206
            return strtolower($item['country']) == strtolower($country_name);
207
        }));
208
    }
209
}
210