reduce code complexity a bit

This commit is contained in:
Robin Appelman 2016-08-26 23:59:48 +02:00
commit 2601885185
2 changed files with 43 additions and 38 deletions

View file

@ -20,6 +20,8 @@ use Icewind\SMB\Exception\NotEmptyException;
use Icewind\SMB\Exception\NotFoundException; use Icewind\SMB\Exception\NotFoundException;
class Parser { class Parser {
const MSG_NOT_FOUND = 'Error opening local file ';
/** /**
* @var \Icewind\SMB\TimeZoneProvider * @var \Icewind\SMB\TimeZoneProvider
*/ */
@ -32,46 +34,45 @@ class Parser {
$this->timeZoneProvider = $timeZoneProvider; $this->timeZoneProvider = $timeZoneProvider;
} }
private function getErrorCode($line) {
$parts = explode(' ', $line);
foreach ($parts as $part) {
if (substr($part, 0, 9) === 'NT_STATUS') {
return $part;
}
}
return false;
}
public function checkForError($output, $path) { public function checkForError($output, $path) {
if (count($output) === 0) { if (strpos($output[0], 'does not exist')) {
return true; throw new NotFoundException($path);
} else { }
if (strpos($output[0], 'does not exist')) { $error = $this->getErrorCode($output[0]);
if (substr($output[0], 0, strlen(self::MSG_NOT_FOUND)) === self::MSG_NOT_FOUND) {
$localPath = substr($output[0], strlen(self::MSG_NOT_FOUND));
throw new InvalidResourceException('Failed opening local file "' . $localPath . '" for writing');
}
switch ($error) {
case ErrorCodes::PathNotFound:
case ErrorCodes::ObjectNotFound:
case ErrorCodes::NoSuchFile:
throw new NotFoundException($path); throw new NotFoundException($path);
} case ErrorCodes::NameCollision:
$parts = explode(' ', $output[0]); throw new AlreadyExistsException($path);
$error = false; case ErrorCodes::AccessDenied:
foreach ($parts as $part) { throw new AccessDeniedException($path);
if (substr($part, 0, 9) === 'NT_STATUS') { case ErrorCodes::DirectoryNotEmpty:
$error = $part; throw new NotEmptyException($path);
} case ErrorCodes::FileIsADirectory:
} case ErrorCodes::NotADirectory:
throw new InvalidTypeException($path);
$notFoundMsg = 'Error opening local file '; case ErrorCodes::SharingViolation:
if (substr($output[0], 0, strlen($notFoundMsg)) === $notFoundMsg) { throw new FileInUseException($path);
$localPath = substr($output[0], strlen($notFoundMsg)); default:
throw new InvalidResourceException('Failed opening local file "' . $localPath . '" for writing'); throw Exception::unknown($path, $error);
}
switch ($error) {
case ErrorCodes::PathNotFound:
case ErrorCodes::ObjectNotFound:
case ErrorCodes::NoSuchFile:
throw new NotFoundException($path);
case ErrorCodes::NameCollision:
throw new AlreadyExistsException($path);
case ErrorCodes::AccessDenied:
throw new AccessDeniedException($path);
case ErrorCodes::DirectoryNotEmpty:
throw new NotEmptyException($path);
case ErrorCodes::FileIsADirectory:
case ErrorCodes::NotADirectory:
throw new InvalidTypeException($path);
case ErrorCodes::SharingViolation:
throw new FileInUseException($path);
default:
throw Exception::unknown($path, $error);
}
} }
} }

View file

@ -401,7 +401,11 @@ class Share extends AbstractShare {
* @return bool * @return bool
*/ */
protected function parseOutput($lines, $path = '') { protected function parseOutput($lines, $path = '') {
return $this->parser->checkForError($lines, $path); if (count($lines) === 0) {
return true;
} else {
return $this->parser->checkForError($lines, $path);
}
} }
/** /**