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

StdCodes   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 92.11%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 130
ccs 35
cts 38
cp 0.9211
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A stdCodes() 0 3 1
A __construct() 0 4 1
A stdCodeByCountryName() 0 12 2
A getOptimizedStdCodesData() 0 6 1
A searchStdCodes() 0 14 4
A stdCodeByCountryCode() 0 12 2
A optimizeStdCodesData() 0 7 1
1
<?php
2
3
namespace Someshwer\WorldCountries\Lib;
4
5
use Illuminate\Encryption\Encrypter;
6
use Someshwer\WorldCountries\Data\DataRepository;
7
use Illuminate\Support\Arr;
8
9
/**
10
 * Author: Someshwer Bandapally
11
 * Date: 18-07-2018.
12
 *
13
 * This class provides STD codes data
14
 *
15
 * Class StdCodes
16
 */
17
class StdCodes extends States
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
     * StdCodes constructor.
36
     *
37
     * @param DataRepository $dataRepository
38
     */
39 39
    public function __construct(DataRepository $dataRepository)
40
    {
41 39
        parent::__construct($dataRepository);
42 39
        $this->data = $dataRepository;
43 39
    }
44
45
    /**
46
     * Optimize STD codes data.
47
     *
48
     * @param $all_std_codes_data
49
     *
50
     * @return string
51
     */
52 4
    private function optimizeStdCodesData($all_std_codes_data)
53
    {
54 4
        $str_length = strlen($all_std_codes_data) - 4;
55 4
        $all_std_codes_trimmed_data = substr($all_std_codes_data, 0, 2).substr($all_std_codes_data, 3, $str_length);
56 4
        $hash = new Encrypter($this->en_key, $this->cipher);
57
58 4
        return $hash->decrypt($all_std_codes_trimmed_data);
59
    }
60
61
    /**
62
     * Fetch optimized std codes data.
63
     *
64
     * @return string
65
     */
66 4
    private function getOptimizedStdCodesData()
67
    {
68 4
        $all_std_codes__data = $this->data->stdCodes();
69 4
        $std_codes_data = $this->optimizeStdCodesData($all_std_codes__data);
70
        // $std_codes_data = json_decode($std_codes_data, true);
71 4
        return $std_codes_data;
72
    }
73
74
    /**
75
     * Returns all std codes.
76
     *
77
     * @return string
78
     */
79 1
    public function stdCodes()
80
    {
81 1
        return $this->getOptimizedStdCodesData();
82
    }
83
84
    /**
85
     * Search STD codes.
86
     *
87
     * @param null $search_string
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $search_string is correct as it would always require null to be passed?
Loading history...
88
     *
89
     * @return array|static
90
     */
91 1
    public function searchStdCodes($search_string = null)
92
    {
93 1
        if (is_null($search_string)) {
0 ignored issues
show
introduced by
The condition is_null($search_string) is always true.
Loading history...
94
            return [];
95
        }
96 1
        $std_codes = $this->getOptimizedStdCodesData();
97
98
        return collect($std_codes)->filter(function ($item) use ($search_string) {
99 1
            return (substr(strtolower($item['country_name']), 0, strlen($search_string)) == strtolower($search_string)) ||
100 1
                (strtolower($item['country_code']) == strtolower($search_string)) ||
101 1
                (strpos(strtolower($item['std_code']), strtolower($search_string)) !== false);
102
        })->transform(function ($value) {
103 1
            return Arr::except($value, 'id');
104 1
        })->values();
105
    }
106
107
    /**
108
     * Search STD code by country name.
109
     *
110
     * @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...
111
     *
112
     * @return array|static
113
     */
114 1
    public function stdCodeByCountryName($country_name = null)
115
    {
116 1
        if (is_null($country_name)) {
0 ignored issues
show
introduced by
The condition is_null($country_name) is always true.
Loading history...
117
            return [];
118
        }
119 1
        $std_codes = $this->getOptimizedStdCodesData();
120
121
        return collect($std_codes)->filter(function ($item) use ($country_name) {
122 1
            return substr(strtolower($item['country_name']), 0, strlen($country_name)) == strtolower($country_name);
123
        })->transform(function ($value) {
124 1
            return Arr::except($value, 'id');
125 1
        })->values();
126
    }
127
128
    /**
129
     * Search STD code by country code.
130
     *
131
     * @param null $country_code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $country_code is correct as it would always require null to be passed?
Loading history...
132
     *
133
     * @return array|static
134
     */
135 1
    public function stdCodeByCountryCode($country_code = null)
136
    {
137 1
        if (is_null($country_code)) {
0 ignored issues
show
introduced by
The condition is_null($country_code) is always true.
Loading history...
138
            return [];
139
        }
140 1
        $std_codes = $this->getOptimizedStdCodesData();
141
142
        return collect($std_codes)->filter(function ($item) use ($country_code) {
143 1
            return strtolower($item['country_code']) == strtolower($country_code);
144
        })->transform(function ($value) {
145 1
            return Arr::except($value, 'id');
146 1
        })->values();
147
    }
148
}
149