Completed
Pull Request — master (#49)
by
unknown
03:17
created

FlysystemRenderer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 21 2
A getFile() 0 4 1
A deliverAsset() 0 17 1
A error() 0 10 2
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 2
    public function response(Result $result = null)
38
    {
39 2
        $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 2
            $file = $this->getFile(
42 2
                Hash::get($data, 'filesystem'),
43 2
                Hash::get($data, 'path')
44 2
            );
45
46 1
            $this->_service->setResponse(
47 1
                $this->deliverAsset($this->_service->getResponse(), $file)
48 1
            );
49 2
        } catch (FileNotFoundException $e) {
50 1
            $response = $this->_service->getResponse()
51 1
                ->withStatus(404);
52
53 1
            $this->_service->setResponse($response);
54
        }
55
56 2
        return true;
57
    }
58
59
    /**
60
     * Get flystem file object
61
     *
62
     * @param Filesystem $filesystem custom filesystem
63
     * @param string $path of file at filesystem
64
     * @return File
65
     */
66 2
    protected function getFile(Filesystem $filesystem, $path)
67
    {
68 2
        return $filesystem->get($path);
69
    }
70
71
    /**
72
     * Deliver the asset stream in body
73
     *
74
     * @param Response $response service response
75
     * @param File $file file object
76
     * @return Response
77
     */
78 1
    public function deliverAsset(Response $response, File $file)
79
    {
80 1
        $contentType = $file->getType();
81 1
        $modified = $file->getTimestamp();
82 1
        $expire = strtotime(Configure::write('Api.Flysystem.expire'));
83 1
        $maxAge = $expire - time();
84 1
        $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...
85
86 1
        return $response->withBody($stream)
87
            // Content
88 1
            ->withHeader('Content-Type', $contentType)
89
            // Cache
90 1
            ->withHeader('Cache-Control', 'public,max-age=' . $maxAge)
91 1
            ->withHeader('Date', gmdate('D, j M Y G:i:s \G\M\T', time()))
92 1
            ->withHeader('Last-Modified', gmdate('D, j M Y G:i:s \G\M\T', $modified))
93 1
            ->withHeader('Expires', gmdate('D, j M Y G:i:s \G\M\T', $expire));
94
    }
95
96
    /**
97
     * Handle error setting a response status
98
     *
99
     * @param Exception $exception thrown at service or action
100
     *
101
     * @return void
102
     */
103 2
    public function error(Exception $exception)
104
    {
105 2
        $code = $exception->getCode();
106 2
        $response = $this->_service->getResponse()
107 2
            ->withStatus($code ? $code : 500);
108
109 2
        $this->log($exception);
110
111 2
        $this->_service->setResponse($response);
112 2
    }
113
}
114