Passed
Push — dev ( 451d27...7f1499 )
by Someshwer
02:53
created

UnionTerritories::formatTerritories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Someshwer\WorldCountries\Utils;
4
5
use Illuminate\Encryption\Encrypter;
0 ignored issues
show
Bug introduced by
The type Illuminate\Encryption\Encrypter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Someshwer\WorldCountries\Data\DataRepository;
7
8
/**
9
 * Author: Someshwer Bandapally
10
 * Date: 14-07-2018.
11
 *
12
 * Gives union territories names
13
 *
14
 * Class UnionTerritories
15
 */
16
trait UnionTerritories
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
    /**
34
     * UnionTerritories constructor.
35
     *
36
     * @param DataRepository $dataRepository
37
     */
38
    public function __construct(DataRepository $dataRepository)
39
    {
40
        $this->data = $dataRepository;
41
    }
42
43
    /**
44
     * Optimizes union territories data.
45
     *
46
     * @param $all_territories_data
47
     *
48
     * @return string
49
     */
50
    private function optimizeTerritoriesData($all_territories_data)
51
    {
52
        $str_length = strlen($all_territories_data) - 4;
53
        $all_territories_trimmed_data = substr($all_territories_data, 0, 2).substr($all_territories_data, 3, $str_length);
54
        $hash = new Encrypter($this->en_key, $this->cipher);
55
56
        return $hash->decrypt($all_territories_trimmed_data);
57
    }
58
59
    /**
60
     * Fetches union territories names from a file.
61
     *
62
     * @return string
63
     */
64
    private function getOptimizedTerritoriesData()
65
    {
66
        $all_territories_data = $this->data->unionTerritories();
67
        $territories_data = $this->optimizeTerritoriesData($all_territories_data);
68
69
        return $territories_data;
70
    }
71
72
    /**
73
     * Formats union territories.
74
     *
75
     * @param $territories
76
     *
77
     * @return static
78
     */
79
    private function formatTerritories($territories)
80
    {
81
        return collect($territories)->transform(function ($item, $key) {
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

81
        return /** @scrutinizer ignore-call */ collect($territories)->transform(function ($item, $key) {
Loading history...
82
            $data['name'] = $key;
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...
83
            $data['display_name'] = str_replace('_', ' ', title_case($key));
0 ignored issues
show
Bug introduced by
The function title_case was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

83
            $data['display_name'] = str_replace('_', ' ', /** @scrutinizer ignore-call */ title_case($key));
Loading history...
84
            $data['capital'] = str_replace('_', ' ', title_case($item));
85
            $data['country'] = 'India';
86
87
            return $data;
88
        })->values();
89
    }
90
91
    /**
92
     * Returns all union territories names.
93
     *
94
     * @return array
95
     */
96
    public function unionTerritories()
97
    {
98
        $territories = $this->getOptimizedTerritoriesData();
99
        $formatted_territories = $this->formatTerritories($territories);
100
101
        return ['union_territories' => ['india' => $formatted_territories]];
102
    }
103
}
104