Conditions | 8 |
Paths | 1244 |
Total Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
31 | public function execute() |
||
32 | { |
||
33 | $database = $this->getDatabase(); |
||
34 | |||
35 | // ensure we're running inside a tx here. |
||
36 | if (!$database->hasActiveTransaction()) { |
||
37 | $database->beginTransaction(); |
||
38 | } |
||
39 | |||
40 | $sql = 'SELECT * FROM jobqueue WHERE status = :status ORDER BY enqueue LIMIT :lim'; |
||
41 | $statement = $database->prepare($sql); |
||
42 | $statement->execute(array(':status' => JobQueue::STATUS_READY, ':lim' => 10)); |
||
43 | /** @var JobQueue[] $queuedJobs */ |
||
44 | $queuedJobs = $statement->fetchAll(PDO::FETCH_CLASS, JobQueue::class); |
||
45 | |||
46 | // mark all the jobs as running, and commit the txn so we're not holding onto long-running transactions. |
||
47 | // We'll re-lock the row when we get to it. |
||
48 | foreach ($queuedJobs as $job) { |
||
49 | $job->setDatabase($database); |
||
50 | $job->setStatus(JobQueue::STATUS_WAITING); |
||
51 | $job->setError(null); |
||
52 | $job->setAcknowledged(null); |
||
53 | $job->save(); |
||
54 | } |
||
55 | |||
56 | $database->commit(); |
||
57 | |||
58 | set_error_handler(array(RunJobQueueTask::class, 'errorHandler'), E_ALL); |
||
59 | |||
60 | foreach ($queuedJobs as $job) { |
||
61 | try { |
||
62 | $database->beginTransaction(); |
||
63 | |||
64 | // re-lock the job |
||
65 | $job->setStatus(JobQueue::STATUS_RUNNING); |
||
66 | $job->save(); |
||
67 | |||
68 | // validate we're allowed to run the requested task (whitelist) |
||
69 | if (!in_array($job->getTask(), $this->taskList)) { |
||
70 | throw new ApplicationLogicException('Job task not registered'); |
||
71 | } |
||
72 | |||
73 | // Create a task. |
||
74 | $taskName = $job->getTask(); |
||
75 | |||
76 | if(!class_exists($taskName)) { |
||
77 | throw new ApplicationLogicException('Job task does not exist'); |
||
78 | } |
||
79 | |||
80 | /** @var BackgroundTaskBase $task */ |
||
81 | $task = new $taskName; |
||
82 | |||
83 | $this->setupTask($task, $job); |
||
84 | $task->run(); |
||
85 | } |
||
86 | catch (Exception $ex) { |
||
87 | $database->rollBack(); |
||
88 | $database->beginTransaction(); |
||
89 | |||
90 | /** @var JobQueue $job */ |
||
91 | $job = JobQueue::getById($job->getId(), $database); |
||
92 | $job->setDatabase($database); |
||
93 | $job->setStatus(JobQueue::STATUS_FAILED); |
||
94 | $job->setError($ex->getMessage()); |
||
95 | $job->setAcknowledged(0); |
||
96 | $job->save(); |
||
97 | |||
98 | /** @var Request $request */ |
||
99 | $request = Request::getById($job->getRequest(), $database); |
||
100 | if ($request === false) { |
||
101 | $request = null; |
||
102 | } |
||
103 | |||
104 | Logger::backgroundJobIssue($this->getDatabase(), $job); |
||
105 | |||
106 | $database->commit(); |
||
107 | } |
||
108 | finally { |
||
109 | $database->commit(); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
132 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.