1: <?php
2:
3: namespace Guzzle\Plugin\Cache;
4:
5: use Guzzle\Common\Exception\InvalidArgumentException;
6: use Guzzle\Http\Message\RequestInterface;
7: use Guzzle\Http\Message\Response;
8:
9: /**
10: * Determines if a request can be cached using a callback
11: */
12: class CallbackCanCacheStrategy extends DefaultCanCacheStrategy
13: {
14: /**
15: * @var callable Callback for request
16: */
17: protected $requestCallback;
18:
19: /**
20: * @var callable Callback for response
21: */
22: protected $responseCallback;
23:
24: /**
25: * @param \Closure|array|mixed $requestCallback Callable method to invoke for requests
26: * @param \Closure|array|mixed $responseCallback Callable method to invoke for responses
27: *
28: * @throws InvalidArgumentException
29: */
30: public function __construct($requestCallback = null, $responseCallback = null)
31: {
32: if ($requestCallback && !is_callable($requestCallback)) {
33: throw new InvalidArgumentException('Method must be callable');
34: }
35:
36: if ($responseCallback && !is_callable($responseCallback)) {
37: throw new InvalidArgumentException('Method must be callable');
38: }
39:
40: $this->requestCallback = $requestCallback;
41: $this->responseCallback = $responseCallback;
42: }
43:
44: /**
45: * {@inheritdoc}
46: */
47: public function canCacheRequest(RequestInterface $request)
48: {
49: return $this->requestCallback
50: ? call_user_func($this->requestCallback, $request)
51: : parent::canCache($request);
52: }
53:
54: /**
55: * {@inheritdoc}
56: */
57: public function canCacheResponse(Response $response)
58: {
59: return $this->responseCallback
60: ? call_user_func($this->responseCallback, $response)
61: : parent::canCacheResponse($response);
62: }
63: }
64: