Completed
Push — master ( 48b899...aaa497 )
by Derek Stephen
01:28
created

DownloadController::getMimeType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Bone\Mvc\Controller;
4
5
use Bone\Exception;
6
use League\Route\Http\Exception\NotFoundException;
7
use InvalidArgumentException;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Zend\Diactoros\Response;
11
use Zend\Diactoros\Stream;
12
13
class DownloadController
14
{
15
    /** @var string $uploadsDirectory */
16
    private $uploadsDirectory;
17
18 8
    public function __construct(string $uploadsDirectory)
19
    {
20 8
        if (!is_dir($uploadsDirectory)) {
21
            throw new InvalidArgumentException('Directory ' . $uploadsDirectory . ' not found');
22
        }
23
24 8
        $this->uploadsDirectory = $uploadsDirectory;
25 8
    }
26
27
    /**
28
     * @param ServerRequestInterface $request
29
     * @param array $args
30
     * @return ResponseInterface
31
     */
32
    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
        $queryParams = $request->getQueryParams();
35
        $path = $this->getFilePath($queryParams);
36
        $mimeType = $this->getMimeType($path);
37
        $contents = file_get_contents($path);
38
        $stream = new Stream('php://memory', 'r+');
39
        $stream->write($contents);
40
        $response = new Response();
41
        $response = $response->withBody($stream);
42
        $response = $response->withHeader('Content-Type', $mimeType);
43
44
        return $response;
45
    }
46
47
    /**
48
     * @param array $queryParams
0 ignored issues
show
Bug introduced by
There is no parameter named $queryParams. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
49
     * @return string
50
     */
51
    private function getMimeType(string $path): string
52
    {
53
        $finfo = finfo_open(FILEINFO_MIME); // return mime type
54
        $mimeType = finfo_file($finfo, $path);
55
        finfo_close($finfo);
56
57
        return $mimeType;
58
    }
59
60
    /**
61
     * @param array $queryParams
62
     * @return string
63
     */
64
    private function getFilePath(array $queryParams): string
65
    {
66
        if (!isset($queryParams['file'])) {
67
            throw new Exception('Invalid Request.', 400);
68
        }
69
70
        $file = $queryParams['file'];
71
        $path = $this->uploadsDirectory . $file;
72
73
        if (file_exists('public' . $file)) {
74
            $path = 'public' . $file;
75
        } else if (!file_exists($path)) {
76
            throw new Exception($path . ' not found.', 404);
77
        }
78
79
        return $path;
80
    }
81
}