diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dc05cf..ff432ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,15 @@ - Requires `innmind/io:~3.2` - Requires `innmind/immutable:~5.15` - `Innmind\OperatingSystem\Config::withHttpHeartbeat()` period is now expressed with a `Innmind\TimeContinuum\Period` +- `Innmind\OperatingSystem\CurrentProcess::id()` now returns an `Innmind\Immutable\Attempt` - `Innmind\OperatingSystem\CurrentProcess::halt()` now returns `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\Filesystem::mount()` now returns an `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\Filesystem::temporary()` now returns an `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\Ports::open()` now returns an `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\Remote::socket()` now returns an `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\Sockets::open()` now returns an `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\Sockets::takeOver()` now returns an `Innmind\Immutable\Attempt` +- `Innmind\OperatingSystem\Sockets::connectTo()` now returns an `Innmind\Immutable\Attempt` ### Fixed diff --git a/src/CurrentProcess.php b/src/CurrentProcess.php index 9ec45b6..c1d0eb1 100644 --- a/src/CurrentProcess.php +++ b/src/CurrentProcess.php @@ -14,7 +14,10 @@ interface CurrentProcess { - public function id(): Pid; + /** + * @return Attempt + */ + public function id(): Attempt; public function signals(): Signals; /** diff --git a/src/CurrentProcess/Generic.php b/src/CurrentProcess/Generic.php index 690b237..71ea177 100644 --- a/src/CurrentProcess/Generic.php +++ b/src/CurrentProcess/Generic.php @@ -30,13 +30,15 @@ public static function of(Halt $halt): self } #[\Override] - public function id(): Pid + public function id(): Attempt { - /** - * @psalm-suppress ArgumentTypeCoercion - * @psalm-suppress PossiblyFalseArgument - */ - return new Pid(\getmypid()); + $pid = \getmypid(); + + /** @psalm-suppress ArgumentTypeCoercion */ + return match ($pid) { + false => Attempt::error(new \RuntimeException('Failed to retrieve process id')), + default => Attempt::result(new Pid($pid)), + }; } #[\Override] diff --git a/src/CurrentProcess/Logger.php b/src/CurrentProcess/Logger.php index be33607..918d9a5 100644 --- a/src/CurrentProcess/Logger.php +++ b/src/CurrentProcess/Logger.php @@ -4,7 +4,6 @@ namespace Innmind\OperatingSystem\CurrentProcess; use Innmind\OperatingSystem\CurrentProcess; -use Innmind\Server\Control\Server\Process\Pid; use Innmind\Server\Status\Server\Memory\Bytes; use Innmind\TimeContinuum\Period; use Innmind\Immutable\Attempt; @@ -28,16 +27,16 @@ public static function psr(CurrentProcess $process, LoggerInterface $logger): se } #[\Override] - public function id(): Pid + public function id(): Attempt { - $pid = $this->process->id(); + return $this->process->id()->map(function($pid) { + $this->logger->debug( + 'Current process id is {pid}', + ['pid' => $pid->toInt()], + ); - $this->logger->debug( - 'Current process id is {pid}', - ['pid' => $pid->toInt()], - ); - - return $pid; + return $pid; + }); } #[\Override] diff --git a/src/Filesystem.php b/src/Filesystem.php index 15fce46..85ce603 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -10,6 +10,7 @@ use Innmind\Url\Path; use Innmind\FileWatch\Ping; use Innmind\Immutable\{ + Attempt, Maybe, Str, Sequence, @@ -17,7 +18,10 @@ interface Filesystem { - public function mount(Path $path): Adapter; + /** + * @return Attempt + */ + public function mount(Path $path): Attempt; public function contains(Path $path): bool; /** @@ -36,7 +40,7 @@ public function watch(Path $path): Ping; * * @param Sequence> $chunks * - * @return Maybe + * @return Attempt */ - public function temporary(Sequence $chunks): Maybe; + public function temporary(Sequence $chunks): Attempt; } diff --git a/src/Filesystem/Generic.php b/src/Filesystem/Generic.php index 6fa1464..ca31c4e 100644 --- a/src/Filesystem/Generic.php +++ b/src/Filesystem/Generic.php @@ -48,7 +48,7 @@ public static function of(Processes $processes, Config $config): self } #[\Override] - public function mount(Path $path): Adapter + public function mount(Path $path): Attempt { /** * @var Adapter $adapter @@ -56,20 +56,22 @@ public function mount(Path $path): Adapter */ foreach ($this->mounted as $adapter => $mounted) { if ($path->toString() === $mounted) { - return $adapter; + return Attempt::result($adapter); } } - $adapter = Adapter\Filesystem::mount( - $path, - $this->config->io(), - ) - ->withCaseSensitivity( - $this->config->filesystemCaseSensitivity(), - ); - $this->mounted[$adapter] = $path->toString(); - - return $adapter; + return Attempt::of(function() use ($path) { + $adapter = Adapter\Filesystem::mount( + $path, + $this->config->io(), + ) + ->withCaseSensitivity( + $this->config->filesystemCaseSensitivity(), + ); + $this->mounted[$adapter] = $path->toString(); + + return $adapter; + }); } #[\Override] @@ -111,7 +113,7 @@ public function watch(Path $path): Ping } #[\Override] - public function temporary(Sequence $chunks): Maybe + public function temporary(Sequence $chunks): Attempt { return Attempt::of( fn() => $this @@ -129,6 +131,6 @@ public function temporary(Sequence $chunks): Maybe ->map(static fn($tmp) => $tmp->read()) ->map(Content::io(...)) ->unwrap(), - )->maybe(); + ); } } diff --git a/src/Filesystem/Logger.php b/src/Filesystem/Logger.php index 9019dc0..6152594 100644 --- a/src/Filesystem/Logger.php +++ b/src/Filesystem/Logger.php @@ -11,6 +11,7 @@ use Innmind\Url\Path; use Innmind\FileWatch\Ping; use Innmind\Immutable\{ + Attempt, Maybe, Sequence, }; @@ -37,11 +38,13 @@ public static function psr( } #[\Override] - public function mount(Path $path): Adapter + public function mount(Path $path): Attempt { - return Adapter\Logger::psr( - $this->filesystem->mount($path), - $this->logger, + return $this->filesystem->mount($path)->map( + fn($adapter) => Adapter\Logger::psr( + $adapter, + $this->logger, + ), ); } @@ -82,7 +85,7 @@ public function watch(Path $path): Ping } #[\Override] - public function temporary(Sequence $chunks): Maybe + public function temporary(Sequence $chunks): Attempt { return $this ->filesystem diff --git a/src/Ports.php b/src/Ports.php index b2f4433..4c3b5e1 100644 --- a/src/Ports.php +++ b/src/Ports.php @@ -9,12 +9,12 @@ Sockets\Internet\Transport, }; use Innmind\IP\IP; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; interface Ports { /** - * @return Maybe + * @return Attempt */ - public function open(Transport $transport, IP $ip, Port $port): Maybe; + public function open(Transport $transport, IP $ip, Port $port): Attempt; } diff --git a/src/Ports/Logger.php b/src/Ports/Logger.php index 8fed62b..6891152 100644 --- a/src/Ports/Logger.php +++ b/src/Ports/Logger.php @@ -7,7 +7,7 @@ use Innmind\Url\Authority\Port; use Innmind\IO\Sockets\Internet\Transport; use Innmind\IP\IP; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Psr\Log\LoggerInterface; final class Logger implements Ports @@ -27,7 +27,7 @@ public static function psr(Ports $ports, LoggerInterface $logger): self } #[\Override] - public function open(Transport $transport, IP $ip, Port $port): Maybe + public function open(Transport $transport, IP $ip, Port $port): Attempt { $this->logger->debug( 'Opening new port at {address}', diff --git a/src/Ports/Unix.php b/src/Ports/Unix.php index dc25700..e131cfc 100644 --- a/src/Ports/Unix.php +++ b/src/Ports/Unix.php @@ -10,7 +10,7 @@ use Innmind\Url\Authority\Port; use Innmind\IO\Sockets\Internet\Transport; use Innmind\IP\IP; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; final class Unix implements Ports { @@ -30,14 +30,13 @@ public static function of(Config $config): self } #[\Override] - public function open(Transport $transport, IP $ip, Port $port): Maybe + public function open(Transport $transport, IP $ip, Port $port): Attempt { return $this ->config ->io() ->sockets() ->servers() - ->internet($transport, $ip, $port) - ->maybe(); + ->internet($transport, $ip, $port); } } diff --git a/src/Remote.php b/src/Remote.php index 0a2cf9a..a97518d 100644 --- a/src/Remote.php +++ b/src/Remote.php @@ -13,7 +13,7 @@ Authority, }; use Innmind\HttpTransport\Transport as HttpTransport; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; interface Remote @@ -21,9 +21,9 @@ interface Remote public function ssh(Url $server): Server; /** - * @return Maybe + * @return Attempt */ - public function socket(Transport $transport, Authority $authority): Maybe; + public function socket(Transport $transport, Authority $authority): Attempt; public function http(): HttpTransport; public function sql(Url $server): Connection; } diff --git a/src/Remote/Generic.php b/src/Remote/Generic.php index de95406..46c484a 100644 --- a/src/Remote/Generic.php +++ b/src/Remote/Generic.php @@ -21,7 +21,7 @@ Transport as HttpTransport, Curl, }; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; final class Generic implements Remote @@ -62,15 +62,14 @@ public function ssh(Url $server): Server } #[\Override] - public function socket(Transport $transport, Authority $authority): Maybe + public function socket(Transport $transport, Authority $authority): Attempt { return $this ->config ->io() ->sockets() ->clients() - ->internet($transport, $authority) - ->maybe(); + ->internet($transport, $authority); } #[\Override] diff --git a/src/Remote/Logger.php b/src/Remote/Logger.php index 9db7403..9189ce2 100644 --- a/src/Remote/Logger.php +++ b/src/Remote/Logger.php @@ -11,7 +11,7 @@ Authority, }; use Innmind\HttpTransport; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; use Psr\Log\LoggerInterface; @@ -41,7 +41,7 @@ public function ssh(Url $server): Control\Server } #[\Override] - public function socket(Transport $transport, Authority $authority): Maybe + public function socket(Transport $transport, Authority $authority): Attempt { $this->logger->debug( 'Opening remote socket at {address}', diff --git a/src/Remote/Resilient.php b/src/Remote/Resilient.php index 1515604..9f0a86a 100644 --- a/src/Remote/Resilient.php +++ b/src/Remote/Resilient.php @@ -19,10 +19,7 @@ Transport as HttpTransport, ExponentialBackoff, }; -use Innmind\Immutable\{ - Maybe, - Attempt, -}; +use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; final class Resilient implements Remote @@ -48,7 +45,7 @@ public function ssh(Url $server): Server } #[\Override] - public function socket(Transport $transport, Authority $authority): Maybe + public function socket(Transport $transport, Authority $authority): Attempt { return $this->remote->socket($transport, $authority); } diff --git a/src/Sockets.php b/src/Sockets.php index b5f1d83..cb136e5 100644 --- a/src/Sockets.php +++ b/src/Sockets.php @@ -8,26 +8,26 @@ Sockets\Servers\Server, Sockets\Unix\Address, }; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; interface Sockets { /** * This method will fail if the socket already exist * - * @return Maybe + * @return Attempt */ - public function open(Address $address): Maybe; + public function open(Address $address): Attempt; /** * This will take control of the socket if it already exist (use carefully) * - * @return Maybe + * @return Attempt */ - public function takeOver(Address $address): Maybe; + public function takeOver(Address $address): Attempt; /** - * @return Maybe + * @return Attempt */ - public function connectTo(Address $address): Maybe; + public function connectTo(Address $address): Attempt; } diff --git a/src/Sockets/Logger.php b/src/Sockets/Logger.php index 3c2fba3..9ba4cf0 100644 --- a/src/Sockets/Logger.php +++ b/src/Sockets/Logger.php @@ -5,7 +5,7 @@ use Innmind\OperatingSystem\Sockets; use Innmind\IO\Sockets\Unix\Address; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Psr\Log\LoggerInterface; final class Logger implements Sockets @@ -25,7 +25,7 @@ public static function psr(Sockets $sockets, LoggerInterface $logger): self } #[\Override] - public function open(Address $address): Maybe + public function open(Address $address): Attempt { $this->logger->debug( 'Opening socket at {address}', @@ -36,7 +36,7 @@ public function open(Address $address): Maybe } #[\Override] - public function takeOver(Address $address): Maybe + public function takeOver(Address $address): Attempt { $this->logger->debug( 'Taking over the socket at {address}', @@ -47,7 +47,7 @@ public function takeOver(Address $address): Maybe } #[\Override] - public function connectTo(Address $address): Maybe + public function connectTo(Address $address): Attempt { $this->logger->debug( 'Connecting to socket at {address}', diff --git a/src/Sockets/Unix.php b/src/Sockets/Unix.php index f169224..03ee3da 100644 --- a/src/Sockets/Unix.php +++ b/src/Sockets/Unix.php @@ -8,7 +8,7 @@ Config, }; use Innmind\IO\Sockets\Unix\Address; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; final class Unix implements Sockets { @@ -28,38 +28,35 @@ public static function of(Config $config): self } #[\Override] - public function open(Address $address): Maybe + public function open(Address $address): Attempt { return $this ->config ->io() ->sockets() ->servers() - ->unix($address) - ->maybe(); + ->unix($address); } #[\Override] - public function takeOver(Address $address): Maybe + public function takeOver(Address $address): Attempt { return $this ->config ->io() ->sockets() ->servers() - ->takeOver($address) - ->maybe(); + ->takeOver($address); } #[\Override] - public function connectTo(Address $address): Maybe + public function connectTo(Address $address): Attempt { return $this ->config ->io() ->sockets() ->clients() - ->unix($address) - ->maybe(); + ->unix($address); } } diff --git a/tests/CurrentProcess/GenericTest.php b/tests/CurrentProcess/GenericTest.php index 2315189..c782669 100644 --- a/tests/CurrentProcess/GenericTest.php +++ b/tests/CurrentProcess/GenericTest.php @@ -29,8 +29,11 @@ public function testId() { $process = Generic::of($this->createMock(Halt::class)); - $this->assertInstanceOf(Pid::class, $process->id()); - $this->assertSame($process->id()->toInt(), $process->id()->toInt()); + $this->assertInstanceOf(Pid::class, $process->id()->unwrap()); + $this->assertSame( + $process->id()->unwrap()->toInt(), + $process->id()->unwrap()->toInt(), + ); } public function testHalt() diff --git a/tests/CurrentProcess/LoggerTest.php b/tests/CurrentProcess/LoggerTest.php index 50ed108..74e97b2 100644 --- a/tests/CurrentProcess/LoggerTest.php +++ b/tests/CurrentProcess/LoggerTest.php @@ -46,7 +46,7 @@ public function testId() $inner ->expects($this->once()) ->method('id') - ->willReturn($expected = new Pid($id)); + ->willReturn(Attempt::result($expected = new Pid($id))); $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) @@ -57,7 +57,7 @@ public function testId() ); $process = Logger::psr($inner, $logger); - $this->assertSame($expected, $process->id()); + $this->assertSame($expected, $process->id()->unwrap()); }); } diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index db0786f..f3bcf98 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -49,7 +49,8 @@ public function testPersistingFileOnCaseInsensitiveFilesystem() $os = Factory::build(Config::of()->caseInsensitiveFilesystem()); $adapter = $os ->filesystem() - ->mount(Path::of($path)); + ->mount(Path::of($path)) + ->unwrap(); $adapter->add( $directory = Directory::named('0') ->add($file = File::named('L', Content::none())) diff --git a/tests/Filesystem/GenericTest.php b/tests/Filesystem/GenericTest.php index 0d4ad6b..01bde44 100644 --- a/tests/Filesystem/GenericTest.php +++ b/tests/Filesystem/GenericTest.php @@ -49,7 +49,7 @@ public function testMount() Config::of(), ); - $adapter = $filesystem->mount(Path::of('/tmp/')); + $adapter = $filesystem->mount(Path::of('/tmp/'))->unwrap(); $this->assertInstanceOf(FilesystemAdapter::class, $adapter); } @@ -61,9 +61,9 @@ public function testMountingTheSamePathTwiceReturnsTheSameAdapter() Config::of(), ); - $adapter = $filesystem->mount(Path::of('/tmp/')); + $adapter = $filesystem->mount(Path::of('/tmp/'))->unwrap(); - $this->assertSame($adapter, $filesystem->mount(Path::of('/tmp/'))); + $this->assertSame($adapter, $filesystem->mount(Path::of('/tmp/'))->unwrap()); } public function testContainsFile() diff --git a/tests/Filesystem/LoggerTest.php b/tests/Filesystem/LoggerTest.php index 41aade7..435fb75 100644 --- a/tests/Filesystem/LoggerTest.php +++ b/tests/Filesystem/LoggerTest.php @@ -14,7 +14,10 @@ FileWatch\Ping, }; use Innmind\TimeWarp\Halt; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\{ + Attempt, + Maybe, +}; use Psr\Log\LoggerInterface; use PHPUnit\Framework\TestCase; use Innmind\BlackBox\{ @@ -47,11 +50,15 @@ public function testMount() $inner ->expects($this->once()) ->method('mount') - ->with($path); + ->with($path) + ->willReturn(Attempt::result($this->createMock(Adapter::class))); $logger = $this->createMock(LoggerInterface::class); $filesystem = Logger::psr($inner, $logger); - $this->assertInstanceOf(Adapter\Logger::class, $filesystem->mount($path)); + $this->assertInstanceOf( + Adapter\Logger::class, + $filesystem->mount($path)->unwrap(), + ); }); } diff --git a/tests/Ports/LoggerTest.php b/tests/Ports/LoggerTest.php index 70ea9ed..90cd958 100644 --- a/tests/Ports/LoggerTest.php +++ b/tests/Ports/LoggerTest.php @@ -16,7 +16,7 @@ IPv4, IPv6, }; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Psr\Log\LoggerInterface; use PHPUnit\Framework\TestCase; use Innmind\BlackBox\{ @@ -63,7 +63,7 @@ public function testOpen() ->expects($this->once()) ->method('open') ->with($transport, $ip, Port::of($port)) - ->willReturn($expected = Maybe::just(null /* hack to avoid creating real server */)); + ->willReturn($expected = Attempt::result(null /* hack to avoid creating real server */)); $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) diff --git a/tests/Remote/LoggerTest.php b/tests/Remote/LoggerTest.php index faca8b6..8499df0 100644 --- a/tests/Remote/LoggerTest.php +++ b/tests/Remote/LoggerTest.php @@ -14,7 +14,7 @@ Clients\Client, }; use Innmind\Url\Url; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; use Psr\Log\LoggerInterface; use PHPUnit\Framework\TestCase; @@ -82,7 +82,7 @@ public function testSocket() ->expects($this->once()) ->method('socket') ->with($transport, $authority) - ->willReturn($expected = Maybe::just(null /* hack to avoid creating real client */)); + ->willReturn($expected = Attempt::result(null /* hack to avoid creating real client */)); $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) diff --git a/tests/Remote/ResilientTest.php b/tests/Remote/ResilientTest.php index 0dc6634..d4cde94 100644 --- a/tests/Remote/ResilientTest.php +++ b/tests/Remote/ResilientTest.php @@ -14,7 +14,7 @@ Internet\Transport, Clients\Client, }; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Formal\AccessLayer\Connection; use PHPUnit\Framework\TestCase; use Innmind\BlackBox\{ @@ -82,7 +82,7 @@ public function testSocket() ->expects($this->once()) ->method('socket') ->with($transport, $authority) - ->willReturn($expected = Maybe::just(null /* hack to avoid creating real client */)); + ->willReturn($expected = Attempt::result(null /* hack to avoid creating real client */)); $this->assertSame($expected, $remote->socket($transport, $authority)); }); diff --git a/tests/Sockets/LoggerTest.php b/tests/Sockets/LoggerTest.php index 05a4e61..1ab4607 100644 --- a/tests/Sockets/LoggerTest.php +++ b/tests/Sockets/LoggerTest.php @@ -13,7 +13,7 @@ Clients\Client, }; use Innmind\Url\Path; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Psr\Log\LoggerInterface; use PHPUnit\Framework\TestCase; @@ -38,7 +38,7 @@ public function testOpen() ->expects($this->once()) ->method('open') ->with($address) - ->willReturn($expected = Maybe::just(null /* hack to avoid creating real server */)); + ->willReturn($expected = Attempt::result(null /* hack to avoid creating real server */)); $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) @@ -60,7 +60,7 @@ public function testTakeOver() ->expects($this->once()) ->method('takeOver') ->with($address) - ->willReturn($expected = Maybe::just(null /* hack to avoid creating real server */)); + ->willReturn($expected = Attempt::result(null /* hack to avoid creating real server */)); $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) @@ -82,7 +82,7 @@ public function testConnectTo() ->expects($this->once()) ->method('connectTo') ->with($address) - ->willReturn($expected = Maybe::just(null /* hack to avoid creating real client */)); + ->willReturn($expected = Attempt::result(null /* hack to avoid creating real client */)); $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once())