diff --git a/src/IO/Exception/DirectoryNotReadableException.php b/src/IO/Exception/DirectoryNotReadableException.php new file mode 100644 index 0000000..ce647a7 --- /dev/null +++ b/src/IO/Exception/DirectoryNotReadableException.php @@ -0,0 +1,11 @@ +directory = $directory; - $this->filters = $filters; - - $factory = new FilterFactory(); - $this->filters = array_map([$factory, 'createFromString'], $this->filters); - } - public static function createFromGlobals(): self { $arguments = self::getArguments(); @@ -33,15 +25,17 @@ class FinderArguments return new self($dir, $arguments); } + public function __construct(?string $directory, array $filters) + { + $this->guardUnusableDirectory($directory); + $this->directory = realpath($directory); + + $factory = new FilterFactory(); + $this->filters = array_map([$factory, 'createFromString'], $filters); + } + public function getDirectory(): string { - if (!file_exists($this->directory)) { - throw new DirectoryNotFoundException($this->directory); - } - if (!is_dir($this->directory)) { - throw new NotADirectoryException($this->directory); - } - return $this->directory; } @@ -52,4 +46,17 @@ class FinderArguments { return $this->filters; } + + private function guardUnusableDirectory(string $directory): void + { + if (!file_exists($directory)) { + throw new DirectoryNotFoundException($directory); + } + if (!is_dir($directory)) { + throw new NotADirectoryException($directory); + } + if (!is_readable($directory)) { + throw new DirectoryNotReadableException($directory); + } + } } diff --git a/src/IO/Input/ShowInfoArguments.php b/src/IO/Input/ShowInfoArguments.php index 5659094..f591d34 100644 --- a/src/IO/Input/ShowInfoArguments.php +++ b/src/IO/Input/ShowInfoArguments.php @@ -11,12 +11,7 @@ class ShowInfoArguments { use ArgvAccess; - private ?string $file; - - public function __construct(?string $file) - { - $this->file = $file; - } + private SplFileInfo $file; public static function createFromGlobals(): self { @@ -24,18 +19,27 @@ class ShowInfoArguments return new self(array_shift($arguments)); } + public function __construct(?string $file) + { + $this->guardUnusableFile($file); + $this->file = new SplFileInfo($file); + } + public function getFile(): SplFileInfo { - if (is_null($this->file)) { + return $this->file; + } + + private function guardUnusableFile(string $file): void + { + if (is_null($file)) { throw new MissingFileArgumentException(); } - if (!file_exists($this->file)) { - throw new FileNotFoundException($this->file); + if (!file_exists($file)) { + throw new FileNotFoundException($file); } - if (!is_readable($this->file)) { - throw new FileNotReadableException($this->file); + if (!is_readable($file)) { + throw new FileNotReadableException($file); } - - return new SplFileInfo($this->file); } } diff --git a/src/RecursiveDocumentLocator.php b/src/RecursiveDocumentLocator.php index 0a942bd..a7ca008 100644 --- a/src/RecursiveDocumentLocator.php +++ b/src/RecursiveDocumentLocator.php @@ -22,9 +22,19 @@ class RecursiveDocumentLocator RecursiveIteratorIterator::SELF_FIRST ); - return collect($iterator) - ->filter(static fn(SplFileInfo $fileInfo) => $fileInfo->isFile()) - ->filter(static fn(SplFileInfo $fileInfo) => preg_match('/.pdf$/i', $fileInfo->getBasename())) - ->map(fn(SplFileInfo $fileInfo) => $this->documentFactory->createDocument($fileInfo)); + $documents = []; + foreach ($iterator as $file) { + if ($this->validate($file)) { + $documents[] = $this->documentFactory->createDocument($file); + } + } + + return collect($documents); + } + + private function validate(SplFileInfo $file): bool + { + return $file->isFile() + && preg_match('/.pdf$/i', $file->getBasename()); } }