add throws on error in uptimerobot call

This commit is contained in:
Giuseppe Nucifora 2022-07-25 10:49:13 +02:00
parent 947649e849
commit 58994915e8
2 changed files with 73 additions and 46 deletions

View File

@ -345,4 +345,9 @@ class Monitor
$this->createDatetime = $createDatetime; $this->createDatetime = $createDatetime;
return $this; return $this;
} }
public function getErrors()
{
}
} }

View File

@ -4,6 +4,7 @@ namespace Pn\UptimeRobotBundle\Service;
use Pn\UptimeRobotBundle\Model\AlertContact; use Pn\UptimeRobotBundle\Model\AlertContact;
use Pn\UptimeRobotBundle\Model\Monitor; use Pn\UptimeRobotBundle\Model\Monitor;
use Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException;
class UptimeRobotMonitorService extends UptimeRobotService class UptimeRobotMonitorService extends UptimeRobotService
{ {
@ -26,7 +27,8 @@ class UptimeRobotMonitorService extends UptimeRobotService
/** /**
* @param array $params * @param array $params
* @return array|bool * @return array
* @throws FailedRequestException
*/ */
public function getMonitors($params = []) public function getMonitors($params = [])
{ {
@ -34,10 +36,11 @@ class UptimeRobotMonitorService extends UptimeRobotService
try { try {
$jsonResponse = $this->client->perform(self::GET_MONITORS, $params); $jsonResponse = $this->client->perform(self::GET_MONITORS, $params);
} catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { } catch (FailedRequestException $exception) {
$this->logError($exception); $this->logError($exception);
throw $exception;
} }
$response = json_decode($jsonResponse); $response = json_decode($jsonResponse, false);
if ($response) { if ($response) {
switch ($response->stat) { switch ($response->stat) {
@ -48,19 +51,23 @@ class UptimeRobotMonitorService extends UptimeRobotService
$this->cachedMonitors[] = $monitor; $this->cachedMonitors[] = $monitor;
} }
break; break;
default:
return false;
break;
}
} }
return $this->cachedMonitors; return $this->cachedMonitors;
} }
throw new FailedRequestException($jsonResponse);
}
/** /**
* @param Monitor $monitor * @param Monitor $monitor
* @return bool|Monitor * @param array $alertContacts
* @param int $threshold
* @param int $recurrence
* @return Monitor|null
* @throws FailedRequestException
*/ */
public function create(Monitor $monitor, array $alertContacts, $threshold = 0, $recurrence = 0) public function create(Monitor $monitor, array $alertContacts, int $threshold = 0, int $recurrence = 0): ?Monitor
{ {
try { try {
$alertContactsString = ''; $alertContactsString = '';
@ -90,30 +97,33 @@ class UptimeRobotMonitorService extends UptimeRobotService
'mwindows' => '' 'mwindows' => ''
] ]
); );
} catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { } catch (FailedRequestException $exception) {
$this->logError($exception); $this->logError($exception);
throw $exception;
} }
$response = json_decode($jsonResponse); $response = json_decode($jsonResponse, false);
if ($response) { if ($response) {
switch ($response->stat) { switch ($response->stat) {
case 'ok': case 'ok':
$monitor->setId($response->monitor->id); $monitor->setId($response->monitor->id);
return $monitor; return $monitor;
break;
default: default:
break;
}
}
return null; return null;
}
}
throw new FailedRequestException($jsonResponse);
} }
/** /**
* @param Monitor $oldMonitor * @param Monitor $oldMonitor
* @param Monitor $monitor * @param Monitor $monitor
* @return bool|Monitor * @param array $alertContacts
* @param int $threshold
* @param int $recurrence
* @return Monitor|null
* @throws FailedRequestException
*/ */
public function update(Monitor $oldMonitor, Monitor $monitor, array $alertContacts, $threshold = 0, $recurrence = 0) public function update(Monitor $oldMonitor, Monitor $monitor, array $alertContacts, $threshold = 0, $recurrence = 0)
{ {
@ -152,27 +162,29 @@ class UptimeRobotMonitorService extends UptimeRobotService
self::EDIT_MONITOR, self::EDIT_MONITOR,
$params $params
); );
} catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { } catch (FailedRequestException $exception) {
$this->logError($exception); $this->logError($exception);
throw $exception;
} }
$response = json_decode($jsonResponse); $response = json_decode($jsonResponse, false);
if ($response) { if ($response) {
switch ($response->stat) { switch ($response->stat) {
case 'ok': case 'ok':
$monitor->setId($response->monitor->id); $monitor->setId($response->monitor->id);
return $monitor; return $monitor;
break;
default: default:
return false; return null;
break;
} }
} }
throw new FailedRequestException($jsonResponse);
} }
/** /**
* @param Monitor $monitor * @param Monitor $monitor
* @return bool|Monitor * @return Monitor|null
* @throws FailedRequestException
*/ */
public function delete(Monitor $monitor) public function delete(Monitor $monitor)
{ {
@ -180,27 +192,29 @@ class UptimeRobotMonitorService extends UptimeRobotService
$jsonResponse = $this->client->perform(self::DELETE_MONITOR, [ $jsonResponse = $this->client->perform(self::DELETE_MONITOR, [
'id' => $monitor->getId() 'id' => $monitor->getId()
]); ]);
} catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { } catch (FailedRequestException $exception) {
$this->logError($exception); $this->logError($exception);
throw $exception;
} }
$response = json_decode($jsonResponse); $response = json_decode($jsonResponse, false);
if ($response) { if ($response) {
switch ($response->stat) { switch ($response->stat) {
case 'ok': case 'ok':
return $monitor; return $monitor;
break;
default: default:
return false;
break;
}
}
return null; return null;
}
}
throw new FailedRequestException($jsonResponse);
} }
/** /**
* @param Monitor $monitor * @param Monitor $monitor
* @return bool|Monitor * @return Monitor|null
* @throws FailedRequestException
*/ */
public function reset(Monitor $monitor) public function reset(Monitor $monitor)
{ {
@ -208,30 +222,33 @@ class UptimeRobotMonitorService extends UptimeRobotService
$jsonResponse = $this->client->perform(self::RESET_MONITOR, [ $jsonResponse = $this->client->perform(self::RESET_MONITOR, [
'id' => $monitor->getId() 'id' => $monitor->getId()
]); ]);
} catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { } catch (FailedRequestException $exception) {
$this->logError($exception); $this->logError($exception);
throw $exception;
} }
$response = json_decode($jsonResponse); $response = json_decode($jsonResponse, false);
if ($response) { if ($response) {
switch ($response->stat) { switch ($response->stat) {
case 'ok': case 'ok':
return $monitor; return $monitor;
break;
default: default:
return false;
break;
}
}
return null; return null;
} }
}
throw new FailedRequestException($jsonResponse);
}
/** /**
* @param $id * @param null $id
* @param null $name
* @param null $url
* @param bool $forceRefresh * @param bool $forceRefresh
* @return mixed|Monitor|null * @return Monitor|null
* @throws FailedRequestException
*/ */
public function find($id = null, $name = null, $url = null, $forceRefresh = false) public function find($id = null, $name = null, $url = null, bool $forceRefresh = false)
{ {
if (empty($this->cachedMonitors) || $forceRefresh) { if (empty($this->cachedMonitors) || $forceRefresh) {
$this->getMonitors(); $this->getMonitors();
@ -252,9 +269,10 @@ class UptimeRobotMonitorService extends UptimeRobotService
* @param $url * @param $url
* @param int $type * @param int $type
* @param bool $forceRefresh * @param bool $forceRefresh
* @return mixed|Monitor|null * @return Monitor|null
* @throws FailedRequestException
*/ */
public function findOneByURLAndType($url, $type = Monitor::TYPE_HTTP, $forceRefresh = false) public function findOneByURLAndType($url, int $type = Monitor::TYPE_HTTP, bool $forceRefresh = false)
{ {
if (empty($this->cachedMonitors) || $forceRefresh) { if (empty($this->cachedMonitors) || $forceRefresh) {
$this->getMonitors(); $this->getMonitors();
@ -272,9 +290,13 @@ class UptimeRobotMonitorService extends UptimeRobotService
/** /**
* @param Monitor $monitor * @param Monitor $monitor
* @return bool|mixed|Monitor * @param array $alertContacts
* @param int $threshold
* @param int $recurrence
* @return null|Monitor
* @throws FailedRequestException
*/ */
public function createOrUpdate(Monitor $monitor, array $alertContacts, $threshold = 0, $recurrence = 0) public function createOrUpdate(Monitor $monitor, array $alertContacts, int $threshold = 0, int $recurrence = 0)
{ {
if ($monitor->isValidObjectForCreate()) { if ($monitor->isValidObjectForCreate()) {
@ -282,11 +304,11 @@ class UptimeRobotMonitorService extends UptimeRobotService
if ($foundMonitor instanceof Monitor) { if ($foundMonitor instanceof Monitor) {
return $this->update($foundMonitor, $monitor, $alertContacts, $threshold, $recurrence); return $this->update($foundMonitor, $monitor, $alertContacts, $threshold, $recurrence);
} else { }
return $this->create($monitor, $alertContacts, $threshold, $recurrence); return $this->create($monitor, $alertContacts, $threshold, $recurrence);
} }
} else {
return $monitor->getErrors(); return null;
}
} }
} }