| Conditions | 10 |
| Paths | 19 |
| Total Lines | 96 |
| Code Lines | 61 |
| 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 |
||
| 44 | public function expireJobsAction() |
||
| 45 | { |
||
| 46 | $repositories = $this->repositories; |
||
| 47 | /* @var \Jobs\Repository\Job $jobsRepo */ |
||
| 48 | $jobsRepo = $repositories->get('Jobs/Job'); |
||
| 49 | $days = (int) $this->params('days'); |
||
| 50 | $limit = (string) $this->params('limit'); |
||
| 51 | $info = $this->params('info'); |
||
| 52 | |||
| 53 | if (!$days) { |
||
| 54 | return 'Invalid value for --days. Must be integer.'; |
||
| 55 | } |
||
| 56 | |||
| 57 | $date = new \DateTime('today'); |
||
| 58 | $date->sub(new \DateInterval('P' . $days . 'D')); |
||
| 59 | |||
| 60 | $query = [ |
||
| 61 | '$and' => [ |
||
| 62 | ['status.name' => StatusInterface::ACTIVE], |
||
| 63 | ['$or' => [ |
||
| 64 | ['datePublishStart.date' => ['$lt' => $date]], |
||
| 65 | ['datePublishEnd.date' => ['$lt' => new \DateTime('today midnight')]], |
||
| 66 | ]], |
||
| 67 | ['$or' => [ |
||
| 68 | ['isDeleted' => ['$exists' => false]], |
||
| 69 | ['isDeleted' => false], |
||
| 70 | ]] |
||
| 71 | ] |
||
| 72 | ]; |
||
| 73 | |||
| 74 | $offset = 0; |
||
| 75 | if ($limit && false !== strpos($limit, ',')) { |
||
| 76 | list($limit,$offset) = explode(',', $limit); |
||
| 77 | } |
||
| 78 | |||
| 79 | $jobs = $jobsRepo->findBy($query, null, (int) $limit, (int) $offset); |
||
| 80 | $count = count($jobs); |
||
| 81 | |||
| 82 | if (0 === $count) { |
||
| 83 | return 'No jobs found.'; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($info) { |
||
| 87 | echo count($jobs) , ' Jobs'; |
||
| 88 | if ($offset) { echo ' starting from ' . $offset; } |
||
| 89 | echo PHP_EOL . PHP_EOL; |
||
| 90 | $this->listExpiredJobs($jobs); |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | foreach ($repositories->getEventManager()->getListeners('preUpdate') as $listener) { |
||
| 95 | $repositories->getEventManager()->removeEventListener('preUpdate', $listener); |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | echo "$count jobs found, which have to expire ...\n"; |
||
| 100 | |||
| 101 | $progress = new ProgressBar( |
||
| 102 | new ConsoleAdapter( |
||
| 103 | array( |
||
| 104 | 'elements' => array( |
||
| 105 | ConsoleAdapter::ELEMENT_TEXT, |
||
| 106 | ConsoleAdapter::ELEMENT_BAR, |
||
| 107 | ConsoleAdapter::ELEMENT_PERCENT, |
||
| 108 | ConsoleAdapter::ELEMENT_ETA |
||
| 109 | ), |
||
| 110 | 'textWidth' => 20, |
||
| 111 | 'barLeftChar' => '-', |
||
| 112 | 'barRightChar' => ' ', |
||
| 113 | 'barIndicatorChar' => '>', |
||
| 114 | ) |
||
| 115 | ), |
||
| 116 | 0, |
||
| 117 | count($jobs) |
||
| 118 | ); |
||
| 119 | |||
| 120 | $i = 0; |
||
| 121 | |||
| 122 | /* @var \Jobs\Entity\Job $job */ |
||
| 123 | foreach ($jobs as $job) { |
||
| 124 | $progress->update($i++, 'Job ' . $i . ' / ' . $count); |
||
| 125 | |||
| 126 | $job->changeStatus('expired'); |
||
| 127 | |||
| 128 | if (0 == $i % 500) { |
||
| 129 | $progress->update($i, 'Write to database...'); |
||
| 130 | $repositories->flush(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $progress->update($i, 'Write to database...'); |
||
| 134 | $repositories->flush(); |
||
| 135 | $progress->update($i, 'Done'); |
||
| 136 | $progress->finish(); |
||
| 137 | |||
| 138 | return PHP_EOL; |
||
| 139 | } |
||
| 140 | |||
| 210 |