FlysystemRenderer::deliverAsset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 19
rs 9.6333
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 CakeDC\Api\Service\Action\Result;
15
use Cake\Core\Configure;
16
use Cake\Http\Response;
17
use Cake\Log\LogTrait;
18
use Cake\Utility\Hash;
19
use Exception;
20
use League\Flysystem\File;
21
use League\Flysystem\FileNotFoundException;
22
use League\Flysystem\Filesystem;
23
use Zend\Diactoros\Stream;
24
25
/**
26
 * Class FlysystemRenderer to render file using Flysystem library
27
 */
28
class FlysystemRenderer extends FileRenderer
29
{
30
    use LogTrait;
31
    /**
32
     * Builds the HTTP response.
33
     *
34
     * @param Result $result The result object returned by the Service.
35
     * @return bool
36
     */
37
    public function response(Result $result = null)
38
    {
39
        $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...
40
        try {
41
            $file = $this->getFile(
42
                Hash::get($data, 'filesystem'),
43
                Hash::get($data, 'path')
44
            );
45
            $name = Hash::get($data, 'name');
46
47
            $this->_service->setResponse(
48
                $this->deliverAsset($this->_service->getResponse(), $file, $name)
49
            );
50
        } catch (FileNotFoundException $e) {
51
            $response = $this->_service->getResponse()
52
                ->withStatus(404);
53
54
            $this->_service->setResponse($response);
55
        }
56
57
        return true;
58
    }
59
60
    /**
61
     * Get flystem file object
62
     *
63
     * @param Filesystem $filesystem custom filesystem
64
     * @param string $path of file at filesystem
65
     * @return File
66
     */
67
    protected function getFile(Filesystem $filesystem, $path)
68
    {
69
        return $filesystem->get($path);
70
    }
71
72
    /**
73
     * Deliver the asset stream in body
74
     *
75
     * @param Response $response service response
76
     * @param File $file file object
77
     * @param string $name file name shown to user
78
     * @return Response
79
     */
80
    public function deliverAsset(Response $response, File $file, $name)
81
    {
82
        $contentType = $file->getType();
83
        $modified = $file->getTimestamp();
84
        $expire = strtotime(Configure::read('Api.Flysystem.expire'));
85
        $maxAge = $expire - time();
86
        $stream = new Stream($file->readStream());
87
88
        return $response->withBody($stream)
89
            // Content
90
            ->withHeader('Content-Type', $contentType)
91
            //Name
92
            ->withDownload($name)
93
            // Cache
94
            ->withHeader('Cache-Control', 'public,max-age=' . $maxAge)
95
            ->withHeader('Date', gmdate('D, j M Y G:i:s \G\M\T', time()))
96
            ->withHeader('Last-Modified', gmdate('D, j M Y G:i:s \G\M\T', $modified))
97
            ->withHeader('Expires', gmdate('D, j M Y G:i:s \G\M\T', $expire));
98
    }
99
100
    /**
101
     * Handle error setting a response status
102
     *
103
     * @param Exception $exception thrown at service or action
104
     *
105
     * @return void
106
     */
107
    public function error(Exception $exception)
108
    {
109
        $code = $exception->getCode();
110
        $response = $this->_service->getResponse()
111
            ->withStatus($code ? $code : 500);
112
113
        $this->log($exception);
114
115
        $this->_service->setResponse($response);
116
    }
117
}
118