diff --git a/.idea/php.xml b/.idea/php.xml index 949b6a9..a6bc50b 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -35,7 +35,7 @@ - + \ No newline at end of file diff --git a/composer.json b/composer.json index d4dfeee..d7859ca 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "minimum-stability": "stable", "require": { - "php": ">=7.1", + "php": ">=7.2", "symfony/framework-bundle": ">=4.3", "symfony/flex": "^1.2", "symfony/dependency-injection": "^v4.4", diff --git a/composer.lock b/composer.lock index 687a7ee..7cf84ee 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bae2c32dc5a579e2e0c95f32dcf3c86e", + "content-hash": "d5477f1164051aeeb67150f3f04a753b", "packages": [ { "name": "geocodio/uptimerobot-api-client", @@ -448,16 +448,16 @@ }, { "name": "symfony/debug", - "version": "v4.4.41", + "version": "v4.4.44", "source": { "type": "git", "url": "https://github.com/symfony/debug.git", - "reference": "6637e62480b60817b9a6984154a533e8e64c6bd5" + "reference": "1a692492190773c5310bc7877cb590c04c2f05be" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/debug/zipball/6637e62480b60817b9a6984154a533e8e64c6bd5", - "reference": "6637e62480b60817b9a6984154a533e8e64c6bd5", + "url": "https://api.github.com/repos/symfony/debug/zipball/1a692492190773c5310bc7877cb590c04c2f05be", + "reference": "1a692492190773c5310bc7877cb590c04c2f05be", "shasum": "" }, "require": { @@ -496,7 +496,7 @@ "description": "Provides tools to ease debugging PHP code", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/debug/tree/v4.4.41" + "source": "https://github.com/symfony/debug/tree/v4.4.44" }, "funding": [ { @@ -513,7 +513,7 @@ } ], "abandoned": "symfony/error-handler", - "time": "2022-04-12T15:19:55+00:00" + "time": "2022-07-28T16:29:46+00:00" }, { "name": "symfony/dependency-injection", @@ -603,7 +603,7 @@ }, { "name": "symfony/deprecation-contracts", - "version": "v2.5.1", + "version": "v2.5.2", "source": { "type": "git", "url": "https://github.com/symfony/deprecation-contracts.git", @@ -650,7 +650,7 @@ "description": "A generic function and convention to trigger deprecation notices", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.1" + "source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.2" }, "funding": [ { @@ -670,16 +670,16 @@ }, { "name": "symfony/error-handler", - "version": "v4.4.41", + "version": "v4.4.44", "source": { "type": "git", "url": "https://github.com/symfony/error-handler.git", - "reference": "529feb0e03133dbd5fd3707200147cc4903206da" + "reference": "be731658121ef2d8be88f3a1ec938148a9237291" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/error-handler/zipball/529feb0e03133dbd5fd3707200147cc4903206da", - "reference": "529feb0e03133dbd5fd3707200147cc4903206da", + "url": "https://api.github.com/repos/symfony/error-handler/zipball/be731658121ef2d8be88f3a1ec938148a9237291", + "reference": "be731658121ef2d8be88f3a1ec938148a9237291", "shasum": "" }, "require": { @@ -718,7 +718,7 @@ "description": "Provides tools to manage errors and ease debugging PHP code", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/error-handler/tree/v4.4.41" + "source": "https://github.com/symfony/error-handler/tree/v4.4.44" }, "funding": [ { @@ -734,20 +734,20 @@ "type": "tidelift" } ], - "time": "2022-04-12T15:19:55+00:00" + "time": "2022-07-28T16:29:46+00:00" }, { "name": "symfony/event-dispatcher", - "version": "v4.4.42", + "version": "v4.4.44", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "708e761740c16b02c86e3f0c932018a06b895d40" + "reference": "1e866e9e5c1b22168e0ce5f0b467f19bba61266a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/708e761740c16b02c86e3f0c932018a06b895d40", - "reference": "708e761740c16b02c86e3f0c932018a06b895d40", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/1e866e9e5c1b22168e0ce5f0b467f19bba61266a", + "reference": "1e866e9e5c1b22168e0ce5f0b467f19bba61266a", "shasum": "" }, "require": { @@ -802,7 +802,7 @@ "description": "Provides tools that allow your application components to communicate with each other by dispatching events and listening to them", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/event-dispatcher/tree/v4.4.42" + "source": "https://github.com/symfony/event-dispatcher/tree/v4.4.44" }, "funding": [ { @@ -818,11 +818,11 @@ "type": "tidelift" } ], - "time": "2022-05-05T15:33:49+00:00" + "time": "2022-07-20T09:59:04+00:00" }, { "name": "symfony/event-dispatcher-contracts", - "version": "v1.1.12", + "version": "v1.1.13", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher-contracts.git", @@ -881,7 +881,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/event-dispatcher-contracts/tree/v1.1.12" + "source": "https://github.com/symfony/event-dispatcher-contracts/tree/v1.1.13" }, "funding": [ { @@ -1239,16 +1239,16 @@ }, { "name": "symfony/http-client-contracts", - "version": "v2.5.1", + "version": "v2.5.2", "source": { "type": "git", "url": "https://github.com/symfony/http-client-contracts.git", - "reference": "1a4f708e4e87f335d1b1be6148060739152f0bd5" + "reference": "ba6a9f0e8f3edd190520ee3b9a958596b6ca2e70" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client-contracts/zipball/1a4f708e4e87f335d1b1be6148060739152f0bd5", - "reference": "1a4f708e4e87f335d1b1be6148060739152f0bd5", + "url": "https://api.github.com/repos/symfony/http-client-contracts/zipball/ba6a9f0e8f3edd190520ee3b9a958596b6ca2e70", + "reference": "ba6a9f0e8f3edd190520ee3b9a958596b6ca2e70", "shasum": "" }, "require": { @@ -1297,7 +1297,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/http-client-contracts/tree/v2.5.1" + "source": "https://github.com/symfony/http-client-contracts/tree/v2.5.2" }, "funding": [ { @@ -1313,20 +1313,20 @@ "type": "tidelift" } ], - "time": "2022-03-13T20:07:29+00:00" + "time": "2022-04-12T15:48:08+00:00" }, { "name": "symfony/http-foundation", - "version": "v5.4.10", + "version": "v5.4.20", "source": { "type": "git", "url": "https://github.com/symfony/http-foundation.git", - "reference": "e7793b7906f72a8cc51054fbca9dcff7a8af1c1e" + "reference": "d0435363362a47c14e9cf50663cb8ffbf491875a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-foundation/zipball/e7793b7906f72a8cc51054fbca9dcff7a8af1c1e", - "reference": "e7793b7906f72a8cc51054fbca9dcff7a8af1c1e", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/d0435363362a47c14e9cf50663cb8ffbf491875a", + "reference": "d0435363362a47c14e9cf50663cb8ffbf491875a", "shasum": "" }, "require": { @@ -1338,8 +1338,11 @@ "require-dev": { "predis/predis": "~1.0", "symfony/cache": "^4.4|^5.0|^6.0", + "symfony/dependency-injection": "^5.4|^6.0", "symfony/expression-language": "^4.4|^5.0|^6.0", - "symfony/mime": "^4.4|^5.0|^6.0" + "symfony/http-kernel": "^5.4.12|^6.0.12|^6.1.4", + "symfony/mime": "^4.4|^5.0|^6.0", + "symfony/rate-limiter": "^5.2|^6.0" }, "suggest": { "symfony/mime": "To use the file extension guesser" @@ -1370,7 +1373,7 @@ "description": "Defines an object-oriented layer for the HTTP specification", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/http-foundation/tree/v5.4.10" + "source": "https://github.com/symfony/http-foundation/tree/v5.4.20" }, "funding": [ { @@ -1386,20 +1389,20 @@ "type": "tidelift" } ], - "time": "2022-06-19T13:13:40+00:00" + "time": "2023-01-29T11:11:52+00:00" }, { "name": "symfony/http-kernel", - "version": "v4.4.43", + "version": "v4.4.50", "source": { "type": "git", "url": "https://github.com/symfony/http-kernel.git", - "reference": "c4c33fb9203e6f166ac0f318ce34e00686702522" + "reference": "aa6df6c045f034aa13ac752fc234bb300b9488ef" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-kernel/zipball/c4c33fb9203e6f166ac0f318ce34e00686702522", - "reference": "c4c33fb9203e6f166ac0f318ce34e00686702522", + "url": "https://api.github.com/repos/symfony/http-kernel/zipball/aa6df6c045f034aa13ac752fc234bb300b9488ef", + "reference": "aa6df6c045f034aa13ac752fc234bb300b9488ef", "shasum": "" }, "require": { @@ -1474,7 +1477,7 @@ "description": "Provides a structured process for converting a Request into a Response", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/http-kernel/tree/v4.4.43" + "source": "https://github.com/symfony/http-kernel/tree/v4.4.50" }, "funding": [ { @@ -1490,20 +1493,20 @@ "type": "tidelift" } ], - "time": "2022-06-26T16:51:30+00:00" + "time": "2023-02-01T08:01:31+00:00" }, { "name": "symfony/polyfill-ctype", - "version": "v1.26.0", + "version": "v1.27.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4" + "reference": "5bbc823adecdae860bb64756d639ecfec17b050a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", - "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/5bbc823adecdae860bb64756d639ecfec17b050a", + "reference": "5bbc823adecdae860bb64756d639ecfec17b050a", "shasum": "" }, "require": { @@ -1518,7 +1521,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.26-dev" + "dev-main": "1.27-dev" }, "thanks": { "name": "symfony/polyfill", @@ -1556,7 +1559,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.26.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.27.0" }, "funding": [ { @@ -1572,20 +1575,20 @@ "type": "tidelift" } ], - "time": "2022-05-24T11:49:31+00:00" + "time": "2022-11-03T14:55:06+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.26.0", + "version": "v1.27.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e" + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e", - "reference": "9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534", "shasum": "" }, "require": { @@ -1600,7 +1603,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.26-dev" + "dev-main": "1.27-dev" }, "thanks": { "name": "symfony/polyfill", @@ -1639,7 +1642,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.26.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.27.0" }, "funding": [ { @@ -1655,20 +1658,20 @@ "type": "tidelift" } ], - "time": "2022-05-24T11:49:31+00:00" + "time": "2022-11-03T14:55:06+00:00" }, { "name": "symfony/polyfill-php73", - "version": "v1.26.0", + "version": "v1.27.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php73.git", - "reference": "e440d35fa0286f77fb45b79a03fedbeda9307e85" + "reference": "9e8ecb5f92152187c4799efd3c96b78ccab18ff9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/e440d35fa0286f77fb45b79a03fedbeda9307e85", - "reference": "e440d35fa0286f77fb45b79a03fedbeda9307e85", + "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/9e8ecb5f92152187c4799efd3c96b78ccab18ff9", + "reference": "9e8ecb5f92152187c4799efd3c96b78ccab18ff9", "shasum": "" }, "require": { @@ -1677,7 +1680,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.26-dev" + "dev-main": "1.27-dev" }, "thanks": { "name": "symfony/polyfill", @@ -1718,7 +1721,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php73/tree/v1.26.0" + "source": "https://github.com/symfony/polyfill-php73/tree/v1.27.0" }, "funding": [ { @@ -1734,20 +1737,20 @@ "type": "tidelift" } ], - "time": "2022-05-24T11:49:31+00:00" + "time": "2022-11-03T14:55:06+00:00" }, { "name": "symfony/polyfill-php80", - "version": "v1.26.0", + "version": "v1.27.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "cfa0ae98841b9e461207c13ab093d76b0fa7bace" + "reference": "7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/cfa0ae98841b9e461207c13ab093d76b0fa7bace", - "reference": "cfa0ae98841b9e461207c13ab093d76b0fa7bace", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936", + "reference": "7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936", "shasum": "" }, "require": { @@ -1756,7 +1759,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.26-dev" + "dev-main": "1.27-dev" }, "thanks": { "name": "symfony/polyfill", @@ -1801,7 +1804,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.26.0" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.27.0" }, "funding": [ { @@ -1817,7 +1820,7 @@ "type": "tidelift" } ], - "time": "2022-05-10T07:21:04+00:00" + "time": "2022-11-03T14:55:06+00:00" }, { "name": "symfony/polyfill-php81", @@ -2166,16 +2169,16 @@ }, { "name": "symfony/var-dumper", - "version": "v5.4.9", + "version": "v5.4.19", "source": { "type": "git", "url": "https://github.com/symfony/var-dumper.git", - "reference": "af52239a330fafd192c773795520dc2dd62b5657" + "reference": "2944bbc23f5f8da2b962fbcbf7c4a6109b2f4b7b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/var-dumper/zipball/af52239a330fafd192c773795520dc2dd62b5657", - "reference": "af52239a330fafd192c773795520dc2dd62b5657", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/2944bbc23f5f8da2b962fbcbf7c4a6109b2f4b7b", + "reference": "2944bbc23f5f8da2b962fbcbf7c4a6109b2f4b7b", "shasum": "" }, "require": { @@ -2235,7 +2238,7 @@ "dump" ], "support": { - "source": "https://github.com/symfony/var-dumper/tree/v5.4.9" + "source": "https://github.com/symfony/var-dumper/tree/v5.4.19" }, "funding": [ { @@ -2251,7 +2254,7 @@ "type": "tidelift" } ], - "time": "2022-05-21T10:24:18+00:00" + "time": "2023-01-16T10:52:33+00:00" }, { "name": "symfony/var-exporter", @@ -2839,7 +2842,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.1", + "php": ">=7.2", "ext-json": "*" }, "platform-dev": [], diff --git a/src/Model/Monitor.php b/src/Model/Monitor.php index d049185..8900de8 100644 --- a/src/Model/Monitor.php +++ b/src/Model/Monitor.php @@ -26,6 +26,8 @@ class Monitor const KEYWORD_TYPE_EXIST = 1; const KEYWORD_TYPE_NOT_EXIST = 2; + const ALLOWED_KEYWORK_TYPES = [self::KEYWORD_TYPE_EXIST, self::KEYWORD_TYPE_NOT_EXIST]; + /** @var int */ private $id; @@ -41,7 +43,7 @@ class Monitor /** @var string */ private $subType; - /** @var string */ + /** @var int */ private $keywordType; /** @var string */ @@ -76,18 +78,18 @@ class Monitor $monitor->setId($monitorObj->id); $monitor->setFriendlyName($monitorObj->friendly_name); $monitor->setUrl($monitorObj->url); - $monitor->setType($monitorObj->type); + $monitor->setType((int)$monitorObj->type); $monitor->setSubType($monitorObj->sub_type); - $monitor->setKeywordType($monitorObj->keyword_type); + $monitor->setKeywordType((int)$monitorObj->keyword_type); $monitor->setKeywordValue($monitorObj->keyword_value); $monitor->setHttpUsername($monitorObj->http_username); $monitor->setHttpPassword($monitorObj->http_password); $monitor->setPort($monitorObj->port); - $monitor->setInterval($monitorObj->interval); - $monitor->setStatus($monitorObj->status); + $monitor->setInterval((int)$monitorObj->interval); + $monitor->setStatus((int)$monitorObj->status); $date = new \DateTime(); - $date->setTimestamp($monitorObj->create_datetime); + $date->setTimestamp((int)$monitorObj->create_datetime); $monitor->setCreateDatetime($date); return $monitor; @@ -201,7 +203,7 @@ class Monitor /** * @return string */ - public function getKeywordType(): ?string + public function getKeywordType(): ?int { return $this->keywordType; } @@ -210,9 +212,11 @@ class Monitor * @param string $keywordType * @return Monitor */ - public function setKeywordType(?string $keywordType): Monitor + public function setKeywordType(?int $keywordType): Monitor { - $this->keywordType = $keywordType; + if (in_array($keywordType, self::ALLOWED_KEYWORK_TYPES, true)){ + $this->keywordType = $keywordType; + } return $this; } @@ -341,4 +345,9 @@ class Monitor $this->createDatetime = $createDatetime; return $this; } + + public function getErrors() + { + + } } \ No newline at end of file diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 9341d92..dfb4d23 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -11,3 +11,7 @@ services: arguments: - '%uptime_robot.credentials.api_key%' - '@Symfony\Component\Serializer\SerializerInterface' + calls: + - setInterval: ['%uptime_robot.configurations.interval%'] + - setAlertContactsString: ['%uptime_robot.configurations.alert_contacts%'] + diff --git a/src/Service/UptimeRobotAlertContacsService.php b/src/Service/UptimeRobotAlertContacsService.php index b589098..e14171e 100644 --- a/src/Service/UptimeRobotAlertContacsService.php +++ b/src/Service/UptimeRobotAlertContacsService.php @@ -26,19 +26,21 @@ class UptimeRobotAlertContacsService extends UptimeRobotService $response = json_decode($jsonResponse); - switch ($response->stat) { - case 'ok': + if ($response) { + switch ($response->stat) { + case 'ok': - foreach ($response->alert_contacts as $alert_contact) { + foreach ($response->alert_contacts as $alert_contact) { - $alertContact = AlertContact::getAlertContactFromResponse($alert_contact); + $alertContact = AlertContact::getAlertContactFromResponse($alert_contact); - $this->cachedAlertContacts[] = $alertContact; - } + $this->cachedAlertContacts[] = $alertContact; + } - break; - default: - break; + break; + default: + break; + } } @@ -66,17 +68,18 @@ class UptimeRobotAlertContacsService extends UptimeRobotService } $response = json_decode($jsonResponse); - - switch ($response->stat) { - case 'ok': - $alertContact->setId($response->alertcontact->id); - return $alertContact; - break; - default: - return false; - break; + if ($response) { + switch ($response->stat) { + case 'ok': + $alertContact->setId($response->alertcontact->id); + return $alertContact; + break; + default: + return false; + break; + } } - + return null; } /** @@ -102,14 +105,16 @@ class UptimeRobotAlertContacsService extends UptimeRobotService $response = json_decode($jsonResponse); - switch ($response->stat) { - case 'ok': - $alertContact->setId($response->alert_contact->id); - return $alertContact; - break; - default: - return false; - break; + if ($response) { + switch ($response->stat) { + case 'ok': + $alertContact->setId($response->alert_contact->id); + return $alertContact; + break; + default: + return null; + break; + } } } @@ -128,15 +133,17 @@ class UptimeRobotAlertContacsService extends UptimeRobotService } $response = json_decode($jsonResponse); - - switch ($response->stat) { - case 'ok': - return $alertContact; - break; - default: - return false; - break; + if ($response) { + switch ($response->stat) { + case 'ok': + return $alertContact; + break; + default: + return null; + break; + } } + return null; } /** @@ -152,7 +159,7 @@ class UptimeRobotAlertContacsService extends UptimeRobotService /** @var AlertContact $alertContact */ foreach ($this->cachedAlertContacts as $alertContact) { - if ($alertContact->getId() === $id || $alertContact->getFriendlyName() === $name || $alertContact->getValue() === $value ) { + if ($alertContact->getId() === $id || $alertContact->getFriendlyName() === $name || $alertContact->getValue() === $value) { return $alertContact; } } diff --git a/src/Service/UptimeRobotApiService.php b/src/Service/UptimeRobotApiService.php index b30f321..ae2f1bc 100644 --- a/src/Service/UptimeRobotApiService.php +++ b/src/Service/UptimeRobotApiService.php @@ -2,6 +2,7 @@ namespace Pn\UptimeRobotBundle\Service; +use Pn\UptimeRobotBundle\Model\AlertContact; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Serializer\SerializerInterface; use Vdhicts\UptimeRobot\Client\Client; @@ -9,6 +10,14 @@ use Vdhicts\UptimeRobot\Client\Configuration; class UptimeRobotApiService { + private $apiKey; + + private $interval; + + private $alertContactsString; + + private $alertContacts; + /** @var $client */ protected $client; @@ -28,10 +37,30 @@ class UptimeRobotApiService public function __construct(string $apiKey, SerializerInterface $serializer) { - $configuration = new Configuration($apiKey); + $this->apiKey = $apiKey; + $configuration = new Configuration($this->apiKey); $this->client = new Client($configuration); $this->serializer = $serializer; + $this->alertContacts = []; + } + + /** + * Check if apikey is setted and suppose that api is valid + * + * @return bool + */ + public function isActive() + { + return !empty($this->apiKey); + } + + /** + * @return string + */ + public function getApiKey(): string + { + return $this->apiKey; } @@ -50,13 +79,65 @@ class UptimeRobotApiService return $this; } + /** + * @return mixed + */ + public function getInterval() + { + return $this->interval; + } + + /** + * @param mixed $interval + */ + public function setInterval($interval): void + { + $this->interval = $interval; + } + + /** + * @param mixed $alertContactsString + */ + public function setAlertContactsString(?string $alertContactsString): void + { + $this->alertContactsString = $alertContactsString; + if (!empty($alertContactsString) && $this->isActive()) { + $alertContacts = explode(',', $alertContactsString); + + foreach ($alertContacts as $alertContactName) { + if (!empty($alertContactName)) { + $alertContact = $this->getAlertContactService()->findbyName($alertContactName); + if ($alertContact instanceof AlertContact) { + $this->alertContacts[] = $alertContact; + } + } + } + } + } + + /** + * @return mixed + */ + public function getAlertContacts() + { + return $this->alertContacts; + } + + /** + * @param mixed $alertContacts + */ + public function setAlertContacts($alertContacts): void + { + $this->alertContacts = $alertContacts; + } + /** * @return UptimeRobotMonitorService */ public function getMonitorService(): UptimeRobotMonitorService { if (null === $this->monitorService) { - $this->monitorService = New UptimeRobotMonitorService($this->client); + $this->monitorService = new UptimeRobotMonitorService($this->client); if ($this->io instanceof SymfonyStyle) { $this->monitorService->setIo($this->io); } @@ -70,7 +151,7 @@ class UptimeRobotApiService public function getAlertContactService(): UptimeRobotAlertContacsService { if (null === $this->alertContactService) { - $this->alertContactService = New UptimeRobotAlertContacsService($this->client); + $this->alertContactService = new UptimeRobotAlertContacsService($this->client); if ($this->io instanceof SymfonyStyle) { $this->alertContactService->setIo($this->io); } diff --git a/src/Service/UptimeRobotMonitorService.php b/src/Service/UptimeRobotMonitorService.php index 2916805..78aa6d9 100644 --- a/src/Service/UptimeRobotMonitorService.php +++ b/src/Service/UptimeRobotMonitorService.php @@ -4,6 +4,7 @@ namespace Pn\UptimeRobotBundle\Service; use Pn\UptimeRobotBundle\Model\AlertContact; use Pn\UptimeRobotBundle\Model\Monitor; +use Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException; class UptimeRobotMonitorService extends UptimeRobotService { @@ -26,7 +27,8 @@ class UptimeRobotMonitorService extends UptimeRobotService /** * @param array $params - * @return array|bool + * @return array + * @throws FailedRequestException */ public function getMonitors($params = []) { @@ -34,32 +36,38 @@ class UptimeRobotMonitorService extends UptimeRobotService try { $jsonResponse = $this->client->perform(self::GET_MONITORS, $params); - } catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { + } catch (FailedRequestException $exception) { $this->logError($exception); + throw $exception; } - $response = json_decode($jsonResponse); + $response = json_decode($jsonResponse, false); - switch ($response->stat) { - case 'ok': - foreach ($response->monitors as $monitorObj) { - $monitor = Monitor::getMonitorFromResponse($monitorObj); + if ($response) { + switch ($response->stat) { + case 'ok': + foreach ($response->monitors as $monitorObj) { + $monitor = Monitor::getMonitorFromResponse($monitorObj); - $this->cachedMonitors[] = $monitor; - } - break; - default: - return false; - break; + $this->cachedMonitors[] = $monitor; + } + break; + } + + return $this->cachedMonitors; } - return $this->cachedMonitors; + throw new FailedRequestException($jsonResponse); } /** * @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 { $alertContactsString = ''; @@ -89,29 +97,33 @@ class UptimeRobotMonitorService extends UptimeRobotService 'mwindows' => '' ] ); - } catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { + } catch (FailedRequestException $exception) { $this->logError($exception); + throw $exception; } - $response = json_decode($jsonResponse); + $response = json_decode($jsonResponse, false); - - switch ($response->stat) { - case 'ok': - $monitor->setId($response->monitor->id); - return $monitor; - break; - default: - return false; - break; + if ($response) { + switch ($response->stat) { + case 'ok': + $monitor->setId($response->monitor->id); + return $monitor; + default: + return null; + } } - + throw new FailedRequestException($jsonResponse); } /** * @param Monitor $oldMonitor * @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) { @@ -138,10 +150,11 @@ class UptimeRobotMonitorService extends UptimeRobotService 'interval' => $monitor->getInterval(), 'http_username' => $monitor->getHttpUsername(), 'http_password' => $monitor->getHttpPassword(), - 'mwindows' => '' + 'mwindows' => '', + 'status' => $monitor->getStatus() ]; - if (!empty($alertContactsString)){ + if (!empty($alertContactsString)) { $params['alert_contacts'] = $alertContactsString; } @@ -149,26 +162,29 @@ class UptimeRobotMonitorService extends UptimeRobotService self::EDIT_MONITOR, $params ); - } catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { + } catch (FailedRequestException $exception) { $this->logError($exception); + throw $exception; } - $response = json_decode($jsonResponse); + $response = json_decode($jsonResponse, false); + if ($response) { + switch ($response->stat) { + case 'ok': + $monitor->setId($response->monitor->id); + return $monitor; + default: + return null; - switch ($response->stat) { - case 'ok': - $monitor->setId($response->monitor->id); - return $monitor; - break; - default: - return false; - break; + } } + throw new FailedRequestException($jsonResponse); } /** * @param Monitor $monitor - * @return bool|Monitor + * @return Monitor|null + * @throws FailedRequestException */ public function delete(Monitor $monitor) { @@ -176,25 +192,29 @@ class UptimeRobotMonitorService extends UptimeRobotService $jsonResponse = $this->client->perform(self::DELETE_MONITOR, [ 'id' => $monitor->getId() ]); - } catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { + } catch (FailedRequestException $exception) { $this->logError($exception); + throw $exception; } - $response = json_decode($jsonResponse); + $response = json_decode($jsonResponse, false); + if ($response) { + switch ($response->stat) { + case 'ok': + return $monitor; + default: + return null; - switch ($response->stat) { - case 'ok': - return $monitor; - break; - default: - return false; - break; + } } + + throw new FailedRequestException($jsonResponse); } /** * @param Monitor $monitor - * @return bool|Monitor + * @return Monitor|null + * @throws FailedRequestException */ public function reset(Monitor $monitor) { @@ -202,28 +222,33 @@ class UptimeRobotMonitorService extends UptimeRobotService $jsonResponse = $this->client->perform(self::RESET_MONITOR, [ 'id' => $monitor->getId() ]); - } catch (\Vdhicts\UptimeRobot\Client\Exceptions\FailedRequestException $exception) { + } catch (FailedRequestException $exception) { $this->logError($exception); + throw $exception; } - $response = json_decode($jsonResponse); - - switch ($response->stat) { - case 'ok': - return $monitor; - break; - default: - return false; - break; + $response = json_decode($jsonResponse, false); + if ($response) { + switch ($response->stat) { + case 'ok': + return $monitor; + default: + return null; + } } + + throw new FailedRequestException($jsonResponse); } /** - * @param $id + * @param null $id + * @param null $name + * @param null $url * @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) { $this->getMonitors(); @@ -244,9 +269,10 @@ class UptimeRobotMonitorService extends UptimeRobotService * @param $url * @param int $type * @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) { $this->getMonitors(); @@ -264,9 +290,13 @@ class UptimeRobotMonitorService extends UptimeRobotService /** * @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()) { @@ -274,11 +304,11 @@ class UptimeRobotMonitorService extends UptimeRobotService if ($foundMonitor instanceof Monitor) { return $this->update($foundMonitor, $monitor, $alertContacts, $threshold, $recurrence); - } else { - return $this->create($monitor, $alertContacts, $threshold, $recurrence); } - } else { - return $monitor->getErrors(); + + return $this->create($monitor, $alertContacts, $threshold, $recurrence); } + + return null; } } \ No newline at end of file