Separation of concerns
This commit is contained in:
44
src/IO/Filesystem/File.php
Normal file
44
src/IO/Filesystem/File.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace IO\Filesystem;
|
||||
|
||||
use IO\Exception\FileNotFoundException;
|
||||
use IO\Exception\FileNotReadableException;
|
||||
use SplFileInfo;
|
||||
|
||||
class File
|
||||
{
|
||||
private SplFileInfo $info;
|
||||
|
||||
private function __construct(string $filepath)
|
||||
{
|
||||
$this->guardUnusableFile($filepath);
|
||||
$filepath = realpath($filepath);
|
||||
$this->info = new SplFileInfo($filepath);
|
||||
}
|
||||
|
||||
public static function fromString(string $filepath): self
|
||||
{
|
||||
return new self($filepath);
|
||||
}
|
||||
|
||||
public function getInfo(): SplFileInfo
|
||||
{
|
||||
return $this->info;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return (string)$this->getInfo();
|
||||
}
|
||||
|
||||
private function guardUnusableFile(string $file): void
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
throw new FileNotFoundException($file);
|
||||
}
|
||||
if (!is_readable($file)) {
|
||||
throw new FileNotReadableException($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user