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.

StatusCakeSkeleton   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 190
Duplicated Lines 11.58 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 28
lcom 1
cbo 3
dl 22
loc 190
rs 10
c 6
b 2
f 0

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 getTestArray() 0 20 4
A preBackup() 0 3 1
A postBackup() 0 3 1
A preRemove() 0 3 1
A dependsOn() 0 4 1
A getTestId() 0 11 2
A createTest() 0 5 1
A updateTest() 0 5 1
A deleteTest() 0 5 1
B performAPICall() 0 24 3
A postRemove() 9 9 3

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
4
/**
5
 * StatusCakeSkeleton
6
 */
7
class StatusCakeSkeleton extends AbstractSkeleton
8
{
9
10
    const NAME = "statuscake";
11
12
    /**
13
     * @return string
14
     */
15
    public function getName()
16
    {
17
        return self::NAME;
18
    }
19
20
    /**
21
     * @param \ArrayObject $project
22
     *
23
     * @return mixed
24
     */
25
    public function create(\ArrayObject $project)
26
    {
27
    }
28
29
    /**
30
     * @return mixed
31
     */
32
    public function preMaintenance()
33
    {
34
    }
35
36
    /**
37
     * @return mixed
38
     */
39
    public function postMaintenance()
40
    {
41
    }
42
43
    /**
44
     * @param \ArrayObject $project
45
     *
46
     * @return mixed
47
     */
48 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...
49
    {
50
        if (isset($this->app["config"]["statuscake"]["enabled"]) && $this->app["config"]["statuscake"]["enabled"]){
51
            $id = $this->getTestId($project);
52
            if ($id) {
53
                $this->updateTest($id, $project);
54
            } else {
55
                $this->createTest($project);
56
            }
57
        } else {
58
            $this->dialogProvider->logConfig("Statuscake is disabled");
59
        }
60
    }
61
62
    private function getCheckName(\ArrayObject $project){
63
        return $project["name"] . "." . str_replace(".kunstmaan.com", "", $this->app["config"]["webserver"]["hostmachine"]);
64
    }
65
66
    private function getTestId(\ArrayObject $project)
67
    {
68
        $tests = $this->performAPICall("Tests/", "GET");
69
        $found = false;
70
        array_walk($tests, function ($item, $key) use (&$found, $project) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
            if ($item["WebsiteName"] == $this->getCheckName($project)) {
72
                $found = $item["TestID"];
73
            }
74
        });
75
        return $found;
76
    }
77
78
    private function createTest(\ArrayObject $project)
79
    {
80
        $data = $this->getTestArray($project);
81
        $this->performAPICall("Tests/Update", "PUT", $data);
82
    }
83
84
    private function updateTest($id, \ArrayObject $project)
85
    {
86
        $data = $this->getTestArray($project, $id);
87
        $this->performAPICall("Tests/Update", "PUT", $data);
88
    }
89
90
    private function deleteTest($id)
91
    {
92
        $data = $this->getTestArray(null, $id);
93
        $this->performAPICall("Tests/Details/", "DELETE", $data);
94
    }
95
96
    /**
97
     * @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...
98
     * @return array
99
     */
100
    private function getTestArray(\ArrayObject $project = null, $id = false)
101
    {
102
        $data = array();
103
        if (!is_null($project)) {
104
            $data = array(
105
                "WebsiteName" => $this->getCheckName($project),
106
                "Paused" => 0,
107
                "WebsiteURL" => ($this->skeletonProvider->hasSkeleton($project, $this->skeletonProvider->findSkeleton("ssl"))?"https://":"http://") . $project["url"],
108
                "CheckRate" => "60",
109
                "TestType" => "HTTP",
110
                "WebsiteHost" => $this->app["config"]["webserver"]["hostmachine"],
111
                "ContactGroup" => 22636,
112
                "TestTags" => $this->app["config"]["webserver"]["hostmachine"]
113
            );
114
        }
115
        if ($id) {
116
            $data["TestID"] = $id;
117
        }
118
        return $data;
119
    }
120
121
    private function performAPICall($url, $method, $data = null)
122
    {
123
124
        // The data to insert
125
        $API = $this->app["config"]["statuscake"]["apikey"];
126
        $Username = $this->app["config"]["statuscake"]["username"];
127
128
        // Create the CURL String
129
        $ch = curl_init("https://www.statuscake.com/API/" . $url);
130
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
131
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
132
        if ($data) {
133
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
134
        }
135
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
136
            "API: " . $API,
137
            "Username: " . $Username
138
        ));
139
140
        $response = curl_exec($ch);
141
        $response = json_decode($response, true);
142
        $this->dialogProvider->logConfig("Sent a " . $method . " call to " . "https://www.statuscake.com/API/" . $url . " and got a response: " . ($method == "GET"?"with " . sizeof($response) . " checks":$response["Message"]));
143
        return $response;
144
    }
145
146
    /**
147
     * @param \ArrayObject $project
148
     *
149
     * @return mixed
150
     */
151
    public function preBackup(\ArrayObject $project)
152
    {
153
    }
154
155
    /**
156
     * @param \ArrayObject $project
157
     *
158
     * @return mixed
159
     */
160
    public function postBackup(\ArrayObject $project)
161
    {
162
    }
163
164
    /**
165
     * @param \ArrayObject $project
166
     *
167
     * @return mixed
168
     */
169
    public function preRemove(\ArrayObject $project)
170
    {
171
    }
172
173
    /**
174
     * @param \ArrayObject $project
175
     *
176
     * @return mixed
177
     */
178 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...
179
    {
180
        if (isset($this->app["config"]["statuscake"]["enabled"]) && $this->app["config"]["statuscake"]["enabled"]) {
181
            $id = $this->getTestId($project["name"]);
182
            $this->deleteTest($id);
183
        } else {
184
            $this->dialogProvider->logConfig("Statuscake is disabled");
185
        }
186
    }
187
188
    /**
189
     * @return string[]
190
     */
191
    public function dependsOn()
192
    {
193
        return array("base", "apache");
194
    }
195
196
}
197