Skip to content

Commit

Permalink
style: use more descriptive variable names for wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Aug 13, 2020
1 parent 3fc92c5 commit 620d364
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
60 changes: 30 additions & 30 deletions src/CloudStorage/CloudStorageStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ class CloudStorageStreamWrapper
private $cache = [];

/**
* The key for the cloud storage object being accessed.
* Mode used when the stream was opened.
*
* @var string
*/
private $key;
private $openedStreamMode;

/**
* Mode used when the stream was opened.
* The key for the cloud storage object opened by "stream_open".
*
* @var string
*/
private $mode;
private $openedStreamObjectKey;

/**
* The resource containing the cloud storage object being accessed.
* The resource containing the cloud storage object opened by "stream_open".
*
* @var resource|null
*/
private $objectResource;
private $openedStreamObjectResource;

/**
* Register the cloud storage stream wrapper.
Expand Down Expand Up @@ -165,7 +165,7 @@ public function stream_cast(): bool
public function stream_close()
{
$this->cache = [];
fclose($this->objectResource);
fclose($this->openedStreamObjectResource);
}

/**
Expand All @@ -175,7 +175,7 @@ public function stream_close()
*/
public function stream_eof(): bool
{
return feof($this->objectResource);
return feof($this->openedStreamObjectResource);
}

