CacheableResponse::getResponse()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 21
nc 3
nop 3
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Bundle\Response;
17
18
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
19
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
class CacheableResponse
24
{
25
    const RESPONSE_PREFIX = '__CACH_RESP__';
26
27
    /**
28
     * @var ExtendedCacheItemPoolInterface
29
     */
30
    protected $request;
31
32
    /**
33
     * @var ExtendedCacheItemPoolInterface
34
     */
35
    protected $cacheInstance;
36
37
    /**
38
     * CachePromise constructor.
39
     * @param ExtendedCacheItemPoolInterface $cacheInstance
40
     */
41
    public function __construct(ExtendedCacheItemPoolInterface $cacheInstance, Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
42
    {
43
        $this->cacheInstance = $cacheInstance;
44
        $this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type object<Symfony\Component\HttpFoundation\Request> is incompatible with the declared type object<Phpfastcache\Core...CacheItemPoolInterface> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
    }
46
47
    /**
48
     * @param string $cacheKey
49
     * @param int|\DateInterval $expiresAfter
50
     * @param callable $callback
51
     * @return mixed
52
     * @throws PhpfastcacheLogicException
53
     */
54
    public function getResponse(string $cacheKey, $expiresAfter = null, callable $callback): Response
55
    {
56
        $cacheKey = self::RESPONSE_PREFIX . $cacheKey;
57
        $cacheItem = $this->cacheInstance->getItem($cacheKey);
58
        $cacheResponse = $cacheItem->get();
59
60
        /**
61
         * No isHit() test here as we directly
62
         * test if the cached response object
63
         * is effectively a "Response" object
64
         */
65
        if (!($cacheResponse instanceof Response)) {
66
            $response = $callback();
67
            if($response instanceof Response){
68
                $cacheItem->expiresAfter($expiresAfter);
69
70
                /**
71
                 * Alter response header to set
72
                 * cache/expiration directives
73
                 */
74
                $response->setExpires($cacheItem->getExpirationDate());
75
                $response->setSharedMaxAge($cacheItem->getTtl());
76
                $response->headers->addCacheControlDirective('must-revalidate', true);
77
                $response->setEtag(md5($response->getContent()));
78
                $response->setPublic();
79
80
                $cacheItem->set($response);
81
                $this->cacheInstance->save($cacheItem);
82
                $cacheResponse = $response;
83
            }else{
84
               throw new PhpfastcacheLogicException('Your callback response MUST return a valid Symfony HTTP Foundation Response object');
85
            }
86
        }else{
87
            $cacheResponse->isNotModified($this->request);
0 ignored issues
show
Documentation introduced by
$this->request is of type object<Phpfastcache\Core...CacheItemPoolInterface>, but the function expects a object<Symfony\Component\HttpFoundation\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
        }
89
90
        return $cacheResponse;
91
    }
92
}