From f380a64755f8ef3ca8136edb31de1af9fca7540e Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Fri, 16 Dec 2022 19:12:15 +0100 Subject: [PATCH] Reduced indention --- src/Eio/File.php | 113 +++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/src/Eio/File.php b/src/Eio/File.php index c80500f..1e758bd 100644 --- a/src/Eio/File.php +++ b/src/Eio/File.php @@ -30,67 +30,84 @@ public function stat(): PromiseInterface public function getContents(int $offset = 0 , ?int $maxlen = null): PromiseInterface { $this->activate(); - return new Promise(function (callable $resolve, callable $reject) use ($offset, $maxlen): void { - \eio_open( - $this->path . DIRECTORY_SEPARATOR . $this->name, - \EIO_O_RDONLY, - 0, - \EIO_PRI_DEFAULT, - function ($_, $fileDescriptor) use ($resolve, $reject, $offset, $maxlen): void { - try { - \eio_fstat($fileDescriptor, \EIO_PRI_DEFAULT, function ($fileDescriptor, $stat) use ($resolve, $reject, $offset, $maxlen): void { - try { - \eio_read($fileDescriptor, $maxlen ?? (int)$stat['size'], $offset, \EIO_PRI_DEFAULT, function ($fileDescriptor, string $buffer) use ($resolve): void { - \eio_close($fileDescriptor, \EIO_PRI_DEFAULT, function () use ($resolve, $buffer) { - $this->deactivate(); - $resolve($buffer); - }); - }, $fileDescriptor); - } catch (\Throwable $error) { - $this->deactivate(); - $reject($error); - } + return $this->openFile( + $this->path . DIRECTORY_SEPARATOR . $this->name, + \EIO_O_RDONLY, + 0, + )->then( + function ($fileDescriptor) use ($offset, $maxlen): PromiseInterface { + return $this->statFileDescriptor($fileDescriptor)->then(function ($stat) use ($fileDescriptor, $offset, $maxlen): PromiseInterface { + return new Promise (function (callable $resolve) use ($fileDescriptor, $offset, $maxlen, $stat): void { + \eio_read($fileDescriptor, $maxlen ?? (int)$stat['size'], $offset, \EIO_PRI_DEFAULT, function ($fileDescriptor, string $buffer) use ($resolve): void { + $resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($buffer): string { + return $buffer; + })); }, $fileDescriptor); - } catch (\Throwable $error) { - $this->deactivate(); - $reject($error); - } - } - ); - }); + }); + }); + } + ); } public function putContents(string $contents, int $flags = 0) { $this->activate(); - return new Promise(function (callable $resolve, callable $reject) use ($contents, $flags): void { - \eio_open( + return $this->openFile( $this->path . DIRECTORY_SEPARATOR . $this->name, (($flags & \FILE_APPEND) == \FILE_APPEND) ? \EIO_O_RDWR | \EIO_O_APPEND : \EIO_O_RDWR | \EIO_O_CREAT, - 0644, + 0644 + )->then( + function ($fileDescriptor) use ($contents, $flags): PromiseInterface { + return new Promise (function (callable $resolve) use ($contents, $fileDescriptor): void { + \eio_write($fileDescriptor, $contents, strlen($contents), 0, \EIO_PRI_DEFAULT, function ($fileDescriptor, int $bytesWritten) use ($resolve): void { + $resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($bytesWritten): int { + return $bytesWritten; + })); + }, $fileDescriptor); + }); + } + ); + } + + private function statFileDescriptor($fileDescriptor): PromiseInterface + { + return new Promise(function (callable $resolve, callable $reject) use ($fileDescriptor) { + \eio_fstat($fileDescriptor, \EIO_PRI_DEFAULT, function ($_, $stat) use ($resolve): void { + $resolve($stat); + }, $fileDescriptor); + }); + } + + private function openFile(string $path, int $flags, int $mode): PromiseInterface + { + return new Promise(function (callable $resolve, callable $reject) use ($path, $flags, $mode): void { + \eio_open( + $path, + $flags, + $mode, \EIO_PRI_DEFAULT, - function ($_, $fileDescriptor) use ($resolve, $reject, $contents, $flags): void { - try { - \eio_write($fileDescriptor, $contents, strlen($contents), 0, \EIO_PRI_DEFAULT, function ($fileDescriptor, int $bytesWritten) use ($resolve, $reject): void { - try { - \eio_close($fileDescriptor, \EIO_PRI_DEFAULT, function () use ($resolve, $bytesWritten): void { - $this->deactivate(); - $resolve($bytesWritten); - }); - } catch (\Throwable $error) { - $this->deactivate(); - $reject($error); - } - }, $fileDescriptor); - } catch (\Throwable $error) { - $this->deactivate(); - $reject($error); - } + function ($_, $fileDescriptor) use ($resolve): void { + $resolve($fileDescriptor); } ); }); } + private function closeOpenFile($fileDescriptor): PromiseInterface + { + return new Promise(function (callable $resolve) use ($fileDescriptor) { + try { + \eio_close($fileDescriptor, \EIO_PRI_DEFAULT, function () use ($resolve): void { + $this->deactivate(); + $resolve(); + }); + } catch (\Throwable $error) { + $this->deactivate(); + throw $error; + } + }); + } + public function unlink(): PromiseInterface { $this->activate(); @@ -121,4 +138,4 @@ protected function deactivate(): void { $this->poll->deactivate(); } -} \ No newline at end of file +}