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

Oceans   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 85
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptimizedOceansData() 0 6 1
A __construct() 0 3 1
A oceans() 0 6 1
A optimizeOceansData() 0 7 1
A formatOceans() 0 8 2
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
 * Provides all oceans names
14
 *
15
 * Class Oceans
16
 */
17
trait Oceans
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
     * Oceans constructor.
36
     *
37
     * @param DataRepository $dataRepository
38
     */
39
    public function __construct(DataRepository $dataRepository)
40
    {
41
        $this->data = $dataRepository;
42
    }
43
44
    /**
45
     * Optimize oceans data.
46
     *
47
     * @param $all_oceans_data
48
     *
49
     * @return string
50
     */
51 1
    private function optimizeOceansData($all_oceans_data)
52
    {
53 1
        $str_length = strlen($all_oceans_data) - 4;
54 1
        $all_oceans_trimmed_data = substr($all_oceans_data, 0, 2).substr($all_oceans_data, 3, $str_length);
55 1
        $hash = new Encrypter($this->en_key, $this->cipher);
56
57 1
        return $hash->decrypt($all_oceans_trimmed_data);
58
    }
59
60
    /**
61
     * Returns optimized oceans data.
62
     *
63
     * @return string
64
     */
65 1
    private function getOptimizedOceansData()
66
    {
67 1
        $all_oceans_data = $this->data->oceans();
68 1
        $continents_data = $this->optimizeOceansData($all_oceans_data);
69
70 1
        return $continents_data;
71
    }
72
73
    /**
74
     * Formats oceans data.
75
     *
76
     * @param $oceans
77
     *
78
     * @return $this
79
     */
80 1
    private function formatOceans($oceans)
81
    {
82
        return collect($oceans)->transform(function ($item) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($oceans)-...ion(...) { /* ... */ }) returns the type Illuminate\Support\Collection which is incompatible with the documented return type Someshwer\WorldCountries\Utils\Oceans.
Loading history...
83 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...
84 1
            $data['display_name'] = Str::title($item);
85 1
            $data['also_called_as'] = ($item == 'antarctic') ? 'Southern Ocean' : null;
86
87 1
            return $data;
88 1
        });
89
    }
90
91
    /**
92
     * Returns all oceans names.
93
     *
94
     * @return array
95
     */
96 1
    public function oceans()
97
    {
98 1
        $oceans = $this->getOptimizedOceansData();
99 1
        $formatted_oceans = $this->formatOceans($oceans);
100
101 1
        return ['oceans' => $formatted_oceans];
102
    }
103
}
104