Completed
Push — feature-20rc1 ( 008ae2 )
by Rob
16:55
created

FlysystemLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Imagine\Data\Loader;
13
14
use League\Flysystem\FilesystemInterface;
15
use Liip\ImagineBundle\Exception\File\Loader\NotLoadableException;
16
use Liip\ImagineBundle\File\FileBlob;
17
use Liip\ImagineBundle\File\FileInterface;
18
19
class FlysystemLoader implements LoaderInterface
20
{
21
    /**
22
     * @var FilesystemInterface
23
     */
24
    private $filesystem;
25
26
    /**
27
     * @param FilesystemInterface $filesystem
28
     */
29
    public function __construct(FilesystemInterface $filesystem)
30
    {
31
        $this->filesystem = $filesystem;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function find(string $identity): FileInterface
38
    {
39
        if (false === $this->filesystem->has($identity)) {
40
            throw new NotLoadableException('Source image "%s" not found.', $identity);
41
        }
42
43
        try {
44
            $file = $this->filesystem->read($identity);
45
            $type = $this->filesystem->getMimetype($identity);
46
        } catch (\Exception $e) {
47
            throw new NotLoadableException('Failed to load "%s" from flysystem service!', $identity, $e);
48
        }
49
50
        return FileBlob::create($file, $type);
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by $this->filesystem->read($identity) on line 44 can also be of type false; however, Liip\ImagineBundle\File\FileBlob::create() does only seem to accept null|string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $type defined by $this->filesystem->getMimetype($identity) on line 45 can also be of type false; however, Liip\ImagineBundle\File\FileBlob::create() does only seem to accept null|string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
51
    }
52
}
53