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

Currencies::getOptimizedCurrenciesData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Someshwer\WorldCountries\Lib;
4
5
use Illuminate\Encryption\Encrypter;
6
use Someshwer\WorldCountries\Data\DataRepository;
7
8
/**
9
 * Author: Someshwer Bandapally
10
 * Date: 25-05-2018.
11
 *
12
 * This class gives all currency names
13
 *
14
 * Class Currencies
15
 */
16
class Currencies extends StdCodes
17
{
18
    /**
19
     * @var DataRepository
20
     */
21
    private $data;
22
23
    /**
24
     * @var string
25
     */
26
    private $en_key = 'Someshwer1@2#BandapallySomeshwer';
27
28
    /**
29
     * @var string
30
     */
31
    private $cipher = 'AES-256-CBC';
32
33 39
    public function __construct(DataRepository $dataRepository)
34
    {
35 39
        parent::__construct($dataRepository);
36 39
        $this->data = $dataRepository;
37 39
    }
38
39
    /**
40
     * Optimizes currency data.
41
     *
42
     * @param $all_currencies_data
43
     *
44
     * @return string
45
     */
46 6
    private function optimizeCurrenciesData($all_currencies_data)
47
    {
48 6
        $str_length = strlen($all_currencies_data) - 4;
49 6
        $all_currencies_trimmed_data = substr($all_currencies_data, 0, 2).substr($all_currencies_data, 3, $str_length);
50 6
        $hash = new Encrypter($this->en_key, $this->cipher);
51
52 6
        return $hash->decrypt($all_currencies_trimmed_data);
53
    }
54
55
    /**
56
     * Getting optimized currency data.
57
     *
58
     * @return mixed|string
59
     */
60 6
    private function getOptimizedCurrenciesData()
61
    {
62 6
        $all_currencies_data = $this->data->currencies();
63 6
        $currencies_data = $this->optimizeCurrenciesData($all_currencies_data);
64 6
        $currencies_data = json_decode($currencies_data, true);
65
66 6
        return $currencies_data;
67
    }
68
69
    /**
70
     * All currencies data.
71
     *
72
     * @return mixed|string
73
     */
74 1
    public function currencies()
75
    {
76 1
        return $this->getOptimizedCurrenciesData();
77
    }
78
79
    /**
80
     * Search currency by either currency name or country name.
81
     *
82
     * @param $search_key
83
     *
84
     * @return static
85
     */
86 1
    public function searchCurrency($search_key = null)
87
    {
88 1
        if (!$search_key) {
89
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
90
        }
91
92
        return collect($this->getOptimizedCurrenciesData())->filter(function ($item) use ($search_key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($this->ge... /* ... */ })->values() returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
93 1
            return (strpos(strtolower($item['currency_name']), strtolower($search_key)) !== false) ||
94 1
                (strpos(strtolower($item['country_name']), strtolower($search_key)) !== false);
95 1
        })->values();
96
    }
97
98
    /**
99
     * Get currency by country name.
100
     *
101
     * @param $country_name
102
     *
103
     * @return static
104
     */
105 1
    public function currencyByCountryName($country_name = null)
106
    {
107 1
        if (!$country_name) {
108
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
109
        }
110
111
        return collect($this->getOptimizedCurrenciesData())->filter(function ($item) use ($country_name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($this->ge... /* ... */ })->values() returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
112 1
            return strtolower($item['country_name']) == strtolower($country_name);
113 1
        })->values();
114
    }
115
116
    /**
117
     * Get currency b y country code.
118
     *
119
     * @param $country_code
120
     *
121
     * @return static
122
     */
123 2
    public function currencyByCountryCode($country_code = null)
124
    {
125 2
        if (!$country_code) {
126
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
127
        }
128
129
        return collect($this->getOptimizedCurrenciesData())->filter(function ($item) use ($country_code) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($this->ge... /* ... */ })->values() returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
130 2
            return strtolower($item['country_code']) == strtolower($country_code);
131 2
        })->values();
132
    }
133
134
    /**
135
     * Get currency by currency name.
136
     *
137
     * @param $currency_name
138
     *
139
     * @return static
140
     */
141 1
    public function currencyByCurrencyName($currency_name = null)
142
    {
143 1
        if (!$currency_name) {
144
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
145
        }
146
147
        return collect($this->getOptimizedCurrenciesData())->filter(function ($item) use ($currency_name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($this->ge... /* ... */ })->values() returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
148 1
            return strpos(strtolower($item['currency_name']), strtolower($currency_name)) !== false;
149 1
        })->values();
150
    }
151
152
    /**
153
     * Get currency by currency code.
154
     *
155
     * @param $currency_code
156
     *
157
     * @return static
158
     */
159
    public function currencyByCurrencyCode($currency_code = null)
160
    {
161
        if (!$currency_code) {
162
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
163
        }
164
165
        return collect($this->getOptimizedCurrenciesData())->filter(function ($item) use ($currency_code) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($this->ge... /* ... */ })->values() returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Lib\Currencies.
Loading history...
166
            return strtolower($item['currency_code']) == strtolower($currency_code);
167
        })->values();
168
    }
169
}
170