Test Failed
Pull Request — master (#49)
by
unknown
08:02
created

FlysystemRenderer::deliverAsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Renderer;
13
14
use Cake\Core\Configure;
15
use CakeDC\Api\Service\Action\Result;
16
use Cake\Http\Response;
17
use Cake\Log\LogTrait;
18
use Exception;
19
use League\Flysystem\File;
20
use League\Flysystem\FileNotFoundException;
21
use League\Flysystem\Filesystem;
22
use Zend\Diactoros\Stream;
23
24
/**
25
 * Class FlysystemRenderer to render file using Flysystem library
26
 */
27
class FlysystemRenderer extends FileRenderer
28
{
29
    use LogTrait;
30
    /**
31
     * Builds the HTTP response.
32
     *
33
     * @param Result $result The result object returned by the Service.
34
     * @return bool
35
     */
36
    public function response(Result $result = null)
37
    {
38
        $data = $result->getData();
0 ignored issues
show
Bug introduced by
It seems like $result is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
39
        try {
40
            $file = $this->getFile($data['filesystem'], $data['path']);
41
42
            $this->_service->setResponse(
43
                $this->deliverAsset($this->_service->getResponse(), $file)
44
            );
45
        } catch (FileNotFoundException $e) {
46
            $response = $this->_service->getResponse()
47
                ->withStatus(404);
48
49
            $this->_service->setResponse($response);
50
        }
51
52
        return true;
53
    }
54
55
    /**
56
     * Get flystem file object
57
     *
58
     * @param Filesystem $filesystem custom filesystem
59
     * @param string $path of file at filesystem
60
     * @return File
61
     */
62
    protected function getFile(Filesystem $filesystem, string $path): File
63
    {
64
        return $filesystem->get($path);
65
    }
66
67
    /**
68
     * Deliver the asset stream in body
69
     *
70
     * @param Response $response service response
71
     * @param File $file file object
72
     * @return Response
73
     */
74
    public function deliverAsset(Response $response, File $file) : Response
75
    {
76
        $contentType = $file->getType();
77
        $modified = $file->getTimestamp();
78
        $expire = strtotime(Configure::write('Api.Flysystem.expire'));
79
        $maxAge = $expire - time();
80
        $stream = new Stream($file->readStream());
0 ignored issues
show
Security Bug introduced by
It seems like $file->readStream() targeting League\Flysystem\File::readStream() can also be of type false; however, Zend\Diactoros\Stream::__construct() does only seem to accept string|resource, did you maybe forget to handle an error condition?
Loading history...
81
82
        return $response->withBody($stream)
83
            // Content
84
            ->withHeader('Content-Type', $contentType)
85
            // Cache
86
            ->withHeader('Cache-Control', 'public,max-age=' . $maxAge)
87
            ->withHeader('Date', gmdate('D, j M Y G:i:s \G\M\T', time()))
88
            ->withHeader('Last-Modified', gmdate('D, j M Y G:i:s \G\M\T', $modified))
89
            ->withHeader('Expires', gmdate('D, j M Y G:i:s \G\M\T', $expire));
90
    }
91
92
    /**
93
     * Handle error setting a response status
94
     *
95
     * @param Exception $exception thrown at service or action
96
     *
97
     * @return void
98
     */
99
    public function error(Exception $exception)
100
    {
101
        $code = $exception->getCode();
102
        $response = $this->_service->getResponse()
103
            ->withStatus($code ? $code : 500);
104
105
        $this->log($exception);
106
107
        $this->_service->setResponse($response);
108
    }
109
}
110