Completed
Push — master ( 4f4fb1...2bb66e )
by Jonathan
02:15
created

src/Responses/PsrResponseFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace League\Glide\Responses;
4
5
use Closure;
6
use League\Flysystem\FilesystemInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
class PsrResponseFactory implements ResponseFactoryInterface
10
{
11
    /**
12
     * Base response object.
13
     * @var ResponseInterface
14
     */
15
    protected $response;
16
17
    /**
18
     * Callback to create stream.
19
     * @var Closure
20
     */
21
    protected $streamCallback;
22
23
    /**
24
     * Create PsrResponseFactory instance.
25
     * @param ResponseInterface $response       Base response object.
26
     * @param Closure           $streamCallback Callback to create stream.
27
     */
28 6
    public function __construct(ResponseInterface $response, Closure $streamCallback)
29
    {
30 6
        $this->response = $response;
31 6
        $this->streamCallback = $streamCallback;
32 6
    }
33
34
    /**
35
     * Create response.
36
     * @param  FilesystemInterface $cache Cache file system.
37
     * @param  string              $path  Cached file path.
38
     * @return ResponseInterface   Response object.
39
     */
40 3
    public function create(FilesystemInterface $cache, $path)
41
    {
42 3
        $stream = $this->streamCallback->__invoke(
43 3
            $cache->readStream($path)
44 2
        );
45
46 3
        $contentType = $cache->getMimetype($path);
47 3
        $contentLength = (string) $cache->getSize($path);
48 3
        $cacheControl = 'max-age=31536000, public';
49 3
        $expires = date_create('+1 years')->format('D, d M Y H:i:s').' GMT';
50
51 3
        return $this->response->withBody($stream)
52 3
            ->withHeader('Content-Type', $contentType)
0 ignored issues
show
It seems like $contentType defined by $cache->getMimetype($path) on line 46 can also be of type false; however, Psr\Http\Message\MessageInterface::withHeader() does only seem to accept string|array<integer,string>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
53 3
            ->withHeader('Content-Length', $contentLength)
54 3
            ->withHeader('Cache-Control', $cacheControl)
55 3
            ->withHeader('Expires', $expires);
56
    }
57
}
58