| Total Complexity | 50 |
| Total Lines | 234 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like CronLock often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CronLock, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CronLock |
||
| 18 | { |
||
| 19 | private string $lockDirectory; |
||
| 20 | private string $taskName; |
||
| 21 | private string $lockFile; |
||
| 22 | |||
| 23 | /** @var resource|null */ |
||
| 24 | private $lockHandle = null; |
||
| 25 | |||
| 26 | private bool $ownsLock = false; |
||
| 27 | private int $maxLockAge; |
||
| 28 | |||
| 29 | private const DEFAULT_MAX_LOCK_AGE = 86400; |
||
| 30 | |||
| 31 | public function __construct(string $taskName, ?string $lockDirectory = null, ?int $maxLockAge = null) |
||
| 40 | } |
||
| 41 | |||
| 42 | public function acquire(): bool |
||
| 43 | { |
||
| 44 | $this->lockHandle = fopen($this->lockFile, 'c+'); |
||
| 45 | if ($this->lockHandle === false) { |
||
| 46 | $this->lockHandle = null; |
||
| 47 | $this->ownsLock = false; |
||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!flock($this->lockHandle, LOCK_EX | LOCK_NB)) { |
||
| 52 | fclose($this->lockHandle); |
||
| 53 | $this->lockHandle = null; |
||
| 54 | $this->ownsLock = false; |
||
| 55 | return false; |
||
| 56 | } |
||
| 57 | |||
| 58 | if (!$this->writeTimestampToHandle($this->lockHandle)) { |
||
| 59 | flock($this->lockHandle, LOCK_UN); |
||
| 60 | fclose($this->lockHandle); |
||
| 61 | $this->lockHandle = null; |
||
| 62 | $this->ownsLock = false; |
||
| 63 | return false; |
||
| 64 | } |
||
| 65 | $this->ownsLock = true; |
||
| 66 | |||
| 67 | return true; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Update lock timestamp (useful for long-running jobs) |
||
| 72 | */ |
||
| 73 | public function refresh(): bool |
||
| 74 | { |
||
| 75 | if (!$this->ownsLock || $this->lockHandle === null) { |
||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | return $this->writeTimestampToHandle($this->lockHandle); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function release(): bool |
||
| 83 | { |
||
| 84 | if (!$this->ownsLock || $this->lockHandle === null) { |
||
| 85 | return true; |
||
| 86 | } |
||
| 87 | |||
| 88 | $unlocked = flock($this->lockHandle, LOCK_UN); |
||
| 89 | $closed = fclose($this->lockHandle); |
||
| 90 | |||
| 91 | $this->lockHandle = null; |
||
| 92 | $this->ownsLock = false; |
||
| 93 | |||
| 94 | $removed = true; |
||
| 95 | if (fs()->exists($this->lockFile)) { |
||
| 96 | $removed = fs()->remove($this->lockFile); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $unlocked && $closed && $removed; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Check if another process currently holds the lock. |
||
| 104 | */ |
||
| 105 | public function isLocked(): bool |
||
| 106 | { |
||
| 107 | if (!fs()->exists($this->lockFile)) { |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | $handle = fopen($this->lockFile, 'c+'); |
||
| 112 | if ($handle === false) { |
||
| 113 | return true; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (!flock($handle, LOCK_EX | LOCK_NB)) { |
||
| 117 | fclose($handle); |
||
| 118 | return true; |
||
| 119 | } |
||
| 120 | |||
| 121 | flock($handle, LOCK_UN); |
||
| 122 | fclose($handle); |
||
| 123 | |||
| 124 | return false; |
||
| 125 | } |
||
| 126 | |||
| 127 | private function sanitizeTaskName(string $taskName): string |
||
| 128 | { |
||
| 129 | $taskName = trim($taskName); |
||
| 130 | if ($taskName === '') { |
||
| 131 | return 'default'; |
||
| 132 | } |
||
| 133 | |||
| 134 | // Keep safe filename chars only |
||
| 135 | $taskName = preg_replace('/[^a-zA-Z0-9._-]+/', '_', $taskName) ?? 'default'; |
||
| 136 | $taskName = trim($taskName, '._-'); |
||
| 137 | |||
| 138 | return $taskName !== '' ? $taskName : 'default'; |
||
| 139 | } |
||
| 140 | |||
| 141 | private function resolveLockDirectory(?string $lockDirectory): string |
||
| 142 | { |
||
| 143 | $path = $lockDirectory ?? cron_config('lock_path'); |
||
| 144 | return $path === null ? $this->getDefaultLockDirectory() : $path; |
||
| 145 | } |
||
| 146 | |||
| 147 | private function getDefaultLockDirectory(): string |
||
| 148 | { |
||
| 149 | return base_dir() . DS . 'runtime' . DS . 'cron' . DS . 'locks'; |
||
| 150 | } |
||
| 151 | |||
| 152 | private function ensureLockDirectoryExists(): void |
||
| 153 | { |
||
| 154 | if ($this->lockDirectory === '') { |
||
| 155 | throw CronException::lockDirectoryNotWritable(''); |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->createDirectory($this->lockDirectory); |
||
| 159 | |||
| 160 | if (!fs()->isWritable($this->lockDirectory)) { |
||
| 161 | throw CronException::lockDirectoryNotWritable($this->lockDirectory); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | private function createDirectory(string $directory): void |
||
| 166 | { |
||
| 167 | if (fs()->isDirectory($directory)) { |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | $parent = dirname($directory); |
||
| 172 | if ($parent && $parent !== $directory) { |
||
| 173 | $this->createDirectory($parent); |
||
| 174 | } |
||
| 175 | |||
| 176 | // @phpstan-ignore-next-line |
||
| 177 | if (!fs()->makeDirectory($directory) && !fs()->isDirectory($directory)) { |
||
| 178 | throw CronException::lockDirectoryNotWritable($directory); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Removes stale lock files that are NOT currently locked by any process. |
||
| 184 | * Safe because we take LOCK_EX before removing. |
||
| 185 | */ |
||
| 186 | private function cleanupStaleLocks(): void |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | private function writeTimestampToHandle($handle): bool |
||
| 237 | } |
||
| 238 | |||
| 239 | private function readTimestampFromHandle($handle): ?int |
||
| 251 | } |
||
| 252 | } |
||
| 253 |