Completed
Push — master ( aafa3b...fcd775 )
by Derek Stephen
02:04 queued 17s
created

DownloadController::downloadAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bone\Controller;
4
5
use Bone\Exception;
6
use InvalidArgumentException;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Laminas\Diactoros\Response;
10
use Laminas\Diactoros\Stream;
11
12
class DownloadController
13
{
14
    /** @var string $uploadsDirectory */
15
    private $uploadsDirectory;
16
17 5
    public function __construct(string $uploadsDirectory)
18
    {
19 5
        if (!is_dir($uploadsDirectory)) {
20 1
            throw new InvalidArgumentException('Directory ' . $uploadsDirectory . ' not found');
21
        }
22
23 4
        $this->uploadsDirectory = $uploadsDirectory;
24 4
    }
25
26
    /**
27
     * @param ServerRequestInterface $request
28
     * @param array $args
29
     * @return ResponseInterface
30
     * @throws Exception
31
     */
32 4
    public function downloadAction(ServerRequestInterface $request, array $args): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34 4
        $queryParams = $request->getQueryParams();
35 4
        $path = $this->getFilePath($queryParams);
36 2
        $mimeType = $this->getMimeType($path);
37 2
        $contents = file_get_contents($path);
38 2
        $stream = new Stream('php://memory', 'r+');
39 2
        $stream->write($contents);
40 2
        $response = new Response();
41 2
        $response = $response->withBody($stream);
42 2
        $response = $response->withHeader('Content-Type', $mimeType);
43
44 2
        return $response;
45
    }
46
47
    /**
48
     * @param string $path
49
     * @return string
50
     */
51 2
    private function getMimeType(string $path): string
52
    {
53 2
        $finfo = finfo_open(FILEINFO_MIME); // return mime type
54 2
        $mimeType = finfo_file($finfo, $path);
55 2
        finfo_close($finfo);
56
57 2
        return $mimeType;
58
    }
59
60
    /**
61
     * @param array $queryParams
62
     * @return string
63
     * @throws Exception
64
     */
65 4
    private function getFilePath(array $queryParams): string
66
    {
67 4
        if (!isset($queryParams['file'])) {
68 1
            throw new Exception('Invalid Request.', 400);
69
        }
70
71 3
        $file = $queryParams['file'];
72 3
        $path = $this->uploadsDirectory . $file;
73
74 3
        if (file_exists('public' . $file)) {
75 1
            $path = 'public' . $file;
76 2
        } else if (!file_exists($path)) {
77 1
            throw new Exception($path . ' not found.', 404);
78
        }
79
80 2
        return $path;
81
    }
82
}