enwikipedia-acc /
waca
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /****************************************************************************** |
||
| 3 | * Wikipedia Account Creation Assistance tool * |
||
| 4 | * * |
||
| 5 | * All code in this file is released into the public domain by the ACC * |
||
| 6 | * Development Team. Please see team.json for a list of contributors. * |
||
| 7 | ******************************************************************************/ |
||
| 8 | |||
| 9 | namespace Waca\Background; |
||
| 10 | |||
| 11 | use Waca\DataObjects\EmailTemplate; |
||
| 12 | use Waca\DataObjects\JobQueue; |
||
| 13 | use Waca\DataObjects\Request; |
||
| 14 | use Waca\DataObjects\User; |
||
| 15 | use Waca\Exceptions\ApplicationLogicException; |
||
| 16 | use Waca\Helpers\HttpHelper; |
||
| 17 | use Waca\Helpers\Interfaces\IEmailHelper; |
||
| 18 | use Waca\Helpers\Interfaces\IOAuthProtocolHelper; |
||
| 19 | use Waca\Helpers\IrcNotificationHelper; |
||
| 20 | use Waca\Helpers\Logger; |
||
| 21 | use Waca\PdoDatabase; |
||
| 22 | use Waca\SiteConfiguration; |
||
| 23 | |||
| 24 | abstract class BackgroundTaskBase |
||
| 25 | { |
||
| 26 | /** @var JobQueue */ |
||
| 27 | private $job; |
||
| 28 | /** @var PdoDatabase */ |
||
| 29 | private $database; |
||
| 30 | /** @var IOAuthProtocolHelper */ |
||
| 31 | private $oauthProtocolHelper; |
||
| 32 | /** @var SiteConfiguration */ |
||
| 33 | private $siteConfiguration; |
||
| 34 | /** @var IEmailHelper */ |
||
| 35 | private $emailHelper; |
||
| 36 | /** @var HttpHelper */ |
||
| 37 | private $httpHelper; |
||
| 38 | /** @var IrcNotificationHelper */ |
||
| 39 | private $notificationHelper; |
||
| 40 | /** @var User */ |
||
| 41 | private $triggerUser; |
||
| 42 | /** @var Request */ |
||
| 43 | private $request; |
||
| 44 | /** @var EmailTemplate */ |
||
| 45 | private $emailTemplate = null; |
||
| 46 | /** @var mixed */ |
||
| 47 | private $parameters; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return JobQueue |
||
| 51 | */ |
||
| 52 | public function getJob() |
||
| 53 | { |
||
| 54 | return $this->job; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param JobQueue $job |
||
| 59 | */ |
||
| 60 | public function setJob(JobQueue $job) |
||
| 61 | { |
||
| 62 | $this->job = $job; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return PdoDatabase |
||
| 67 | */ |
||
| 68 | public function getDatabase() |
||
| 69 | { |
||
| 70 | return $this->database; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param PdoDatabase $database |
||
| 75 | */ |
||
| 76 | public function setDatabase(PdoDatabase $database) |
||
| 77 | { |
||
| 78 | $this->database = $database; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return IOAuthProtocolHelper |
||
| 83 | */ |
||
| 84 | public function getOauthProtocolHelper() |
||
| 85 | { |
||
| 86 | return $this->oauthProtocolHelper; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param IOAuthProtocolHelper $oauthProtocolHelper |
||
| 91 | */ |
||
| 92 | public function setOauthProtocolHelper(IOAuthProtocolHelper $oauthProtocolHelper) |
||
| 93 | { |
||
| 94 | $this->oauthProtocolHelper = $oauthProtocolHelper; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return SiteConfiguration |
||
| 99 | */ |
||
| 100 | public function getSiteConfiguration() |
||
| 101 | { |
||
| 102 | return $this->siteConfiguration; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param SiteConfiguration $siteConfiguration |
||
| 107 | */ |
||
| 108 | public function setSiteConfiguration(SiteConfiguration $siteConfiguration) |
||
| 109 | { |
||
| 110 | $this->siteConfiguration = $siteConfiguration; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return HttpHelper |
||
| 115 | */ |
||
| 116 | public function getHttpHelper() |
||
| 117 | { |
||
| 118 | return $this->httpHelper; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param HttpHelper $httpHelper |
||
| 123 | */ |
||
| 124 | public function setHttpHelper(HttpHelper $httpHelper) |
||
| 125 | { |
||
| 126 | $this->httpHelper = $httpHelper; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return IEmailHelper |
||
| 131 | */ |
||
| 132 | public function getEmailHelper() |
||
| 133 | { |
||
| 134 | return $this->emailHelper; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param IEmailHelper $emailHelper |
||
| 139 | */ |
||
| 140 | public function setEmailHelper(IEmailHelper $emailHelper) |
||
| 141 | { |
||
| 142 | $this->emailHelper = $emailHelper; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return IrcNotificationHelper |
||
| 147 | */ |
||
| 148 | public function getNotificationHelper() |
||
| 149 | { |
||
| 150 | return $this->notificationHelper; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param IrcNotificationHelper $notificationHelper |
||
| 155 | */ |
||
| 156 | public function setNotificationHelper($notificationHelper) |
||
| 157 | { |
||
| 158 | $this->notificationHelper = $notificationHelper; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | protected abstract function execute(); |
||
| 165 | |||
| 166 | public function run() |
||
| 167 | { |
||
| 168 | $this->triggerUser = User::getById($this->job->getTriggerUserId(), $this->getDatabase()); |
||
|
0 ignored issues
–
show
|
|||
| 169 | |||
| 170 | if ($this->triggerUser === false) { |
||
| 171 | throw new ApplicationLogicException('Cannot locate trigger user'); |
||
| 172 | } |
||
| 173 | |||
| 174 | $this->request = Request::getById($this->job->getRequest(), $this->getDatabase()); |
||
|
0 ignored issues
–
show
It seems like
\Waca\DataObjects\Reques..., $this->getDatabase()) of type object<Waca\DataObject> or false is incompatible with the declared type object<Waca\DataObjects\Request> of property $request.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 175 | |||
| 176 | if ($this->request === false) { |
||
| 177 | throw new ApplicationLogicException('Cannot locate request'); |
||
| 178 | } |
||
| 179 | |||
| 180 | if($this->job->getEmailTemplate() !== null){ |
||
| 181 | $this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase()); |
||
|
0 ignored issues
–
show
It seems like
\Waca\DataObjects\EmailT..., $this->getDatabase()) of type object<Waca\DataObject> or false is incompatible with the declared type object<Waca\DataObjects\EmailTemplate> of property $emailTemplate.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 182 | |||
| 183 | if ($this->emailTemplate === false) { |
||
| 184 | throw new ApplicationLogicException('Cannot locate email template'); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->parameters = json_decode($this->job->getParameters()); |
||
| 189 | |||
| 190 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
| 191 | throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg()); |
||
| 192 | } |
||
| 193 | |||
| 194 | // Should we wait for a parent job? |
||
| 195 | if($this->job->getParent() !== null) { |
||
| 196 | /** @var JobQueue $parentJob */ |
||
| 197 | $parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase()); |
||
| 198 | |||
| 199 | if($parentJob === false) { |
||
| 200 | $this->markFailed("Parent job could not be found"); |
||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | switch ($parentJob->getStatus()) { |
||
| 205 | case JobQueue::STATUS_CANCELLED: |
||
| 206 | case JobQueue::STATUS_FAILED: |
||
| 207 | $this->markCancelled('Parent job failed/cancelled'); |
||
| 208 | return; |
||
| 209 | case JobQueue::STATUS_WAITING: |
||
| 210 | case JobQueue::STATUS_READY: |
||
| 211 | case JobQueue::STATUS_RUNNING: |
||
| 212 | case JobQueue::STATUS_HELD: |
||
| 213 | // Defer to next execution |
||
| 214 | $this->job->setStatus(JobQueue::STATUS_READY); |
||
| 215 | $this->job->save(); |
||
| 216 | return; |
||
| 217 | case JobQueue::STATUS_COMPLETE: |
||
| 218 | // do nothing |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | $this->execute(); |
||
| 224 | } |
||
| 225 | |||
| 226 | View Code Duplication | protected function markComplete() |
|
|
0 ignored issues
–
show
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...
|
|||
| 227 | { |
||
| 228 | $this->job->setStatus(JobQueue::STATUS_COMPLETE); |
||
| 229 | $this->job->setError(null); |
||
| 230 | $this->job->setAcknowledged(null); |
||
| 231 | $this->job->save(); |
||
| 232 | |||
| 233 | Logger::backgroundJobComplete($this->getDatabase(), $this->getJob()); |
||
| 234 | } |
||
| 235 | |||
| 236 | View Code Duplication | protected function markCancelled($reason = null) |
|
|
0 ignored issues
–
show
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...
|
|||
| 237 | { |
||
| 238 | $this->job->setStatus(JobQueue::STATUS_CANCELLED); |
||
| 239 | $this->job->setError($reason); |
||
| 240 | $this->job->setAcknowledged(null); |
||
| 241 | $this->job->save(); |
||
| 242 | |||
| 243 | Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
||
| 244 | } |
||
| 245 | |||
| 246 | View Code Duplication | protected function markFailed($reason = null) |
|
|
0 ignored issues
–
show
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...
|
|||
| 247 | { |
||
| 248 | $this->job->setStatus(JobQueue::STATUS_FAILED); |
||
| 249 | $this->job->setError($reason); |
||
| 250 | $this->job->setAcknowledged(0); |
||
| 251 | $this->job->save(); |
||
| 252 | |||
| 253 | Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return User |
||
| 258 | */ |
||
| 259 | public function getTriggerUser() |
||
| 260 | { |
||
| 261 | return $this->triggerUser; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return Request |
||
| 266 | */ |
||
| 267 | public function getRequest() |
||
| 268 | { |
||
| 269 | return $this->request; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return EmailTemplate |
||
| 274 | */ |
||
| 275 | public function getEmailTemplate() |
||
| 276 | { |
||
| 277 | return $this->emailTemplate; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | public function getParameters() |
||
| 284 | { |
||
| 285 | return $this->parameters; |
||
| 286 | } |
||
| 287 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.