Skip to content

Commit 4d3c414

Browse files
committed
Merge pull request #41 from ddeboer/simplify-exceptions
use factory methods for exceptions
2 parents 43e8686 + bb8f720 commit 4d3c414

15 files changed

+124
-84
lines changed

src/CacheInvalidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FOS\HttpCache;
44

55
use FOS\HttpCache\Exception\ExceptionCollection;
6+
use FOS\HttpCache\Exception\InvalidArgumentException;
67
use FOS\HttpCache\Exception\ProxyResponseException;
78
use FOS\HttpCache\Exception\ProxyUnreachableException;
89
use FOS\HttpCache\Exception\UnsupportedInvalidationMethodException;
@@ -73,7 +74,7 @@ public function __construct(CacheProxyInterface $cache)
7374
*
7475
* @return bool
7576
*
76-
* @throws \InvalidArgumentException
77+
* @throws InvalidArgumentException
7778
*/
7879
public function supports($operation)
7980
{
@@ -85,7 +86,7 @@ public function supports($operation)
8586
case self::INVALIDATE:
8687
return $this->cache instanceof BanInterface;
8788
default:
88-
throw new \InvalidArgumentException('Unknown operation ' . $operation);
89+
throw new InvalidArgumentException('Unknown operation ' . $operation);
8990
}
9091
}
9192

src/Exception/ExceptionCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* A collection of exceptions that might occur during the flush operation of a
77
* CacheProxyInterface implementation
88
*/
9-
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable
9+
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable, HttpCacheExceptionInterface
1010
{
1111
protected $exceptions = array();
1212

@@ -29,9 +29,9 @@ public function add(\Exception $e)
2929
}
3030

3131
/**
32-
* Get first exception in collection
32+
* Get first exception in collection or null, if there is none.
3333
*
34-
* @return \Exception | null
34+
* @return \Exception|null
3535
*/
3636
public function getFirst()
3737
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace FOS\HttpCache\Exception;
4+
5+
/**
6+
* Common interface for all exceptions thrown by this library.
7+
*/
8+
interface HttpCacheExceptionInterface
9+
{}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace FOS\HttpCache\Exception;
4+
5+
/**
6+
* Wrapping the base exception for FOSHttpCache.
7+
*/
8+
class InvalidArgumentException extends \InvalidArgumentException implements HttpCacheExceptionInterface
9+
{
10+
}

src/Exception/InvalidUrlException.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,56 @@
22

33
namespace FOS\HttpCache\Exception;
44

5-
class InvalidUrlException extends \InvalidArgumentException
5+
/**
6+
* Thrown during setup if the configuration for a cache proxy is invalid.
7+
*/
8+
class InvalidUrlException extends InvalidArgumentException implements HttpCacheExceptionInterface
69
{
710
/**
8-
* Constructor
11+
* @param string $url The invalid URL.
12+
* @param string $reason Further explanation why the URL was invalid (optional)
913
*
10-
* @param string $url Invalid URL
11-
* @param string $reason Reason (optional)
14+
* @return InvalidUrlException
1215
*/
13-
public function __construct($url, $reason = null)
16+
public static function invalidUrl($url, $reason = null)
1417
{
1518
$msg = sprintf('URL "%s" is invalid.', $url);
1619
if ($reason) {
1720
$msg .= sprintf('Reason: %s', $reason);
1821
}
1922

20-
parent::__construct($msg);
23+
return new InvalidUrlException($msg);
24+
}
25+
26+
/**
27+
* @param string $server Invalid server
28+
* @param array $allowed Allowed URL parts
29+
*
30+
* @return InvalidUrlException
31+
*/
32+
public static function invalidUrlParts($server, array $allowed)
33+
{
34+
return new InvalidUrlException(sprintf(
35+
'Server "%s" is invalid. Only %s URL parts are allowed.',
36+
$server,
37+
implode(', ', $allowed)
38+
));
39+
}
40+
41+
/**
42+
* @param string $url Requested full URL
43+
* @param string $scheme Requested URL scheme
44+
* @param array $allowed Supported URL schemes
45+
*
46+
* @return InvalidUrlException
47+
*/
48+
public static function invalidUrlScheme($url, $scheme, array $allowed)
49+
{
50+
return new InvalidUrlException(sprintf(
51+
'Host "%s" with scheme "%s" is invalid. Only schemes "%s" are supported',
52+
$url,
53+
$scheme,
54+
implode(', ', $allowed)
55+
));
2156
}
2257
}

src/Exception/InvalidUrlPartsException.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Exception/InvalidUrlSchemeException.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Exception/MissingHostException.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace FOS\HttpCache\Exception;
44

5-
class MissingHostException extends \RuntimeException
5+
/**
6+
* Thrown when there is no default host configured and an invalidation request
7+
* with just a path is made.
8+
*/
9+
class MissingHostException extends \RuntimeException implements HttpCacheExceptionInterface
610
{
711
/**
8-
* Constructor
12+
* @param string $path The path that was asked to be invalidated.
913
*
10-
* @param string $path Path
14+
* @return MissingHostException
1115
*/
12-
public function __construct($path)
16+
public static function missingHost($path)
1317
{
1418
$msg = sprintf(
1519
'Path "%s" cannot be invalidated without a host. '
@@ -18,6 +22,6 @@ public function __construct($path)
1822
$path
1923
);
2024

21-
parent::__construct($msg);
25+
return new MissingHostException($msg);
2226
}
2327
}

src/Exception/ProxyResponseException.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
namespace FOS\HttpCache\Exception;
44

55
/**
6-
* An error response from the caching proxy
6+
* Wrapping an error response from the caching proxy.
77
*/
8-
class ProxyResponseException extends \RuntimeException
8+
class ProxyResponseException extends \RuntimeException implements HttpCacheExceptionInterface
99
{
10-
public function __construct($proxy, $statusCode, $statusMessage, \Exception $previous = null)
10+
/**
11+
* @param string $host The host name that was contacted.
12+
* @param string $statusCode The status code of the reply.
13+
* @param string $statusMessage The content of the reply.
14+
* @param \Exception $previous The exception from guzzle.
15+
*
16+
* @return ProxyUnreachableException
17+
*/
18+
public static function proxyResponse($host, $statusCode, $statusMessage, \Exception $previous = null)
1119
{
12-
parent::__construct(
20+
return new ProxyResponseException(
1321
sprintf(
1422
'%s error response "%s" from caching proxy at %s',
1523
$statusCode,
1624
$statusMessage,
17-
$proxy
25+
$host
1826
),
1927
$statusCode,
2028
$previous

src/Exception/ProxyUnreachableException.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
namespace FOS\HttpCache\Exception;
44

55
/**
6-
* Thrown when a request to the reverse caching proxy fails
6+
* Thrown when a request to the reverse caching proxy fails to establish a
7+
* connection.
78
*/
8-
class ProxyUnreachableException extends \RuntimeException
9+
class ProxyUnreachableException extends \RuntimeException implements HttpCacheExceptionInterface
910
{
10-
public function __construct($host, $message, \Exception $previous = null)
11+
/**
12+
* @param string $host The host name that was contacted.
13+
* @param string $message The error message from guzzle.
14+
* @param \Exception $previous The exception from guzzle.
15+
*
16+
* @return ProxyUnreachableException
17+
*/
18+
public static function proxyUnreachable($host, $message, \Exception $previous = null)
1119
{
12-
parent::__construct(
20+
return new ProxyUnreachableException(
1321
sprintf(
1422
'Request to caching proxy at %s failed with message "%s"',
1523
$host,

0 commit comments

Comments
 (0)