Completed
Push — dev-master ( 72adf2...0854c8 )
by Derek Stephen
01:46
created

DownloadController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 16%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 50
ccs 4
cts 25
cp 0.16
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A downloadAction() 0 30 5
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
        $file = $request->getQueryParams()['file'];
35
36
        if (file_exists('public' . $file)) {
37
            $path = 'public' . $file;
38
        } else if (!file_exists($this->uploadsDirectory . $file)) {
39
            throw new Exception($path . ' not found.', 404);
40
        } else {
41
            $path = $this->uploadsDirectory . $file;
42
        }
43
        
44
        // magic_mime module installed?
45
        if (function_exists('mime_content_type')) {
46
            $mimeType = mime_content_type($path);
47
        } else if (function_exists('finfo_file')) { // or fileinfo module installed?
48
            $finfo = finfo_open(FILEINFO_MIME); // return mime type
49
            $mimeType = finfo_file($finfo, $path);
50
            finfo_close($finfo);
51
        }
52
53
        $contents = file_get_contents($path);
54
        $stream = new Stream('php://memory', 'r+');
55
        $stream->write($contents);
56
        $response = new Response();
57
        $response = $response->withBody($stream);
58
        $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...
59
60
        return $response;
61
    }
62
}