GeoLocationByIpModel::setMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bashar\GeoLocationModel;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * A sample controller to show how a controller class can be implemented.
10
 * The controller will be injected with $di if implementing the interface
11
 * ContainerInjectableInterface, like this sample class does.
12
 * The controller is mounted on a particular route and can then handle all
13
 * requests for that mount point.
14
 *
15
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
16
 */
17
class GeoLocationByIpModel implements ContainerInjectableInterface
18
{
19
    use ContainerInjectableTrait;
20
21
    /**
22
     * @var string
23
     */
24
    private $message = "127.0.0.1";
25
    private $apiResult;
26
    private $geoApi;
27
28
29
    /**
30
     * returns the api from the config file "ipstackcfg"
31
     *
32
     */
33 4
    public function getDetails() : string
34
    {
35 4
        return $this->message;
36
    }
37
38
39
    /**
40
     * Sets the api from the config file
41
     * into the controller.
42
     *
43
     */
44 13
    public function setMessage($message) : void
45
    {
46 13
        $this->message = $message;
47 13
    }
48
49
50
    /**
51
     * Sets the api from the config file
52
     * into the controller.
53
     *
54
     */
55 2
    public function setGeoApi($enteredIp) : void
56
    {
57 2
        $this->geoApi = "http://api.ipstack.com/". $enteredIp . "?access_key=" . $this->message .
58 2
        '&hostname=1&security=1';
59 2
    }
60
61
62
    /**
63
     * returns the api from the config file "ipstackcfg"
64
     *
65
     */
66 1
    public function getGeoApi()
67
    {
68 1
        return $this->geoApi;
69
    }
70
71
72
    /**
73
     * Gets the Geo Location info in
74
     * Gets the Geo Location info in
75
     * an array
76
     *
77
     */
78 2
    public function doCurl($geoApi) : string
79
    {
80
        // create & initialize a curl session
81 2
        $curl = curl_init();
82
83
        // set our url with curl_setopt()
84 2
        curl_setopt($curl, CURLOPT_URL, $geoApi);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

84
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $geoApi);
Loading history...
85
86
        // return the transfer as a string, also with setopt()
87 2
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
88
89
        // curl_exec() executes the started curl session
90
        // $output contains the output string
91 2
        $output = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

91
        $output = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
92
93
        // close curl resource to free up system resources
94
        // (deletes the variable made by curl_init)
95 2
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

95
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
96
97 2
        return $output;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $output could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
98
    }
99
100
101
    /**
102
     * a json format
103
     *
104
     */
105 1
    public function getGeoLocationJson()
106
    {
107 1
        $encoded = json_encode($this->apiResult, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $options of json_encode(). ( Ignorable by Annotation )

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

107
        $encoded = json_encode($this->apiResult, /** @scrutinizer ignore-type */ true);
Loading history...
108 1
        $pattern1 = "/\"{/i";
109 1
        $first = preg_replace($pattern1, "{", $encoded);
110 1
        $pattern2 = "/[\]]$/i";
111 1
        $second = preg_replace($pattern2, "<br>]", $first);
112 1
        $pattern3 = "/,/i";
113 1
        $third = preg_replace($pattern3, ",<br>********", $second);
114 1
        $pattern4 = "/{/i";
115 1
        $fourth = preg_replace($pattern4, "{<br>********", $third);
116 1
        $pattern5 = "/}/i";
117 1
        $fifth = preg_replace($pattern5, "<br>****}", $fourth);
118 1
        $pattern6 = "/[\\\\]/i";
119 1
        $sixth = preg_replace($pattern6, "", $fifth);
120 1
        $string = str_replace("*", "&nbsp", $sixth);
121
122 1
        return $string;
123
    }
124
125
126
    /**
127
     * Gets the Geo Location info in
128
     * an array
129
     *
130
     */
131 1
    public function getGeoLocationNormal($enteredIp)
132
    {
133 1
        $this->setGeoApi($enteredIp);
134 1
        $output = $this->doCurl($this->geoApi);
135
136 1
        $apiResult = json_decode($output, true);
137 1
        $this->apiResult = $apiResult;
138 1
        return $this->apiResult;
139
    }
140
}
141