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

Continents   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 87
ccs 20
cts 23
cp 0.8696
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A optimizeContinentsData() 0 9 1
A getOptimizedContinentsData() 0 6 1
A formatContinents() 0 8 2
A continents() 0 6 1
A __construct() 0 3 1
1
<?php
2
3
namespace Someshwer\WorldCountries\Utils;
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 the list of continents names
14
 *
15
 * Class Continents
16
 */
17
trait Continents
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
     * Continents constructor.
36
     *
37
     * @param DataRepository $dataRepository
38
     */
39
    public function __construct(DataRepository $dataRepository)
40
    {
41
        $this->data = $dataRepository;
42
    }
43
44
    /**
45
     * Get optimized continents data.
46
     *
47
     * @return string
48
     */
49 1
    private function getOptimizedContinentsData()
50
    {
51 1
        $all_continents_data = $this->data->continents();
52 1
        $continents_data = $this->optimizeContinentsData($all_continents_data);
53
        // return $continents_data = json_decode($continents_data, true);
54 1
        return $continents_data;
55
    }
56
57
    /**
58
     * Optimize continents data.
59
     *
60
     * @param $all_continents_data
61
     *
62
     * @return string
63
     */
64 1
    private function optimizeContinentsData($all_continents_data)
65
    {
66 1
        $str_length = strlen($all_continents_data) - 4;
67 1
        $all_continents_trimmed_data = substr($all_continents_data, 0, 2).substr($all_continents_data, 3, $str_length);
68
        // $hash = new Encrypter($this->en_key, Config::get('app.cipher'));
69 1
        $hash = new Encrypter($this->en_key, $this->cipher);
70 1
        $all_continents = $hash->decrypt($all_continents_trimmed_data);
71
72 1
        return $all_continents;
73
    }
74
75
    /**
76
     * Format continents.
77
     *
78
     * @param $continents
79
     *
80
     * @return $this
81
     */
82 1
    private function formatContinents($continents)
83
    {
84
        return collect($continents)->transform(function ($item) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($continen...ion(...) { /* ... */ }) returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Utils\Continents.
Loading history...
85 1
            $data['name'] = $item;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
86 1
            $data['display_name'] = str_replace('_', ' ', Str::title($item));
87 1
            $data['also_called_as'] = ($item == 'australia') ? 'Oceania' : null;
88
89 1
            return $data;
90 1
        });
91
    }
92
93
    /**
94
     * Get list of continents names.
95
     *
96
     * @return array
97
     */
98 1
    public function continents()
99
    {
100 1
        $continents = $this->getOptimizedContinentsData();
101 1
        $formatted_continents = $this->formatContinents($continents);
102
103 1
        return ['continents' => $formatted_continents];
104
    }
105
}
106