GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MonitoringSkeleton   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 196
Duplicated Lines 11.22 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 31
lcom 1
cbo 4
dl 22
loc 196
rs 9.8
c 4
b 0
f 2

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A create() 0 3 1
A preMaintenance() 0 3 1
A postMaintenance() 0 3 1
A maintenance() 13 13 4
A getCheckName() 0 3 1
A getTestId() 0 9 2
A createTest() 0 5 1
A updateTest() 0 5 1
A deleteTest() 0 5 1
C getTestArray() 0 34 8
A performAPICall() 0 18 2
A preBackup() 0 3 1
A postBackup() 0 3 1
A preRemove() 0 3 1
A postRemove() 9 9 3
A dependsOn() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Kunstmaan\Skylab\Skeleton;
3
use UptimeRobot\API;
4
5
/**
6
 * MonitoringSkeleton
7
 */
8
class MonitoringSkeleton extends AbstractSkeleton
9
{
10
11
    const NAME = "monitoring";
12
13
    /**
14
     * @return string
15
     */
16
    public function getName()
17
    {
18
        return self::NAME;
19
    }
20
21
    /**
22
     * @param \ArrayObject $project
23
     *
24
     * @return mixed
25
     */
26
    public function create(\ArrayObject $project)
27
    {
28
    }
29
30
    /**
31
     * @return mixed
32
     */
33
    public function preMaintenance()
34
    {
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    public function postMaintenance()
41
    {
42
    }
43
44
    /**
45
     * @param \ArrayObject $project
46
     *
47
     * @return mixed
48
     */
49 View Code Duplication
    public function maintenance(\ArrayObject $project)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (isset($this->app["config"]["monitoring"]["enabled"]) && $this->app["config"]["monitoring"]["enabled"]){
52
            $id = $this->getTestId($project);
53
            if ($id) {
54
                $this->updateTest($id, $project);
55
            } else {
56
                $this->createTest($project);
57
            }
58
        } else {
59
            $this->dialogProvider->logConfig("Monitoring is disabled");
60
        }
61
    }
62
63
    private function getCheckName(\ArrayObject $project){
64
        return $project["name"] . " @ " . str_replace(".kunstmaan.com", "", $this->app["config"]["webserver"]["hostmachine"]);
65
    }
66
67
    private function getTestId(\ArrayObject $project)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69
        $tests = $this->performAPICall("/getMonitors", array("search" => $this->getCheckName($project)));
70
        if ($tests["stat"] == "fail"){
71
            return false;
72
        } else {
73
            return $tests["monitors"]["monitor"][0]["id"];
74
        }
75
    }
76
77
    private function createTest(\ArrayObject $project)
78
    {
79
        $data = $this->getTestArray($project);
80
        $this->performAPICall("/newMonitor", $data);
81
    }
82
83
    private function updateTest($id, \ArrayObject $project)
84
    {
85
        $data = $this->getTestArray($project, $id);
86
        $this->performAPICall("/editMonitor", $data);
87
    }
88
89
    private function deleteTest($id)
90
    {
91
        $data = $this->getTestArray(null, $id);
92
        $this->performAPICall("/deleteMonitor", $data);
93
    }
94
95
    /**
96
     * @param \ArrayObject $project
0 ignored issues
show
Documentation introduced by
Should the type for parameter $project not be null|\ArrayObject?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
97
     * @return array
98
     */
99
    private function getTestArray(\ArrayObject $project = null, $id = false)
100
    {
101
        $data = array();
102
        if (!is_null($project)) {
103
104
            $monitorPath = '';
105
            if (isset($project['monuri'])) { //for backwards compatibility only
106
                $monitorPath = $project['monuri'];
107
            }
108
109
            if (isset($project['monitoring_path'])) {
110
                $monitorPath = $project['monitoring_path'];
111
            }
112
113
            $monitorUrl = ($this->skeletonProvider->hasSkeleton($project, $this->skeletonProvider->findSkeleton("ssl"))?"https://":"http://") . $project["url"] . $monitorPath;
114
            if (filter_var($monitorUrl, FILTER_VALIDATE_URL, array('flags' => array(FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED))) === false) {
115
                //fallback to basic url
116
                $this->dialogProvider->logWarning("The ".$project["name"]." monitoring url is not valid: " . $monitorUrl);
117
                $monitorUrl = ($this->skeletonProvider->hasSkeleton($project, $this->skeletonProvider->findSkeleton("ssl"))?"https://":"http://") . $project["url"];
118
            }
119
120
            $data = array(
121
                "monitorFriendlyName" => $this->getCheckName($project),
122
                "monitorURL" => $monitorUrl,
123
                "monitorType" => 1,
124
                "monitorAlertContacts" => "2351398_5_60-0254691_5_60-2356728_5_60-2356781_5_60",
125
                "monitorInterval" => 1
126
            );
127
        }
128
        if ($id) {
129
            $data["monitorID"] = $id;
130
        }
131
        return $data;
132
    }
133
134
    private function performAPICall($url, $data = array())
135
    {
136
137
        //Set configuration settings
138
        $config = [
139
            'apiKey' => $this->app["config"]["monitoring"]["apikey"],
140
            'url' => 'http://api.uptimerobot.com'
141
        ];
142
143
        try {
144
            $api = new API($config);
145
            $results = $api->request($url, $data);
146
            $this->dialogProvider->logConfig("Sent a call to " . $url . " and got a response: " . print_r($results, true));
147
            return $results;
148
        } catch (\Exception $e) {
149
            $this->dialogProvider->logConfig("Sent a call to " . $url . " and got an error: " . $e->getMessage());
150
        }
151
    }
152
153
    /**
154
     * @param \ArrayObject $project
155
     *
156
     * @return mixed
157
     */
158
    public function preBackup(\ArrayObject $project)
159
    {
160
    }
161
162
    /**
163
     * @param \ArrayObject $project
164
     *
165
     * @return mixed
166
     */
167
    public function postBackup(\ArrayObject $project)
168
    {
169
    }
170
171
    /**
172
     * @param \ArrayObject $project
173
     *
174
     * @return mixed
175
     */
176
    public function preRemove(\ArrayObject $project)
177
    {
178
    }
179
180
    /**
181
     * @param \ArrayObject $project
182
     *
183
     * @return mixed
184
     */
185 View Code Duplication
    public function postRemove(\ArrayObject $project)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187
        if (isset($this->app["config"]["monitoring"]["enabled"]) && $this->app["config"]["monitoring"]["enabled"]){
188
            $id = $this->getTestId($project);
189
            $this->deleteTest($id);
190
        } else {
191
            $this->dialogProvider->logConfig("Monitoring is disabled");
192
        }
193
    }
194
195
    /**
196
     * @return string[]
197
     */
198
    public function dependsOn()
199
    {
200
        return array("base", "apache");
201
    }
202
203
}
204