public function run() : \Jyxo\Beholder\Result
{
// The \Jyxo\Webdav\Client class is required
if (!class_exists(\Jyxo\Webdav\Client::class)) {
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, sprintf('Class %s missing', \Jyxo\Webdav\Client::class));
}
$random = md5(uniqid((string) time(), true));
$dir = trim($this->dir, '/');
if (!empty($dir)) {
$dir = '/' . $dir;
}
$path = $dir . '/beholder-' . $random . '.txt';
$content = $random;
// Status label
$serverUrl = $this->server;
if (!preg_match('~^https?://~', $this->server)) {
$serverUrl = 'http://' . $serverUrl;
}
$parsed = parse_url($serverUrl);
$host = $parsed['host'];
$port = !empty($parsed['port']) ? $parsed['port'] : 80;
$description = (false !== filter_var($host, FILTER_VALIDATE_IP) ? gethostbyaddr($host) : $host) . ':' . $port . $dir;
try {
$webdav = new \Jyxo\Webdav\Client([$serverUrl]);
foreach ($this->options as $name => $value) {
$webdav->setRequestOption($name, $value);
}
// Writing
$webdav->put($path, $content);
// Exists
if (!$webdav->exists($path)) {
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Exists error %s', $description));
}
// Reading
$readContent = $webdav->get($path);
if (strlen($readContent) !== strlen($content)) {
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description));
}
// Deleting
$webdav->unlink($path);
} catch (\Jyxo\Webdav\FileNotCreatedException $e) {
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Write error %s', $description));
} catch (\Jyxo\Webdav\FileNotExistException $e) {
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description));
} catch (\Jyxo\Webdav\FileNotDeletedException $e) {
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Delete error %s', $description));
} catch (\Jyxo\Webdav\Exception $e) {
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Error %s', $description));
}
// OK
return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $description);
}