Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 27 | class ObjectSchedule extends AbstractModel implements ObjectScheduleInterface |
||
|
|
|||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Store the factory instance for the current class. |
||
| 31 | * |
||
| 32 | * @var FactoryInterface |
||
| 33 | */ |
||
| 34 | private $modelFactory; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The object type of the scheduled object (required). |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $targetType; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The object ID of the scheduled object (required). |
||
| 45 | * |
||
| 46 | * @var mixed |
||
| 47 | */ |
||
| 48 | private $targetId; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * When the item should be processed. |
||
| 52 | * |
||
| 53 | * The date/time at which this queue item job should be ran. |
||
| 54 | * If NULL, 0, or a past date/time, then it should be performed immediately. |
||
| 55 | * |
||
| 56 | * @var DateTimeInterface $scheduledDate |
||
| 57 | */ |
||
| 58 | private $scheduledDate; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The property identifier of the scheduled object (required). |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $dataDiff = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Whether the item has been processed. |
||
| 69 | * |
||
| 70 | * @var boolean $processed |
||
| 71 | */ |
||
| 72 | private $processed = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * When the item was processed. |
||
| 76 | * |
||
| 77 | * @var DateTimeInterface $processedDate |
||
| 78 | */ |
||
| 79 | private $processedDate; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set an object model factory. |
||
| 83 | * |
||
| 84 | * @param FactoryInterface $factory The model factory, to create objects. |
||
| 85 | * @return ObjectScheduleInterface Chainable |
||
| 86 | */ |
||
| 87 | public function setModelFactory(FactoryInterface $factory) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Retrieve the object model factory. |
||
| 96 | * |
||
| 97 | * @throws RuntimeException If the model factory was not previously set. |
||
| 98 | * @return FactoryInterface |
||
| 99 | */ |
||
| 100 | protected function modelFactory() |
||
| 101 | { |
||
| 102 | if (!isset($this->modelFactory)) { |
||
| 103 | throw new RuntimeException(sprintf( |
||
| 104 | 'Model Factory is not defined for "%s"', |
||
| 105 | get_class($this) |
||
| 106 | )); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this->modelFactory; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set the scheduled object's type. |
||
| 114 | * |
||
| 115 | * @param string $targetType The object type (model). |
||
| 116 | * @throws InvalidArgumentException If the object type parameter is not a string. |
||
| 117 | * @return ObjectScheduleInterface Chainable |
||
| 118 | */ |
||
| 119 | public function setTargetType($targetType) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Retrieve the scheduled object's type. |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function getTargetType() |
||
| 138 | { |
||
| 139 | return $this->targetType; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set the scheduled object's ID. |
||
| 144 | * |
||
| 145 | * @param mixed $targetId The object ID. |
||
| 146 | * @return ObjectScheduleInterface Chainable |
||
| 147 | */ |
||
| 148 | public function setTargetId($targetId) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Retrieve the scheduled object's ID. |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | public function getTargetId() |
||
| 161 | { |
||
| 162 | return $this->targetId; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param array|string $data The data diff. |
||
| 167 | * @return ObjectRevision |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function setDataDiff($data) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getDataDiff() |
||
| 185 | { |
||
| 186 | return $this->dataDiff; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the schedule's processed status. |
||
| 191 | * |
||
| 192 | * @param boolean $processed Whether the schedule has been processed. |
||
| 193 | * @return ObjectScheduleInterface Chainable |
||
| 194 | */ |
||
| 195 | public function setProcessed($processed) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Determine if the schedule has been processed. |
||
| 204 | * |
||
| 205 | * @return boolean |
||
| 206 | */ |
||
| 207 | public function getProcessed() |
||
| 208 | { |
||
| 209 | return $this->processed; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set the date/time the item should be processed at. |
||
| 214 | * |
||
| 215 | * @param null|string|DateTimeInterface $ts A date/time string or object. |
||
| 216 | * @throws InvalidArgumentException If the date/time is invalid. |
||
| 217 | * @return ObjectScheduleInterface Chainable |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function setScheduledDate($ts) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Retrieve the date/time the item should be processed at. |
||
| 251 | * |
||
| 252 | * @return null|DateTimeInterface |
||
| 253 | */ |
||
| 254 | public function getScheduledDate() |
||
| 255 | { |
||
| 256 | return $this->scheduledDate; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Set the date/time the item was processed at. |
||
| 261 | * |
||
| 262 | * @param null|string|DateTimeInterface $ts A date/time string or object. |
||
| 263 | * @throws InvalidArgumentException If the date/time is invalid. |
||
| 264 | * @return ObjectScheduleInterface Chainable |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function setProcessedDate($ts) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Retrieve the date/time the item was processed at. |
||
| 298 | * |
||
| 299 | * @return null|DateTimeInterface |
||
| 300 | */ |
||
| 301 | public function getProcessedDate() |
||
| 302 | { |
||
| 303 | return $this->processedDate; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Hook called before saving the item. |
||
| 308 | * |
||
| 309 | * Presets the item as _to-be_ processed and queued now. |
||
| 310 | * |
||
| 311 | * @return boolean |
||
| 312 | */ |
||
| 313 | protected function preSave() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Process the item. |
||
| 324 | * |
||
| 325 | * @param callable $callback An optional callback routine executed after the item is processed. |
||
| 326 | * @param callable $successCallback An optional callback routine executed when the item is resolved. |
||
| 327 | * @param callable $failureCallback An optional callback routine executed when the item is rejected. |
||
| 328 | * @return boolean|null Success / Failure, or null in case of a skipped item. |
||
| 329 | */ |
||
| 330 | public function process( |
||
| 391 | } |
||
| 392 |