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

Countries   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 86
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A countries() 0 10 1
A optimizeCountryData() 0 9 1
A searchCountry() 0 16 2
1
<?php
2
3
namespace Someshwer\WorldCountries\Lib;
4
5
use Illuminate\Encryption\Encrypter;
6
use Someshwer\WorldCountries\Data\DataRepository;
7
use Illuminate\Support\Str;
8
9
/**
10
 * Author: Someshwer Bandapally
11
 * Date: 14-07-2018.
12
 *
13
 * This class gives all country names
14
 *
15
 * Class Countries
16
 */
17
class Countries extends ISOCodes
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
     * Countries 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
     * Optimizing the country data.
47
     *
48
     * @param $all_countries_data
49
     *
50
     * @return string
51
     */
52 2
    private function optimizeCountryData($all_countries_data)
53
    {
54 2
        $str_length = strlen($all_countries_data) - 4;
55 2
        $all_countries_trimmed_data = substr($all_countries_data, 0, 2).substr($all_countries_data, 3, $str_length);
56
        // $hash = new Encrypter($this->en_key, Config::get('app.cipher'));
57 2
        $hash = new Encrypter($this->en_key, $this->cipher);
58 2
        $all_countries = $hash->decrypt($all_countries_trimmed_data);
59
60 2
        return $all_countries;
61
    }
62
63
    /**
64
     * Get all countries.
65
     *
66
     * @return array
67
     */
68 1
    public function countries()
69
    {
70 1
        $all_countries_data = $this->data->countries();
71 1
        $all_countries = $this->optimizeCountryData($all_countries_data);
72
        $countries = collect($all_countries)->map(function ($item) {
73
            // return Str::title(str_replace('-',' ', $item));
74 1
            return Str::studly($item);
75 1
        });
76
77 1
        return ['countries' => $countries];
78
    }
79
80
    /**
81
     * Search country by search string.
82
     *
83
     * @param $search_string
84
     *
85
     * @return array
86
     */
87 1
    public function searchCountry($search_string = null)
88
    {
89 1
        $all_countries_data = $this->data->countries();
90 1
        $all_countries = $this->optimizeCountryData($all_countries_data);
91 1
        if ($search_string == null) {
92
            return array_map(function ($it) {
93
                return Str::studly($it);
94
            }, $all_countries);
0 ignored issues
show
Bug introduced by
$all_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

94
            }, /** @scrutinizer ignore-type */ $all_countries);
Loading history...
95
        }
96
        $countries = array_map(function ($it) {
97 1
            return Str::studly($it);
98
        }, array_filter($all_countries, function ($item) use ($search_string) {
0 ignored issues
show
Bug introduced by
$all_countries of type string is incompatible with the type array expected by parameter $input of array_filter(). ( Ignorable by Annotation )

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

98
        }, array_filter(/** @scrutinizer ignore-type */ $all_countries, function ($item) use ($search_string) {
Loading history...
99 1
            return strpos($item, strtolower($search_string)) === 0;
100 1
        }));
101
102 1
        return ['countries' => array_values($countries)];
103
    }
104
}
105