| Total Complexity | 52 |
| Total Lines | 249 |
| 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 |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Update lock timestamp (useful for long-running jobs) |
||
| 72 | */ |
||
| 73 | public function refresh(): bool |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getTimestamp(): int |
||
| 83 | { |
||
| 84 | if ($this->lockHandle === null) { |
||
| 85 | return 0; |
||
| 86 | } |
||
| 87 | |||
| 88 | $timestamp = $this->readTimestampFromHandle($this->lockHandle); |
||
| 89 | return $timestamp ?? 0; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function release(): bool |
||
| 93 | { |
||
| 94 | if (!$this->ownsLock || $this->lockHandle === null) { |
||
| 95 | return true; |
||
| 96 | } |
||
| 97 | |||
| 98 | $unlocked = flock($this->lockHandle, LOCK_UN); |
||
| 99 | $closed = fclose($this->lockHandle); |
||
| 100 | |||
| 101 | $this->lockHandle = null; |
||
| 102 | $this->ownsLock = false; |
||
| 103 | |||
| 104 | $removed = true; |
||
| 105 | if (fs()->exists($this->lockFile)) { |
||
| 106 | $removed = fs()->remove($this->lockFile); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $unlocked && $closed && $removed; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Check if another process currently holds the lock. |
||
| 114 | */ |
||
| 115 | public function isLocked(): bool |
||
| 116 | { |
||
| 117 | if (!fs()->exists($this->lockFile)) { |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | |||
| 121 | $handle = fopen($this->lockFile, 'c+'); |
||
| 122 | if ($handle === false) { |
||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!flock($handle, LOCK_EX | LOCK_NB)) { |
||
| 127 | fclose($handle); |
||
| 128 | return true; |
||
| 129 | } |
||
| 130 | |||
| 131 | flock($handle, LOCK_UN); |
||
| 132 | fclose($handle); |
||
| 133 | |||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | private function sanitizeTaskName(string $taskName): string |
||
| 138 | { |
||
| 139 | $taskName = trim($taskName); |
||
| 140 | if ($taskName === '') { |
||
| 141 | return 'default'; |
||
| 142 | } |
||
| 143 | |||
| 144 | // Keep safe filename chars only |
||
| 145 | $taskName = preg_replace('/[^a-zA-Z0-9._-]+/', '_', $taskName) ?? 'default'; |
||
| 146 | $taskName = trim($taskName, '._-'); |
||
| 147 | |||
| 148 | return $taskName !== '' ? $taskName : 'default'; |
||
| 149 | } |
||
| 150 | |||
| 151 | private function resolveLockDirectory(?string $lockDirectory): string |
||
| 152 | { |
||
| 153 | $path = $lockDirectory ?? cron_config('lock_path'); |
||
| 154 | return $path === null ? $this->getDefaultLockDirectory() : $path; |
||
| 155 | } |
||
| 156 | |||
| 157 | private function getDefaultLockDirectory(): string |
||
| 158 | { |
||
| 159 | return base_dir() . DS . 'runtime' . DS . 'cron' . DS . 'locks'; |
||
| 160 | } |
||
| 161 | |||
| 162 | private function ensureLockDirectoryExists(): void |
||
| 163 | { |
||
| 164 | if ($this->lockDirectory === '') { |
||
| 165 | throw CronException::lockDirectoryNotWritable(''); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->createDirectory($this->lockDirectory); |
||
| 169 | |||
| 170 | if (!fs()->isWritable($this->lockDirectory)) { |
||
| 171 | throw CronException::lockDirectoryNotWritable($this->lockDirectory); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | private function createDirectory(string $directory): void |
||
| 176 | { |
||
| 177 | if (fs()->isDirectory($directory)) { |
||
| 178 | return; |
||
| 179 | } |
||
| 180 | |||
| 181 | $parent = dirname($directory); |
||
| 182 | if ($parent && $parent !== $directory) { |
||
| 183 | $this->createDirectory($parent); |
||
| 184 | } |
||
| 185 | |||
| 186 | // @phpstan-ignore-next-line |
||
| 187 | if (!fs()->makeDirectory($directory) && !fs()->isDirectory($directory)) { |
||
| 188 | throw CronException::lockDirectoryNotWritable($directory); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Removes stale lock files that are NOT currently locked by any process. |
||
| 194 | * Safe because we take LOCK_EX before removing. |
||
| 195 | */ |
||
| 196 | private function cleanupStaleLocks(): void |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | private function writeTimestampToHandle($handle): bool |
||
| 252 | } |
||
| 253 | |||
| 254 | private function readTimestampFromHandle($handle): ?int |
||
| 266 | } |
||
| 267 | } |
||
| 268 |