/**
Expand All @@ -187,16 +187,16 @@ public function stream_eof(): bool
*/
public function stream_flush()
{
if ('r' === $this->mode) {
if ('r' === $this->openedStreamMode) {
return false;
}

return $this->call(function () {
rewind($this->objectResource);
rewind($this->openedStreamObjectResource);

$this->getClient()->putObject($this->key, stream_get_contents($this->objectResource), $this->getMimetype());
$this->getClient()->putObject($this->openedStreamObjectKey, stream_get_contents($this->openedStreamObjectResource), $this->getMimetype());

$this->removeCacheValue($this->key);
$this->removeCacheValue($this->openedStreamObjectKey);
});
}

Expand All @@ -218,31 +218,31 @@ public function stream_metadata(): bool
public function stream_open(string $path, string $mode): bool
{
return $this->call(function () use ($path, $mode) {
$this->key = $this->parsePath($path);
$this->mode = $this->parseMode($this->key, $mode);
$this->openedStreamObjectKey = $this->parsePath($path);
$this->openedStreamMode = $this->parseMode($this->openedStreamObjectKey, $mode);

$client = $this->getClient();
$object = '';

if ('a' === $this->mode) {
if ('a' === $this->openedStreamMode) {
try {
$object = $client->getObject($this->key);
$object = $client->getObject($this->openedStreamObjectKey);
} catch (\Exception $exception) {
}
} elseif ('r' === $this->mode) {
$object = $client->getObject($this->key);
} elseif ('r' === $this->openedStreamMode) {
$object = $client->getObject($this->openedStreamObjectKey);
}

if ('r' !== $this->mode) {
if ('r' !== $this->openedStreamMode) {
// Test that we can save the file that we're opening
$client->putObject($this->key, $object);
$client->putObject($this->openedStreamObjectKey, $object);
}

$this->objectResource = fopen('php://temp', 'r+');
fwrite($this->objectResource, $object);
$this->openedStreamObjectResource = fopen('php://temp', 'r+');
fwrite($this->openedStreamObjectResource, $object);

if ('r' === $this->mode) {
rewind($this->objectResource);
if ('r' === $this->openedStreamMode) {
rewind($this->openedStreamObjectResource);
}
});
}
Expand All @@ -254,7 +254,7 @@ public function stream_open(string $path, string $mode): bool
*/
public function stream_read(int $count)
{
return fread($this->objectResource, $count);
return fread($this->openedStreamObjectResource, $count);
}

/**
Expand All @@ -264,7 +264,7 @@ public function stream_read(int $count)
*/
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
{
return 0 === fseek($this->objectResource, $offset, $whence);
return 0 === fseek($this->openedStreamObjectResource, $offset, $whence);
}

/**
Expand All @@ -274,7 +274,7 @@ public function stream_seek(int $offset, int $whence = SEEK_SET): bool
*/
public function stream_stat()
{
return $this->getStat($this->key);
return $this->getStat($this->openedStreamObjectKey);
}

/**
Expand All @@ -284,7 +284,7 @@ public function stream_stat()
*/
public function stream_tell()
{
return ftell($this->objectResource);
return ftell($this->openedStreamObjectResource);
}

/**
Expand All @@ -294,7 +294,7 @@ public function stream_tell()
*/
public function stream_write($data)
{
return fwrite($this->objectResource, $data);
return fwrite($this->openedStreamObjectResource, $data);
}

/**
Expand Down Expand Up @@ -484,7 +484,7 @@ private function getMimetype(): string
'zip' => 'application/zip',
];

$extension = strtolower(pathinfo($this->key, PATHINFO_EXTENSION));
$extension = strtolower(pathinfo($this->openedStreamObjectKey, PATHINFO_EXTENSION));

return $mimetypes[$extension] ?? '';
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/CloudStorage/CloudStorageStreamWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ public function testStreamFlushWhenNotReading()

$wrapperReflection = new \ReflectionObject($wrapper);

$keyReflection = $wrapperReflection->getProperty('key');
$keyReflection = $wrapperReflection->getProperty('openedStreamObjectKey');
$keyReflection->setAccessible(true);

$modeReflection = $wrapperReflection->getProperty('mode');
$modeReflection = $wrapperReflection->getProperty('openedStreamMode');
$modeReflection->setAccessible(true);

$keyReflection->setValue($wrapper, '/foo.txt');
Expand Down Expand Up @@ -266,7 +266,7 @@ public function testStreamFlushWhenReading()

$wrapperReflection = new \ReflectionObject($wrapper);

$modeReflection = $wrapperReflection->getProperty('mode');
$modeReflection = $wrapperReflection->getProperty('openedStreamMode');
$modeReflection->setAccessible(true);

$modeReflection->setValue($wrapper, 'r');
Expand Down Expand Up @@ -444,7 +444,7 @@ public function testStreamStatWhenObjectDoesntExist()

$wrapperReflection = new \ReflectionObject($wrapper);

$keyReflection = $wrapperReflection->getProperty('key');
$keyReflection = $wrapperReflection->getProperty('openedStreamObjectKey');
$keyReflection->setAccessible(true);
$keyReflection->setValue($wrapper, '/foo.txt');

Expand All @@ -470,7 +470,7 @@ public function testStreamStatWithDirectory()

$wrapperReflection = new \ReflectionObject($wrapper);

$keyReflection = $wrapperReflection->getProperty('key');
$keyReflection = $wrapperReflection->getProperty('openedStreamObjectKey');
$keyReflection->setAccessible(true);
$keyReflection->setValue($wrapper, '/directory/');

Expand Down Expand Up @@ -510,7 +510,7 @@ public function testStreamStatWithFileSize()

$wrapperReflection = new \ReflectionObject($wrapper);

$keyReflection = $wrapperReflection->getProperty('key');
$keyReflection = $wrapperReflection->getProperty('openedStreamObjectKey');
$keyReflection->setAccessible(true);
$keyReflection->setValue($wrapper, '/foo.txt');

Expand Down Expand Up @@ -550,7 +550,7 @@ public function testStreamStatWithLastModified()

$wrapperReflection = new \ReflectionObject($wrapper);

$keyReflection = $wrapperReflection->getProperty('key');
$keyReflection = $wrapperReflection->getProperty('openedStreamObjectKey');
$keyReflection->setAccessible(true);
$keyReflection->setValue($wrapper, '/foo.txt');

Expand Down Expand Up @@ -590,7 +590,7 @@ public function testStreamStatWithRegularFile()

$wrapperReflection = new \ReflectionObject($wrapper);

$keyReflection = $wrapperReflection->getProperty('key');
$keyReflection = $wrapperReflection->getProperty('openedStreamObjectKey');
$keyReflection->setAccessible(true);
$keyReflection->setValue($wrapper, '/foo.txt');

Expand Down

0 comments on commit 620d364

Please sign in to comment.