Completed
Pull Request — master (#300)
by
unknown
12:55
created

PsrResponseFactory::createResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 12
1
<?php
2
3
namespace League\Glide\Responses;
4
5
use Closure;
6
use League\Flysystem\FilesystemException as V2FilesystemException;
7
use League\Flysystem\FilesystemInterface;
8
use League\Flysystem\FilesystemOperator;
9
use League\Glide\Filesystem\FilesystemException;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\StreamInterface;
12
13
class PsrResponseFactory implements ResponseFactoryInterface, Flysystem2ResponseFactoryInterface
14
{
15
    /**
16
     * Base response object.
17
     * @var ResponseInterface
18
     */
19
    protected $response;
20
21
    /**
22
     * Callback to create stream.
23
     * @var Closure
24
     */
25
    protected $streamCallback;
26
27
    /**
28
     * Create PsrResponseFactory instance.
29
     * @param ResponseInterface $response       Base response object.
30
     * @param Closure           $streamCallback Callback to create stream.
31
     */
32
    public function __construct(ResponseInterface $response, Closure $streamCallback)
33
    {
34
        $this->response = $response;
35
        $this->streamCallback = $streamCallback;
36
    }
37
38
    /**
39
     * Build response object
40
     * @param StreamInterface $stream
41
     * @param string          $contentType
42
     * @param string          $contentLength
43
     * @return ResponseInterface Response object.
44
     * @throws FilesystemException
45
     */
46
    private function createResponse($stream, $contentType, $contentLength)
47
    {
48
        $cacheControl = 'max-age=31536000, public';
49
        $expires = date_create('+1 years')->format('D, d M Y H:i:s').' GMT';
50
51
        if ($contentType === false) {
52
            throw new FilesystemException('Unable to determine the image content type.');
53
        }
54
55
        if ($contentLength === false) {
56
            throw new FilesystemException('Unable to determine the image content length.');
57
        }
58
59
        return $this->response->withBody($stream)
60
                              ->withHeader('Content-Type', $contentType)
61
                              ->withHeader('Content-Length', $contentLength)
62
                              ->withHeader('Cache-Control', $cacheControl)
63
                              ->withHeader('Expires', $expires);
64
    }
65
66
    /**
67
     * Create response.
68
     * @param  FilesystemInterface $cache Cache file system.
69
     * @param  string              $path  Cached file path.
70
     * @return ResponseInterface   Response object.
71
     * @throws FilesystemException
72
     */
73
    public function create(FilesystemInterface $cache, $path)
74
    {
75
        $stream = $this->streamCallback->__invoke(
76
            $cache->readStream($path)
77
        );
78
79
        $contentType = $cache->getMimetype($path);
80
        $contentLength = (string) $cache->getSize($path);
81
82
        return $this->createResponse($stream, $contentType, $contentLength);
83
    }
84
85
    /**
86
     * Create response.
87
     * @param  FilesystemOperator  $cache  Cache file system.
88
     * @param  string  $path  Cached file path.
89
     * @return ResponseInterface   Response object.
90
     * @throws FilesystemException
91
     */
92
    public function createFlysystem2(FilesystemOperator $cache, $path)
93
    {
94
        try {
95
            $stream = $this->streamCallback->__invoke(
96
                $cache->readStream($path)
97
            );
98
99
            $contentType = $cache->mimeType($path);
100
            $contentLength = (string) $cache->fileSize($path);
101
102
            return $this->createResponse($stream, $contentType, $contentLength);
103
        } catch (V2FilesystemException $exception) {
104
            throw new FilesystemException($exception->getMessage());
105
        }
106
    }
107
}
108