Skip to content

Commit a7dd5ea

Browse files
committed
Made the rest of the CRUD Util methods throw RouterErrorException exceptions, each with its own code.
1 parent c36f52d commit a7dd5ea

File tree

2 files changed

+105
-20
lines changed

2 files changed

+105
-20
lines changed

src/PEAR2/Net/RouterOS/RouterErrorException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class RouterErrorException extends RuntimeException implements Exception
5454
const CODE_ADD_ERROR = 0x120001;
5555
const CODE_SET_ERROR = 0x120002;
5656
const CODE_REMOVE_ERROR = 0x120004;
57+
const CODE_ENABLE_ERROR = 0x120012;
58+
const CODE_DISABLE_ERROR = 0x120022;
59+
const CODE_COMMENT_ERROR = 0x120042;
60+
const CODE_UNSET_ERROR = 0x120082;
61+
const CODE_MOVE_ERROR = 0x120107;
5762
const CODE_SCRIPT_GET_ERROR = 0x210001;
5863
const CODE_SCRIPT_ADD_ERROR = 0x220001;
5964
const CODE_SCRIPT_REMOVE_ERROR = 0x220004;

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 100 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,19 @@ public function setMenu($newMenu)
128128
} else {
129129
$newMenu = substr(
130130
$menuRequest->setCommand(
131-
'/' . str_replace('/', ' ', substr($this->menu, 1)) .
132-
' ' . str_replace('/', ' ', $newMenu) . ' ?'
131+
'/' .
132+
str_replace('/', ' ', (string)substr($this->menu, 1)) .
133+
' ' .
134+
str_replace('/', ' ', $newMenu)
135+
. ' ?'
133136
)->getCommand(),
134137
1,
135138
-2/*strlen('/?')*/
136139
);
137140
if ('' !== $newMenu) {
138141
$this->menu = '/' . $newMenu;
142+
} else {
143+
$this->menu = '';
139144
}
140145
}
141146
}
@@ -598,12 +603,24 @@ public function get($number, $valueName = null)
598603
* See {@link static::find()} for a description of what criteria are
599604
* accepted.
600605
*
601-
* @return ResponseCollection returns the response collection, allowing you
602-
* to inspect errors, if any.
606+
* @return ResponseCollection Returns the response collection, allowing you
607+
* to inspect the output. Current RouterOS versions don't return
608+
* anything useful, but if future ones do, you can read it right away.
609+
*
610+
* @throws RouterErrorException When the router returns one or more errors.
603611
*/
604612
public function enable()
605613
{
606-
return $this->doBulk('enable', func_get_args());
614+
$responses = $this->doBulk('enable', func_get_args());
615+
if (count($responses->getAllOfType(Response::TYPE_ERROR)) > 0) {
616+
throw new RouterErrorException(
617+
'Error when enabling items',
618+
RouterErrorException::CODE_ENABLE_ERROR,
619+
null,
620+
$responses
621+
);
622+
}
623+
return $responses;
607624
}
608625

609626
/**
@@ -615,11 +632,23 @@ public function enable()
615632
* accepted.
616633
*
617634
* @return ResponseCollection Returns the response collection, allowing you
618-
* to inspect errors, if any.
635+
* to inspect the output. Current RouterOS versions don't return
636+
* anything useful, but if future ones do, you can read it right away.
637+
*
638+
* @throws RouterErrorException When the router returns one or more errors.
619639
*/
620640
public function disable()
621641
{
622-
return $this->doBulk('disable', func_get_args());
642+
$responses = $this->doBulk('disable', func_get_args());
643+
if (count($responses->getAllOfType(Response::TYPE_ERROR)) > 0) {
644+
throw new RouterErrorException(
645+
'Error when disabling items',
646+
RouterErrorException::CODE_DISABLE_ERROR,
647+
null,
648+
$responses
649+
);
650+
}
651+
return $responses;
623652
}
624653

625654
/**
@@ -631,13 +660,24 @@ public function disable()
631660
* accepted.
632661
*
633662
* @return ResponseCollection Returns the response collection, allowing you
634-
* to inspect errors, if any.
663+
* to inspect the output. Current RouterOS versions don't return
664+
* anything useful, but if future ones do, you can read it right away.
665+
*
666+
* @throws RouterErrorException When the router returns one or more errors.
635667
*/
636668
public function remove()
637669
{
638-
$result = $this->doBulk('remove', func_get_args());
670+
$responses = $this->doBulk('remove', func_get_args());
639671
$this->clearIdCache();
640-
return $result;
672+
if (count($responses->getAllOfType(Response::TYPE_ERROR)) > 0) {
673+
throw new RouterErrorException(
674+
'Error when removing items',
675+
RouterErrorException::CODE_REMOVE_ERROR,
676+
null,
677+
$responses
678+
);
679+
}
680+
return $responses;
641681
}
642682

