Skip to content

Commit

Permalink
#184 - the Configuration object is used – XpathCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
AloisJasa committed Apr 12, 2023
1 parent 2be67fa commit 8befa30
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
1 change: 0 additions & 1 deletion app/Check/Consumers/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function process(\PhpAmqpLib\Message\AMQPMessage $message): int


/**
* @param \Pd\Monitoring\Check\Check $check
* @return bool TRUE, pokud se podařilo úspěšně provést kontrolu, jinak FALSE. Po FALSE může následovat opětovné spuštění kontroly
*/
abstract protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool;
Expand Down
29 changes: 11 additions & 18 deletions app/Check/Consumers/XpathCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,17 @@ final class XpathCheck extends Check
private ?string $lastUrl = NULL;


/**
* @param \Pd\Monitoring\Check\Check|\Pd\Monitoring\Check\XpathCheck $check
* @return bool
*/
protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
if ( ! $check instanceof \Pd\Monitoring\Check\XpathCheck) {
throw new \Pd\Monitoring\Exception();
}
$check->xpathLastResult = NULL;

$client = new \GuzzleHttp\Client();

$options = [
'connect_timeout' => $check::ALIVE_TIMEOUT / 1000,
'timeout' => 2 * $check::ALIVE_TIMEOUT / 1000,
'headers' => [
'User-Agent' => 'PeckaMonitoringBot/1.0',
],
'allow_redirects' => TRUE,
];
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create($check::ALIVE_TIMEOUT / 1000, (int) (\round(2 * ($check::ALIVE_TIMEOUT / 1000))))
->withAllowRedirects(\Pd\Monitoring\Check\Consumers\Client\Configuration\AllowRedirects::create(TRUE))
;
$client = new \GuzzleHttp\Client($guzzleOptions->config());

if ($check->siteMap && ! $this->siteMapLoader) {
$this->siteMapLoader = new \Pd\Monitoring\Check\SiteMapLoader($check->url);
Expand All @@ -37,7 +30,7 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
if ($this->siteMapLoader) {
$withoutError = TRUE;
while ($url = $this->siteMapLoader->getNextUrl($this->lastUrl)) {
$loaded = $this->loadUrl($client, $options, $check, $url);
$loaded = $this->loadUrl($client, $check, $url);
if ( ! $loaded) {
$this->logError($check, \sprintf('Došlo k chybě na url "%s"', $url));
$withoutError = FALSE;
Expand All @@ -47,7 +40,7 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool

return $withoutError;
} else {
$loaded = $this->loadUrl($client, $options, $check, $check->url);
$loaded = $this->loadUrl($client, $check, $check->url);
if ($loaded) {
return TRUE;
}
Expand All @@ -67,9 +60,9 @@ protected function getCheckType(): int
}


private function loadUrl(\GuzzleHttp\Client $client, array $options, \Pd\Monitoring\Check\XpathCheck $check, string $url): bool
private function loadUrl(\GuzzleHttp\Client $client, \Pd\Monitoring\Check\XpathCheck $check, string $url): bool
{
$response = $client->request('GET', $url, $options);
$response = $client->get($url);

if ($response->getStatusCode() !== 200) {
$this->logHeaders($check, $response);
Expand Down
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -455,26 +455,11 @@ parameters:
count: 1
path: app/Check/Consumers/RabbitQueueCheck.php

-
message: "#^Access to an undefined property Pd\\\\Monitoring\\\\Check\\\\Check\\:\\:\\$xpathLastResult\\.$#"
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Access to undefined constant Pd\\\\Monitoring\\\\Check\\\\Check\\:\\:ALIVE_TIMEOUT\\.$#"
count: 2
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Cannot call method getNextUrl\\(\\) on Pd\\\\Monitoring\\\\Check\\\\SiteMapLoader\\|null\\.$#"
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Method Pd\\\\Monitoring\\\\Check\\\\Consumers\\\\XpathCheck\\:\\:loadUrl\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Only booleans are allowed in a negated boolean, Pd\\\\Monitoring\\\\Check\\\\SiteMapLoader\\|null given\\.$#"
count: 1
Expand All @@ -485,11 +470,6 @@ parameters:
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Parameter \\#3 \\$check of method Pd\\\\Monitoring\\\\Check\\\\Consumers\\\\XpathCheck\\:\\:loadUrl\\(\\) expects Pd\\\\Monitoring\\\\Check\\\\XpathCheck, Pd\\\\Monitoring\\\\Check\\\\Check given\\.$#"
count: 2
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Only booleans are allowed in &&, string\\|null given on the left side\\.$#"
count: 1
Expand Down

0 comments on commit 8befa30

Please sign in to comment.