From 62146c4027e48cfbdb4f518de137de8430392e24 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 18 Feb 2018 22:29:36 +0000 Subject: Split client and manager --- src/Manager/Filesystem/CreateFile.php | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Manager/Filesystem/CreateFile.php (limited to 'src/Manager/Filesystem/CreateFile.php') diff --git a/src/Manager/Filesystem/CreateFile.php b/src/Manager/Filesystem/CreateFile.php new file mode 100644 index 0000000..ce9537f --- /dev/null +++ b/src/Manager/Filesystem/CreateFile.php @@ -0,0 +1,65 @@ + + */ +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 + */ + 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 + */ + public function execute() + { + touch($this->filename); + + $contents = $this->contents; + if ($contents) { + file_put_contents($this->filename, $contents); + } + } +} -- cgit v1.2.3