Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pages response http code check #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Kendu/HealthCheck/Base/Pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php namespace Kendu\HealthCheck\Base;

class Pages implements CheckInterface
{
public function __construct(array $params)
{
$this->params = $params;
}

public function run()
{
$baseUrl = 'http://'.$_SERVER['SERVER_NAME'];
$errors = [];

if (isset($this->params['pages'])) {
foreach ($this->params['pages'] as $key => $page) {
$url = $baseUrl . $page;

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_NOBODY, true);

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

if (200 > $httpCode || $httpCode > 300) {
$errors[] = sprintf('%s not reachable. Code: "%s"', $page, $httpCode);
}

curl_close($handle);
}

if (!empty($errors)) {
return new \Kendu\HealthCheck\Status\Fail([
'message' => implode(';', $errors)
]);
}

return new \Kendu\HealthCheck\Status\Pass;
}

return new \Kendu\HealthCheck\Status\Fail([
'message' => 'No page url defined'
]);
}

public function id()
{
return 'pages';
}
}