AssetResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace IQParts\Content;
4
5
use GuzzleHttp\Psr7\Stream;
6
use IQParts\Content\Object\CacheControl;
7
use IQParts\Content\Object\File;
8
use League\Flysystem\FileNotFoundException;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Class AssetResolver
14
 * @package IQParts\Framework\Content
15
 */
16
final class AssetResolver
17
{
18
    /**
19
     * @var MimeDetector
20
     */
21
    private $detector;
22
    /**
23
     * @var Filesystem
24
     */
25
    private $filesystem;
26
27
    /**
28
     * AssetResolver constructor.
29
     * @param Filesystem $filesystem
30
     * @internal param string $assetDir
31
     */
32
    public function __construct(Filesystem $filesystem )
33
    {
34
        $this->detector = new MimeDetector();
35
        $this->filesystem = $filesystem ;
36
    }
37
38
    /**
39
     * @param string $file
40
     * @return bool
41
     */
42
    public function exists(string $file): bool
43
    {
44
        // Will never throw as we check if the file exists.
45
        return $this->filesystem->exists($file) && $this->filesystem->get($file) instanceof File;
46
    }
47
48
    /**
49
     * @param RequestInterface $request
50
     * @param ResponseInterface $response
51
     * @param CacheControl $cacheControl
52
     * @param bool $inline
53
     * @return ResponseInterface|bool
54
     * @throws FileNotFoundException
55
     */
56
    public function resolve(
57
        RequestInterface $request,
58
        ResponseInterface $response,
59
        CacheControl $cacheControl,
60
        bool $inline = true
61
    )
62
    {
63
        $target = $request->getRequestTarget();
64
65
        $file = $this->filesystem->get($target);
66
67
        /** @var ResponseInterface $response */
68
        $response = $response->withHeader('Cache-Control', (string) $cacheControl);
69
        $response = $response->withHeader('Content-Type', $this->detector->detectFromExtension(
70
            $file->getBasename())
71
        );
72
73
        if ($inline) {
74
            $response = $response->withHeader('Content-Disposition', 'inline; filename="' . $file->getBasename() . '";');
75
        } else {
76
            $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . $file->getBasename() . '";');
77
        }
78
        $response = $response->withHeader('Content-Length', (string)$file->getSize());
0 ignored issues
show
Bug introduced by
The method getSize does only exist in IQParts\Content\Object\File, but not in IQParts\Content\Object\Folder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
80
        /**
81
         * Will always return a stream
82
         */
83
        $stream = $file->getStream();
0 ignored issues
show
Bug introduced by
The method getStream does only exist in IQParts\Content\Object\File, but not in IQParts\Content\Object\Folder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
        return $response->withBody(new Stream($stream))->withStatus(200);
85
    }
86
}