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

StreamLoader::find()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 30
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 6
eloc 12
nc 4
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 Liip\ImagineBundle\Exception\File\Loader\NotLoadableException;
15
use Liip\ImagineBundle\File\FileBlob;
16
use Liip\ImagineBundle\File\FileInterface;
17
18
class StreamLoader implements LoaderInterface
19
{
20
    /**
21
     * The wrapper prefix to append to the path to be loaded.
22
     *
23
     * @var string
24
     */
25
    protected $wrapperPrefix;
26
27
    /**
28
     * A stream context resource to use.
29
     *
30
     * @var resource|null
31
     */
32
    protected $context;
33
34
    /**
35
     * @param string        $wrapperPrefix
36
     * @param resource|null $context
37
     *
38
     * @throws \InvalidArgumentException
39
     */
40
    public function __construct(string $wrapperPrefix, $context = null)
41
    {
42
        $this->wrapperPrefix = $wrapperPrefix;
43
44
        if ($context && !is_resource($context)) {
45
            throw new \InvalidArgumentException('The given context is no valid resource.');
46
        }
47
48
        $this->context = empty($context) ? null : $context;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function find(string $identity): FileInterface
55
    {
56
        $name = $this->wrapperPrefix.$identity;
57
58
        /*
59
         * This looks strange, but at least in PHP 5.3.8 it will raise an E_WARNING if the 4th parameter is null.
60
         * fopen() will be called only once with the correct arguments.
61
         *
62
         * The error suppression is solely to determine whether the file exists.
63
         * file_exists() is not used as not all wrappers support stat() to actually check for existing resources.
64
         */
65
        if (($this->context && !$resource = @fopen($name, 'r', null, $this->context)) || !$resource = @fopen($name, 'r')) {
66
            throw new NotLoadableException('Source image %s not found.', $name);
67
        }
68
69
        // Closing the opened stream to avoid locking of the resource to find.
70
        fclose($resource);
71
72
        try {
73
            $content = file_get_contents($name, null, $this->context);
74
        } catch (\Exception $e) {
75
            throw new NotLoadableException('Source image %s could not be loaded.', $name, $e);
76
        }
77
78
        if (false === $content) {
79
            throw new NotLoadableException('Source image %s could not be loaded.', $name);
80
        }
81
82
        return FileBlob::create($content);
83
    }
84
}
85