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

DataManager::find()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 2
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;
13
14
use Liip\ImagineBundle\Exception\File\Attributes\Resolver\InvalidFileAttributesException;
15
use Liip\ImagineBundle\Exception\Imagine\Data\InvalidFileFoundException;
16
use Liip\ImagineBundle\File\Attributes\Attributes;
17
use Liip\ImagineBundle\File\Attributes\Resolver\FileAttributesApplier;
18
use Liip\ImagineBundle\File\FileInterface;
19
use Liip\ImagineBundle\Imagine\Data\Loader\LoaderInterface;
20
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
21
22
class DataManager
23
{
24
    /**
25
     * @var FileAttributesApplier
26
     */
27
    protected $fileAttributes;
28
29
    /**
30
     * @var FilterConfiguration
31
     */
32
    protected $filterConfig;
33
34
    /**
35
     * @var string|null
36
     */
37
    protected $defaultLoader;
38
39
    /**
40
     * @var string|null
41
     */
42
    protected $globalDefaultImage;
43
44
    /**
45
     * @var LoaderInterface[]
46
     */
47
    protected $loaders = [];
48
49
    /**
50
     * @param FilterConfiguration   $filterConfig
51
     * @param FileAttributesApplier $fileAttributesApplier
52
     * @param string                $defaultLoader
53
     * @param string                $globalDefaultImage
54
     */
55
    public function __construct(
56
        FilterConfiguration $filterConfig,
57
        FileAttributesApplier $fileAttributesApplier,
58
        string $defaultLoader = null,
59
        string $globalDefaultImage = null
60
    ) {
61
        $this->filterConfig = $filterConfig;
62
        $this->fileAttributes = $fileAttributesApplier;
63
        $this->defaultLoader = $defaultLoader;
64
        $this->globalDefaultImage = $globalDefaultImage;
65
    }
66
67
    /**
68
     * Adds a loader to retrieve images for the given filter.
69
     *
70
     * @param string          $filter
71
     * @param LoaderInterface $loader
72
     */
73
    public function addLoader(string $filter, LoaderInterface $loader): void
74
    {
75
        $this->loaders[$filter] = $loader;
76
    }
77
78
    /**
79
     * Returns a loader previously attached to the given filter.
80
     *
81
     * @param string $filter
82
     *
83
     * @throws \InvalidArgumentException
84
     *
85
     * @return LoaderInterface
86
     */
87
    public function getLoader(string $filter): LoaderInterface
88
    {
89
        $config = $this->filterConfig->get($filter);
90
        $loader = empty($config['data_loader']) ? $this->defaultLoader : $config['data_loader'];
91
92
        if (!isset($this->loaders[$loader])) {
93
            throw new \InvalidArgumentException(sprintf(
94
                'Could not find data loader "%s" for "%s" filter type',
95
                $loader, $filter
96
            ));
97
        }
98
99
        return $this->loaders[$loader];
100
    }
101
102
    /**
103
     * Retrieves an image with the given filter applied.
104
     *
105
     * @param string $filter
106
     * @param string $path
107
     *
108
     * @throws \LogicException
109
     *
110
     * @return FileInterface
111
     */
112
    public function find(string $filter, string $path): FileInterface
113
    {
114
        $file = $this->getLoader($filter)->find($path);
115
116
        try {
117
            $file = $this->fileAttributes->apply($file);
118
        } catch (InvalidFileAttributesException $e) {
119
            throw new InvalidFileFoundException('Invalid attributes resolved for "%s".', $path, $e);
120
        }
121
122
        if (!$file->getContentType()->isMatch('image')) {
123
            throw new InvalidFileFoundException(
124
                'Invalid content type attribute "%s" for "%s" (expected primary content type "image" but got "%s").',
125
                $file->getContentType()->stringify(), $path, $file->getContentType()->getType() ?: 'null'
126
            );
127
        }
128
129
        return $file;
130
    }
131
132
    /**
133
     * Get default image url with the given filter applied.
134
     *
135
     * @param string $filter
136
     *
137
     * @return string|null
138
     */
139
    public function getDefaultImageUrl(string $filter): ?string
140
    {
141
        $config = $this
142
            ->filterConfig
143
            ->get($filter);
144
145
        if (!empty($config['default_image'])) {
146
            return $config['default_image'];
147
        }
148
149
        if (!empty($this->globalDefaultImage)) {
150
            return $this->globalDefaultImage;
151
        }
152
153
        return null;
154
    }
155
}
156