Completed
Push — master ( ee40ad...c11639 )
by Derek Stephen
53s
created

DownloadController::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Bone\Mvc\Controller;
4
5
use League\Route\Http\Exception\NotFoundException;
6
use InvalidArgumentException;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Zend\Diactoros\Response;
10
use Zend\Diactoros\Stream;
11
12
class DownloadController
13
{
14
    /** @var string $uploadsDirectory */
15
    private $uploadsDirectory;
16
17 8
    public function __construct(string $uploadsDirectory)
18
    {
19 8
        if (!is_dir($uploadsDirectory)) {
20
            throw new InvalidArgumentException('Directory ' . $uploadsDirectory . 'not found');
21
        }
22
23 8
        $this->uploadsDirectory = $uploadsDirectory;
24 8
    }
25
26
    /**
27
     * @param ServerRequestInterface $request
28
     * @param array $args
29
     * @return ResponseInterface
30
     */
31
    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...
32
    {
33
        $file = $request->getQueryParams()['file'];
34
35
        if (file_exists('public' . $file)) {
36
            $path = 'public' . $file;
37
        } else if (!file_exists($this->uploadsDirectory . $file)) {
38
            throw new Exception($path . ' not found.', 404);
39
        } else {
40
            $path = $this->uploadsDirectory . $file;
41
        }
42
        
43
        // magic_mime module installed?
44
        if (function_exists('mime_content_type')) {
45
            $mimeType = mime_content_type($path);
46
        } else if (function_exists('finfo_file')) { // or fileinfo module installed?
47
            $finfo = finfo_open(FILEINFO_MIME); // return mime type
48
            $mimeType = finfo_file($finfo, $path);
49
            finfo_close($finfo);
50
        }
51
52
        $contents = file_get_contents($path);
53
        $stream = new Stream('php://memory', 'r+');
54
        $stream->write($contents);
55
        $response = new Response();
56
        $response = $response->withBody($stream);
57
        $response = $response->withHeader('Content-Type', $mimeType);
0 ignored issues
show
Bug introduced by
The variable $mimeType does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
58
59
        return $response;
60
    }
61
}