Skip to content

Commit 39dd2f0

Browse files
committed
Optimized IOHelperTrait quality by splitting some code
1 parent 2278a05 commit 39dd2f0

1 file changed

Lines changed: 35 additions & 30 deletions

File tree

lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ trait IOHelperTrait
4242
* for files-based drivers
4343
* @return DriverStatistic
4444
* @throws PhpfastcacheIOException
45+
* @throws PhpfastcacheInvalidArgumentException
4546
*/
4647
public function getStats(): DriverStatistic
4748
{
@@ -73,6 +74,7 @@ public function getStats(): DriverStatistic
7374
* @param bool $skip
7475
* @return string
7576
* @throws PhpfastcacheIOException
77+
* @throws PhpfastcacheInvalidArgumentException
7678
*/
7779
protected function getFilePath(string|bool $keyword, bool $skip = false): string
7880
{
@@ -103,36 +105,12 @@ protected function getFilePath(string|bool $keyword, bool $skip = false): string
103105
* @return string
104106
* @throws PhpfastcacheIOException
105107
* @throws PhpfastcacheInvalidArgumentException
106-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
107-
* @SuppressWarnings(PHPMD.NPathComplexity)
108108
*/
109109
public function getPath(bool $readonly = false): string
110110
{
111-
/**
112-
* Get the base system temporary directory
113-
*/
114111
$tmpDir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache';
115-
116-
/**
117-
* Calculate the security key
118-
*/
119-
{
120-
$httpHost = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'HTTP_HOST');
121-
$securityKey = $this->getConfig()->getSecurityKey();
122-
if (!$securityKey || \mb_strtolower($securityKey) === 'auto') {
123-
if (isset($httpHost)) {
124-
$securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $httpHost)));
125-
} else {
126-
$securityKey = (SapiDetector::isWebScript() ? 'web' : 'cli');
127-
}
128-
}
129-
130-
if ($securityKey !== '') {
131-
$securityKey .= '/';
132-
}
133-
134-
$securityKey = static::cleanFileName($securityKey);
135-
}
112+
$httpHost = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'HTTP_HOST');
113+
$securityKey = $this->buildSecurityKey($httpHost);
136114

137115
/**
138116
* Extends the temporary directory
@@ -149,21 +127,50 @@ public function getPath(bool $readonly = false): string
149127
$pathSuffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName();
150128
$fullPath = Directory::getAbsolutePath($path . $pathSuffix);
151129
$fullPathTmp = Directory::getAbsolutePath($tmpDir . $pathSuffix);
152-
$fullPathHash = $this->getConfig()->getDefaultFileNameHashFunction()($fullPath);
130+
131+
$this->mkdir($fullPath, $fullPathTmp);
153132

154133
/**
155134
* In readonly mode we only attempt
156135
* to verify if the directory exists
157136
* or not, if it does not then we
158137
* return the temp dir
159138
*/
160-
if ($readonly === true) {
139+
if ($readonly) {
161140
if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($fullPath) || !@\is_writable($fullPath))) {
162141
return $fullPathTmp;
163142
}
164143
return $fullPath;
165144
}
166145

146+
return realpath($fullPath);
147+
}
148+
149+
protected function buildSecurityKey(?string $httpHost): string
150+
{
151+
$securityKey = $this->getConfig()->getSecurityKey();
152+
if (!$securityKey || \mb_strtolower($securityKey) === 'auto') {
153+
if (isset($httpHost)) {
154+
$securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $httpHost)));
155+
} else {
156+
$securityKey = (SapiDetector::isWebScript() ? 'web' : 'cli');
157+
}
158+
}
159+
160+
if (!empty($securityKey)) {
161+
$securityKey .= '/';
162+
}
163+
164+
return static::cleanFileName($securityKey);
165+
}
166+
167+
/**
168+
* @throws PhpfastcacheIOException
169+
*/
170+
protected function mkdir(string $fullPath, string $fullPathTmp): void
171+
{
172+
$fullPathHash = $this->getConfig()->getDefaultFileNameHashFunction()($fullPath);
173+
167174
if (!isset($this->tmp[$fullPathHash]) || (!@\file_exists($fullPath) || !@\is_writable($fullPath))) {
168175
if (!@\file_exists($fullPath)) {
169176
if (@mkdir($fullPath, $this->getDefaultChmod(), true) === false && !\is_dir($fullPath)) {
@@ -193,8 +200,6 @@ public function getPath(bool $readonly = false): string
193200

194201
$this->tmp[$fullPathHash] = $fullPath;
195202
}
196-
197-
return realpath($fullPath);
198203
}
199204

200205
/**

0 commit comments

Comments
 (0)