Skip to content

Commit

Permalink
Reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jan 28, 2015
1 parent de7c899 commit 9534696
Showing 1 changed file with 37 additions and 45 deletions.
82 changes: 37 additions & 45 deletions framework/ManageSieve/lib/Horde/ManageSieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,7 @@ public function login(
$this->_params['euser'] = $euser;
}

if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
$this->_checkConnected();
if (self::STATE_AUTHENTICATED == $this->_state) {
throw new Exception('Already authenticated');
}
Expand Down Expand Up @@ -466,9 +464,7 @@ public function removeScript($scriptname)
*/
public function hasSpace($scriptname, $size)
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}
$this->_checkAuthenticated();

try {
$this->_doCmd(
Expand All @@ -489,9 +485,7 @@ public function hasSpace($scriptname, $size)
*/
public function getExtensions()
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
$this->_checkConnected();
return $this->_capability['extensions'];
}

Expand All @@ -505,9 +499,7 @@ public function getExtensions()
*/
public function hasExtension($extension)
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
$this->_checkConnected();

$extension = trim($this->_toUpper($extension));
if (is_array($this->_capability['extensions'])) {
Expand All @@ -529,9 +521,7 @@ public function hasExtension($extension)
*/
public function getAuthMechs()
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
$this->_checkConnected();
return $this->_capability['sasl'];
}

Expand All @@ -545,9 +535,7 @@ public function getAuthMechs()
*/
public function hasAuthMech($method)
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
$this->_checkConnected();

$method = trim($this->_toUpper($method));
if (is_array($this->_capability['sasl'])) {
Expand Down Expand Up @@ -728,9 +716,7 @@ protected function _authEXTERNAL($user, $pass, $euser)
*/
protected function _cmdDeleteScript($scriptname)
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}
$this->_checkAuthenticated();
$this->_doCmd(sprintf('DELETESCRIPT %s', $this->_escape($scriptname)));
}

Expand All @@ -744,14 +730,10 @@ protected function _cmdDeleteScript($scriptname)
*/
protected function _cmdGetScript($scriptname)
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}

$this->_checkAuthenticated();
$result = $this->_doCmd(
sprintf('GETSCRIPT %s', $this->_escape($scriptname))
);

return preg_replace('/^{[0-9]+}\r\n/', '', $result);
}

Expand All @@ -765,9 +747,7 @@ protected function _cmdGetScript($scriptname)
*/
protected function _cmdSetActive($scriptname)
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}
$this->_checkAuthenticated();
$this->_doCmd(sprintf('SETACTIVE %s', $this->_escape($scriptname)));
}

Expand All @@ -780,9 +760,7 @@ protected function _cmdSetActive($scriptname)
*/
protected function _cmdListScripts()
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}
$this->_checkAuthenticated();

$result = $this->_doCmd('LISTSCRIPTS');

Expand Down Expand Up @@ -812,17 +790,13 @@ protected function _cmdListScripts()
*/
protected function _cmdPutScript($scriptname, $scriptdata)
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}

$this->_checkAuthenticated();
$command = sprintf(
"PUTSCRIPT %s {%d+}\r\n%s",
$this->_escape($scriptname),
strlen($scriptdata),
$scriptdata
);

$this->_doCmd($command);
}

Expand All @@ -836,14 +810,10 @@ protected function _cmdPutScript($scriptname, $scriptdata)
*/
protected function _cmdLogout($sendLogoutCMD = true)
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}

$this->_checkConnected();
if ($sendLogoutCMD) {
$this->_doCmd('LOGOUT');
}

$this->_sock->close();
$this->_state = self::STATE_DISCONNECTED;
}
Expand All @@ -855,9 +825,7 @@ protected function _cmdLogout($sendLogoutCMD = true)
*/
protected function _cmdCapability()
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
$this->_checkConnected();
$result = $this->_doCmd('CAPABILITY');
$this->_parseCapability($result);
}
Expand Down Expand Up @@ -1115,6 +1083,30 @@ protected function _getBestAuthMethod($authmethod = null)
);
}

/**
* Asserts that the client is in disconnected state.
*
* @throws \Horde\ManageSieve\Exception
*/
protected function _checkConnected()
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
}

/**
* Asserts that the client is in authenticated state.
*
* @throws \Horde\ManageSieve\Exception
*/
protected function _checkAuthenticated()
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}
}

/**
* Locale independant strtoupper() implementation.
*
Expand Down

0 comments on commit 9534696

Please sign in to comment.