1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ModuleRatings; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
|
8
|
|
|
abstract class Check |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The number of points given when this check is successful. This is normally loaded in via configuration. |
12
|
|
|
* |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
protected $points = 0; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Whether this check was successful when it was run |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $successful = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The controlling class responsible for running all checks |
26
|
|
|
* |
27
|
|
|
* @var CheckSuite |
28
|
|
|
*/ |
29
|
|
|
protected $suite; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Utility class used for making HTTP requests |
33
|
|
|
* |
34
|
|
|
* @var ClientInterface |
35
|
|
|
*/ |
36
|
|
|
protected $requestClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ClientInterface $requestFactory |
40
|
|
|
*/ |
41
|
|
|
public function __construct(ClientInterface $requestClient = null) |
42
|
|
|
{ |
43
|
|
|
if (!$requestClient) { |
44
|
|
|
$requestClient = new Client; |
45
|
|
|
} |
46
|
|
|
$this->setRequestClient($requestClient); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the check "key", which is used for referencing this check in code |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
abstract public function getKey(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the check description, which is used for humans |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
abstract public function getDescription(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Run the check logic, set the successful result at the end |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
abstract public function run(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the resulting number of points that has been assigned to the module for this check, depending |
72
|
|
|
* on whether it was successful or not. |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public function getResult() |
77
|
|
|
{ |
78
|
|
|
if (!$this->getSuccessful()) { |
79
|
|
|
return 0; |
80
|
|
|
} |
81
|
|
|
return $this->getPoints(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param CheckSuite $suite |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setSuite(CheckSuite $suite) |
89
|
|
|
{ |
90
|
|
|
$this->suite = $suite; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return CheckSuite |
96
|
|
|
*/ |
97
|
|
|
public function getSuite() |
98
|
|
|
{ |
99
|
|
|
return $this->suite; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the number of points that a successful check gives |
104
|
|
|
* @param int $points |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function setPoints($points) |
108
|
|
|
{ |
109
|
|
|
$this->points = (int) $points; |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* The number of points that a successful check gives |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public function getPoints() |
119
|
|
|
{ |
120
|
|
|
return $this->points; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set whether the check successfully passed |
125
|
|
|
* |
126
|
|
|
* @param bool $result |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function setSuccessful($result) |
130
|
|
|
{ |
131
|
|
|
$this->successful = (bool) $result; |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Whether the check successfully passed |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function getSuccessful() |
140
|
|
|
{ |
141
|
|
|
return $this->successful; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the HTTP request client |
146
|
|
|
* |
147
|
|
|
* @param ClientInterface $client |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setRequestClient(ClientInterface $client) |
151
|
|
|
{ |
152
|
|
|
$this->requestClient = $client; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the HTTP request client |
158
|
|
|
* |
159
|
|
|
* @return ClientInterface |
160
|
|
|
*/ |
161
|
|
|
public function getRequestClient() |
162
|
|
|
{ |
163
|
|
|
return $this->requestClient; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|