Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"maintenance"
],
"require": {
"php": ">=5.5",
"psr/http-message": "^1.0"
"php": ">=5.6",
"psr/http-message": "^1.0",
"php-middleware/double-pass-compatibility": "^1.0",
"http-interop/http-middleware": "^0.4.1"
},
"require-dev": {
"phpunit/phpunit": "^4.8.6",
"phpunit/phpunit": "^5.6 || ^6.1",
"zendframework/zend-diactoros": "^1.1.3"
},
"autoload": {
Expand Down
44 changes: 24 additions & 20 deletions src/MaintenanceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace PhpMiddleware\Maintenance;

use Psr\Http\Message\ResponseInterface;
use DateTime;
use DateTimeInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use InvalidArgumentException;
use PhpMiddleware\DoublePassCompatibilityTrait;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

final class MaintenanceMiddleware
final class MaintenanceMiddleware implements MiddlewareInterface
{
use DoublePassCompatibilityTrait;

/**
* @var string
*/
Expand All @@ -22,7 +30,7 @@ private function __construct()
}

/**
* @return \self
* @return self
*/
public static function create()
{
Expand All @@ -37,7 +45,7 @@ public static function create()
public static function createWithRetryAsSeconds($seconds)
{
if (!is_int($seconds)) {
throw new \InvalidArgumentException('Seconds must be integer');
throw new InvalidArgumentException('Seconds must be integer');
}
$instance = new self();
$instance->retryAfter = (string) $seconds;
Expand All @@ -59,25 +67,25 @@ public static function createWithRetryAsSecondsAndRefresh($seconds)
}

/**
* @param \DateTimeInterface $datetime
* @param DateTimeInterface $datetime
*
* @return self
*/
public static function createWithRetryAsDateTime(\DateTimeInterface $datetime)
public static function createWithRetryAsDateTime(DateTimeInterface $datetime)
{
$instance = new self();

$instance->retryAfter = $datetime->format(\DateTime::RFC2822);
$instance->retryAfter = $datetime->format(DateTime::RFC2822);

return $instance;
}

/**
* @param \DateTimeInterface $datetime
* @param DateTimeInterface $datetime
*
* @return self
*/
public static function createWithRetryAsDateTimeAndRefresh(\DateTimeInterface $datetime)
public static function createWithRetryAsDateTimeAndRefresh(DateTimeInterface $datetime)
{
$instance = self::createWithRetryAsDateTime($datetime);
$diff = time() - $datetime->getTimestamp();
Expand All @@ -86,23 +94,19 @@ public static function createWithRetryAsDateTimeAndRefresh(\DateTimeInterface $d
return $instance;
}

/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$headers = [];

if ($this->retryAfter !== '') {
$response = $response->withHeader('Retry-After', $this->retryAfter);
$headers['Retry-After'] = $this->retryAfter;

if ($this->refresh > 0) {
$response = $response->withHeader('Refresh', (string) $this->refresh);
$headers['Refresh'] = (string) $this->refresh;
}
}

return $response->withStatus(503);
return new Response('php://memory', 503, $headers);
}

}
4 changes: 2 additions & 2 deletions test/MaintenanceMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use DateTime;
use Exception;
use PhpMiddleware\Maintenance\MaintenanceMiddleware;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;

class MaintenanceMiddlewareTest extends PHPUnit_Framework_TestCase
class MaintenanceMiddlewareTest extends TestCase
{
public function testWithoutRetry()
{
Expand Down