643683
/**
@@ -661,14 +701,26 @@ public function remove()
661701
* string.
662702
*
663703
* @return ResponseCollection Returns the response collection, allowing you
664-
* to inspect errors, if any.
704+
* to inspect the output. Current RouterOS versions don't return
705+
* anything useful, but if future ones do, you can read it right away.
706+
*
707+
* @throws RouterErrorException When the router returns one or more errors.
665708
*/
666709
public function comment($numbers, $comment)
667710
{
668711
$commentRequest = new Request($this->menu . '/comment');
669712
$commentRequest->setArgument('comment', $comment);
670713
$commentRequest->setArgument('numbers', $this->find($numbers));
671-
return $this->client->sendSync($commentRequest);
714+
$responses = $this->client->sendSync($commentRequest);
715+
if (count($responses->getAllOfType(Response::TYPE_ERROR)) > 0) {
716+
throw new RouterErrorException(
717+
'Error when commenting items',
718+
RouterErrorException::CODE_COMMENT_ERROR,
719+
null,
720+
$responses
721+
);
722+
}
723+
return $responses;
672724
}
673725

674726
/**
@@ -704,7 +756,16 @@ public function set($numbers, array $newValues)
704756
if (null !== $numbers) {
705757
$setRequest->setArgument('numbers', $this->find($numbers));
706758
}
707-
return $this->client->sendSync($setRequest);
759+
$responses = $this->client->sendSync($setRequest);
760+
if (count($responses->getAllOfType(Response::TYPE_ERROR)) > 0) {
761+
throw new RouterErrorException(
762+
'Error when setting items',
763+
RouterErrorException::CODE_SET_ERROR,
764+
null,
765+
$responses
766+
);
767+
}
768+
return $responses;
708769
}
709770

710771
/**
@@ -744,10 +805,19 @@ public function edit($numbers, $valueName, $newValue)
744805
public function unsetValue($numbers, $valueName)
745806
{
746807
$unsetRequest = new Request($this->menu . '/unset');
747-
return $this->client->sendSync(
808+
$responses = $this->client->sendSync(
748809
$unsetRequest->setArgument('numbers', $this->find($numbers))
749810
->setArgument('value-name', $valueName)
750811
);
812+
if (count($responses->getAllOfType(Response::TYPE_ERROR)) > 0) {
813+
throw new RouterErrorException(
814+
'Error when unsetting value of items',
815+
RouterErrorException::CODE_UNSET_ERROR,
816+
null,
817+
$responses
818+
);
819+
}
820+
return $responses;
751821
}
752822

753823
/**
@@ -842,7 +912,16 @@ public function move($numbers, $destination = null)
842912
$moveRequest->setArgument('destination', $destination);
843913
}
844914
$this->clearIdCache();
845-
return $this->client->sendSync($moveRequest);
915+
$responses = $this->client->sendSync($moveRequest);
916+
if (count($responses->getAllOfType(Response::TYPE_ERROR)) > 0) {
917+
throw new RouterErrorException(
918+
'Error when moving items',
919+
RouterErrorException::CODE_MOVE_ERROR,
920+
null,
921+
$responses
922+
);
923+
}
924+
return $responses;
846925
}
847926

848927
/**
@@ -1069,18 +1148,19 @@ public function fileGetContents($filename, $tmpScriptName = null)
10691148
/**
10701149
* Performs an action on a bulk of items at the current menu.
10711150
*
1072-
* @param string $what What action to perform.
1073-
* @param array $args Zero or more arguments can be specified, each being
1074-
* a criteria. If zero arguments are specified, removes all items.
1151+
* @param string $command What command to perform.
1152+
* @param array $args Zero or more arguments can be specified,
1153+
* each being a criteria.
1154+
* If zero arguments are specified, matches all items.
10751155
* See {@link static::find()} for a description of what criteria are
10761156
* accepted.
10771157
*
10781158
* @return ResponseCollection Returns the response collection, allowing you
10791159
* to inspect errors, if any.
10801160
*/
1081-
protected function doBulk($what, array $args = array())
1161+
protected function doBulk($command, array $args = array())
10821162
{
1083-
$bulkRequest = new Request($this->menu . '/' . $what);
1163+
$bulkRequest = new Request("{$this->menu}/{$command}");
10841164
$bulkRequest->setArgument(
10851165
'numbers',
10861166
call_user_func_array(array($this, 'find'), $args)

0 commit comments

Comments
 (0)