Completed
Push — dev-master ( ee40ad...39856d )
by Derek Stephen
01:52
created

DownloadController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 13.03%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 47
ccs 3
cts 23
cp 0.1303
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A downloadAction() 0 27 4
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 9
    public function __construct(string $uploadsDirectory)
18
    {
19 9
        if (!is_dir($uploadsDirectory)) {
20 9
            throw new InvalidArgumentException('Directory not found');
21
        }
22
23
        $this->uploadsDirectory = $uploadsDirectory;
24
    }
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
        $path = $this->uploadsDirectory . $file;
35
36
        if (!file_exists($path)) {
37
            throw new Exception($path . ' not found.', 404);
38
        }
39
        
40
        // magic_mime module installed?
41
        if (function_exists('mime_content_type')) {
42
            $mimeType = mime_content_type($path);
43
        } else if (function_exists('finfo_file')) { // or fileinfo module installed?
44
            $finfo = finfo_open(FILEINFO_MIME); // return mime type
45
            $mimeType = finfo_file($finfo, $path);
46
            finfo_close($finfo);
47
        }
48
49
        $contents = file_get_contents($path);
50
        $stream = new Stream('php://memory', 'r+');
51
        $stream->write($contents);
52
        $response = new Response();
53
        $response = $response->withBody($stream);
54
        $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...
55
56
        return $response;
57
    }
58
}