diff options
author | Fbenas <philbeansburton@gmail.com> | 2018-02-18 22:29:36 +0000 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2018-02-18 22:29:36 +0000 |
commit | 62146c4027e48cfbdb4f518de137de8430392e24 (patch) | |
tree | 791efd0bdccf4f23302428539a0fdc1047502646 /src/Client/Filesystem | |
parent | 7d2128f364c0e81a15653db2508d3e09b262eca1 (diff) |
Split client and manager
Diffstat (limited to 'src/Client/Filesystem')
-rw-r--r-- | src/Client/Filesystem/CreateDirectory.php | 49 | ||||
-rw-r--r-- | src/Client/Filesystem/CreateFile.php | 65 |
2 files changed, 114 insertions, 0 deletions
diff --git a/src/Client/Filesystem/CreateDirectory.php b/src/Client/Filesystem/CreateDirectory.php new file mode 100644 index 0000000..2621169 --- /dev/null +++ b/src/Client/Filesystem/CreateDirectory.php @@ -0,0 +1,49 @@ +<?php + +namespace App\Filesystem; + +use App\Task\TaskInterface; + +/** + * Class to create a new directory + * + * @author Phil Burton <phil@d3r.com> + */ +class CreateDirectory implements TaskInterface +{ + /** + * Directory to create + * + * @var string + */ + protected $directory; + + /** + * Check if the directory already exists + * + * @param string $filename + * @author Phil Burton <phil@d3r.com> + */ + public function __construct(string $directory) + { + $this->directory = $directory; + + if (!is_writable(dirname($directory))) { + throw new \Exception('Cannot create directory at: ' . $directory); + } + + if (file_exists($directory)) { + throw new \Exception('Directory already exists at: ' . $directory); + } + } + + /** + * Create the new directory + * + * @author Phil Burton <phil@d3r.com> + */ + public function execute() + { + mkdir($this->directory); + } +} diff --git a/src/Client/Filesystem/CreateFile.php b/src/Client/Filesystem/CreateFile.php new file mode 100644 index 0000000..ce9537f --- /dev/null +++ b/src/Client/Filesystem/CreateFile.php @@ -0,0 +1,65 @@ +<?php + +namespace App\Filesystem; + +use App\Task\TaskInterface; + +/** + * Class to create a new file + * + * @author Phil Burton <phil@d3r.com> + */ +class CreateFile implements TaskInterface +{ + /** + * Name of file to create + * + * @var string + */ + protected $filename; + + /** + * File contents to write + * + * @var string + */ + protected $contents; + + /** + * Check if the file already exists + * + * @param string $filename + * @author Phil Burton <phil@d3r.com> + */ + public function __construct(string $filename, $contents = false) + { + $this->filename = $filename; + + if ($contents) { + $this->contents = $contents; + } + + if (!is_writable(dirname($filename))) { + throw new \Exception('Cannot create file at: ' . $filename); + } + + if (file_exists($filename)) { + throw new \Exception('File already exists at: ' . $filename); + } + } + + /** + * Create the new file + * + * @author Phil Burton <phil@d3r.com> + */ + public function execute() + { + touch($this->filename); + + $contents = $this->contents; + if ($contents) { + file_put_contents($this->filename, $contents); + } + } +} |