GameTag::build()   F
last analyzed

Complexity

Conditions 11
Paths 516

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 15
nc 516
nop 1
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 11
rs 3.8222
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Pamo\GameTag;
4
5
use Pamo\MultiCurl\MultiCurl;
6
7
/**
8
 * GameTag
9
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
10
 * @SuppressWarnings(PHPMD.NPathComplexity)
11
 */
12
class GameTag
13
{
14
    /**
15
     * @var object $multiCurl tool.
16
     * @var string $baseAddress for the API.
17
     */
18
    private $multiCurl;
19
    private $baseAddress;
20
21
22
23
    /**
24
     * GameTag a tag
25
     *
26
     * @param string $baseAddress for the API.
27
     * @param string $apiKey for authentication.
28
     *
29
     */
30 1
    public function init()
31
    {
32 1
        $this->multiCurl = new MultiCurl();
33 1
        $this->baseAddress = "https://api.rawg.io";
34 1
    }
35
36
37
38
    /**
39
     * Return the API Data
40
     *
41
     * @param string $ipAddress is the IP Address to validate.
42
     *
43
     * @return array
44
     */
45 1
    public function getAllData(string $params) : array
46
    {
47 1
        $url = ["$this->baseAddress" . "$params"];
48
        $header = [
49 1
            "Content-Type: application/json",
50
            "charset=utf-8",
51
            "User-Agent=GameOverflow"
52
        ];
53
54 1
        $data = $this->multiCurl->get($url, $header);
55
56 1
        if ($data && !empty($data[0]["results"])) {
57 1
            $buildData = $this->build($data[0]);
58
        }
59
60 1
        $result = isset($buildData) ? $buildData : [];
61
62 1
        return $result;
63
    }
64
65
66
67
    /**
68
     * Return the organized API Data
69
     *
70
     * @param array $data from the IP.
71
     *
72
     * @return array
73
     */
74 1
    private function build(array $data) : array
75
    {
76
        $buildData = [
77 1
            "next" => isset($data["next"]) ? $data["next"] : null,
78 1
            "previous" => isset($data["previous"]) ? $data["previous"] : null,
79
            "results" => []
80
        ];
81
82 1
        foreach ($data["results"] as $row) {
83
            $result = [
84 1
                "name" => empty($row["name"]) ? null: $row["name"],
85 1
                "parents" => empty($row["parent_platforms"]) ? null : $row["parent_platforms"],
86 1
                "platforms" => empty($row["platforms"]) ? null : $row["platforms"],
87 1
                "released" => empty($row["released"]) ? null : $row["released"],
88 1
                "image" => empty($row["background_image"]) ? null : $row["background_image"],
89 1
                "video" => empty($row["clip"]["clip"]) ? null : $row["clip"]["clip"],
90 1
                "genres" => empty($row["genres"]) ? null : $row["genres"]
91
            ];
92 1
            array_push($buildData["results"], $result);
93
        }
94
95 1
        return $buildData;
96
    }
97
}
98