AbstractDoctrineLoader::find()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.6666
cc 3
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\Binary\Loader;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
16
17
abstract class AbstractDoctrineLoader implements LoaderInterface
18
{
19
    /**
20
     * @var ObjectManager
21
     */
22
    protected $manager;
23
24
    /**
25
     * @var string
26
     */
27
    protected $class;
28
29
    /**
30
     * @param string $class
31
     */
32
    public function __construct(ObjectManager $manager, $class = null)
33
    {
34
        $this->manager = $manager;
35
        $this->class = $class;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function find($path)
42
    {
43
        $image = $this->manager->find($this->class, $this->mapPathToId($path));
44
45
        if (!$image) {
46
            // try to find the image without extension
47
            $info = pathinfo($path);
48
            $name = $info['dirname'].'/'.$info['filename'];
49
50
            $image = $this->manager->find($this->class, $this->mapPathToId($name));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $image is correct as $this->manager->find($th...is->mapPathToId($name)) (which targets Doctrine\Persistence\ObjectManager::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
51
        }
52
53
        if (!$image) {
54
            throw new NotLoadableException(sprintf('Source image was not found with id "%s"', $path));
55
        }
56
57
        return stream_get_contents($this->getStreamFromImage($image));
58
    }
59
60
    /**
61
     * Map the requested path (ie. subpath in the URL) to an id that can be used to lookup the image in the Doctrine store.
62
     *
63
     * @param string $path
64
     *
65
     * @return string
66
     */
67
    abstract protected function mapPathToId($path);
68
69
    /**
70
     * Return a stream resource from the Doctrine entity/document with the image content.
71
     *
72
     * @param object $image
73
     *
74
     * @return resource
75
     */
76
    abstract protected function getStreamFromImage($image);
77
}
78