| Conditions | 1 |
| Paths | 1 |
| Total Lines | 92 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php /** @noinspection SqlConstantCondition */ |
||
| 17 | public function execute() |
||
| 18 | { |
||
| 19 | $database = $this->getDatabase(); |
||
| 20 | $expiryTime = [':expiry' => $this->getSiteConfiguration()->getEmailConfirmationExpiryDays()]; |
||
| 21 | |||
| 22 | // start by fetching the number of unconfirmed requests which have expired |
||
| 23 | $eligibleRecords = $this->getExpiredCount($database, $expiryTime); |
||
| 24 | |||
| 25 | // fetch the number of unconfirmed requests which have expired and which have no FK constraints which would |
||
| 26 | // otherwise prevent their deletion |
||
| 27 | $eligibleUnconstrainedRecords = $this->getExpiredUnconstrainedCount($database, $expiryTime); |
||
| 28 | |||
| 29 | // Delete any requester comments for expired requests |
||
| 30 | $requesterCommentDelete = <<<SQL |
||
| 31 | DELETE FROM comment |
||
| 32 | WHERE 1 = 1 |
||
| 33 | -- only requester comments |
||
| 34 | AND comment.visibility = 'requester' |
||
| 35 | -- where the following record exists |
||
| 36 | AND exists( |
||
| 37 | SELECT 1 FROM request r |
||
| 38 | WHERE 1 = 1 |
||
| 39 | -- a request matching the currently-checked comment |
||
| 40 | AND comment.request = r.id |
||
| 41 | -- and the request is expired |
||
| 42 | AND r.date < DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL :expiry DAY) |
||
| 43 | -- no confirmed email address |
||
| 44 | AND r.emailconfirm <> 'Confirmed' |
||
| 45 | -- not already marked as stale |
||
| 46 | AND r.emailconfirm <> 'Stale' |
||
| 47 | -- email confirmation was requested |
||
| 48 | AND r.emailconfirm <> '' |
||
| 49 | -- no non-requester comments exist (nobody has commented on the request) |
||
| 50 | AND NOT exists (SELECT 1 FROM comment c2 WHERE c2.request = r.id AND c2.visibility <> 'requester') |
||
| 51 | -- no jobqueue entries for this request exist |
||
| 52 | AND NOT exists (SELECT 1 FROM jobqueue j WHERE j.request = r.id) |
||
| 53 | -- no log entries for this request exist |
||
| 54 | AND NOT exists (SELECT 1 FROM log l WHERE l.objectid = r.id and l.objecttype = 'Request') |
||
| 55 | ); |
||
| 56 | SQL; |
||
| 57 | $statement = $database->prepare($requesterCommentDelete); |
||
| 58 | $statement->execute($expiryTime); |
||
| 59 | $deletedComments = $statement->rowCount(); |
||
| 60 | |||
| 61 | // Delete any expired requests with no remaining FK constraints |
||
| 62 | $requestDelete = <<<SQL |
||
| 63 | DELETE FROM request |
||
| 64 | WHERE 1 = 1 |
||
| 65 | -- request date older than X days ago |
||
| 66 | AND request.date < DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL :expiry DAY) |
||
| 67 | -- no confirmed email address |
||
| 68 | AND request.emailconfirm <> 'Confirmed' |
||
| 69 | -- not already marked as stale |
||
| 70 | AND request.emailconfirm <> 'Stale' |
||
| 71 | -- email confirmation was requested |
||
| 72 | AND request.emailconfirm <> '' |
||
| 73 | -- no comments exist (we just deleted requester comments) |
||
| 74 | AND NOT exists(SELECT 1 FROM comment c WHERE c.request = request.id) |
||
| 75 | -- no jobqueue entries for this request exist |
||
| 76 | AND NOT exists(SELECT 1 FROM jobqueue j WHERE j.request = request.id) |
||
| 77 | -- no log entries for this request exist |
||
| 78 | AND NOT exists(SELECT 1 FROM log l WHERE l.objectid = request.id and l.objecttype = 'Request'); |
||
| 79 | SQL; |
||
| 80 | $statement = $database->prepare($requestDelete); |
||
| 81 | $statement->execute($expiryTime); |
||
| 82 | $deletedRequests = $statement->rowCount(); |
||
| 83 | |||
| 84 | // We've deleted all we can sensibly get away with. Disable the ability to email-confirm requests, and close |
||
| 85 | // them as stale. The purge job will pick up the clearing of any private data. |
||
| 86 | // Note - *very* few requests should get this far; it normally means a tool admin has overridden the |
||
| 87 | // email-confirmation lockout and done something to the non-confirmed request. |
||
| 88 | |||
| 89 | $splatExpired = <<<SQL |
||
| 90 | UPDATE request |
||
| 91 | SET emailconfirm = 'Stale', status = 'Closed', updateversion = updateversion + 1 |
||
| 92 | WHERE 1 = 1 |
||
| 93 | AND request.date < DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL :expiry DAY) |
||
| 94 | AND request.emailconfirm <> 'Confirmed' |
||
| 95 | AND request.emailconfirm <> 'Stale' |
||
| 96 | AND request.emailconfirm <> '' |
||
| 97 | ; |
||
| 98 | SQL; |
||
| 99 | |||
| 100 | $statement = $database->prepare($splatExpired); |
||
| 101 | $statement->execute($expiryTime); |
||
| 102 | $requestsMarkedStale = $statement->rowCount(); |
||
| 103 | |||
| 104 | // All done. |
||
| 105 | $database->commit(); |
||
| 106 | |||
| 107 | printf('Cleanup: %d expired; %d unconstrained, %d comments deleted, %d requests deleted, %d marked stale', |
||
| 108 | $eligibleRecords, $eligibleUnconstrainedRecords, $deletedComments, $deletedRequests, $requestsMarkedStale); |
||
| 109 | } |
||
| 162 | } |