• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

liip / LiipImagineBundle / 10701363663

04 Sep 2024 12:11PM UTC coverage: 81.688% (-0.06%) from 81.746%
10701363663

push

github

web-flow
Merge pull request #1610 from liip/cs-fix

apply latest cs fixer

67 of 84 new or added lines in 42 files covered. (79.76%)

2 existing lines in 1 file now uncovered.

2275 of 2785 relevant lines covered (81.69%)

103.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.4
/Imagine/Cache/Resolver/AwsS3Resolver.php
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\Cache\Resolver;
13

14
use Aws\S3\S3Client;
15
use Liip\ImagineBundle\Binary\BinaryInterface;
16
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotStorableException;
17
use Psr\Log\LoggerInterface;
18

19
class AwsS3Resolver implements ResolverInterface
20
{
21
    /**
22
     * @var S3Client
23
     */
24
    protected $storage;
25

26
    /**
27
     * @var string
28
     */
29
    protected $bucket;
30

31
    /**
32
     * @var string
33
     */
34
    protected $acl;
35

36
    /**
37
     * @var array
38
     */
39
    protected $getOptions;
40

41
    /**
42
     * Object options added to PUT requests.
43
     *
44
     * @var array
45
     */
46
    protected $putOptions;
47

48
    /**
49
     * @var LoggerInterface
50
     */
51
    protected $logger;
52

53
    /**
54
     * @var string
55
     */
56
    protected $cachePrefix;
57

58
    /**
59
     * Constructs a cache resolver storing images on Amazon S3.
60
     *
61
     * @param S3Client $storage    The Amazon S3 storage API. It's required to know authentication information
62
     * @param string   $bucket     The bucket name to operate on
63
     * @param string   $acl        The ACL to use when storing new objects. Default: owner read/write, public read
64
     * @param array    $getOptions A list of options to be passed when retrieving the object url from Amazon S3
65
     * @param array    $putOptions A list of options to be passed when saving the object to Amazon S3
66
     */
67
    public function __construct(S3Client $storage, $bucket, $acl = 'public-read', array $getOptions = [], $putOptions = [])
68
    {
69
        $this->storage = $storage;
208✔
70
        $this->bucket = $bucket;
208✔
71
        $this->acl = $acl;
208✔
72
        $this->getOptions = $getOptions;
208✔
73
        $this->putOptions = $putOptions;
208✔
74
    }
112✔
75

76
    public function setLogger(LoggerInterface $logger)
77
    {
78
        $this->logger = $logger;
39✔
79
    }
21✔
80

81
    /**
82
     * @param string $cachePrefix
83
     */
84
    public function setCachePrefix($cachePrefix)
85
    {
86
        $this->cachePrefix = $cachePrefix;
×
87
    }
88

89
    public function isStored($path, $filter)
90
    {
91
        return $this->objectExists($this->getObjectPath($path, $filter));
13✔
92
    }
93

94
    public function resolve($path, $filter)
95
    {
96
        return $this->getObjectUrl($this->getObjectPath($path, $filter));
39✔
97
    }
98

99
    public function store(BinaryInterface $binary, $path, $filter)
100
    {
101
        $objectPath = $this->getObjectPath($path, $filter);
39✔
102

103
        try {
104
            $this->storage->putObject(
39✔
105
                array_merge(
39✔
106
                    $this->putOptions,
39✔
107
                    [
18✔
108
                        'ACL' => $this->acl,
39✔
109
                        'Bucket' => $this->bucket,
39✔
110
                        'Key' => $objectPath,
39✔
111
                        'Body' => $binary->getContent(),
39✔
112
                        'ContentType' => $binary->getMimeType(),
39✔
113
                    ]
18✔
114
                )
18✔
115
            );
18✔
116
        } catch (\Exception $e) {
13✔
117
            $this->logError('The object could not be created on Amazon S3.', [
13✔
118
                'objectPath' => $objectPath,
13✔
119
                'filter' => $filter,
13✔
120
                'bucket' => $this->bucket,
13✔
121
                'exception' => $e,
13✔
122
            ]);
6✔
123

124
            throw new NotStorableException('The object could not be created on Amazon S3.', $e->getCode(), $e);
13✔
125
        }
126
    }
14✔
127

128
    public function remove(array $paths, array $filters)
129
    {
130
        if (empty($paths) && empty($filters)) {
117✔
131
            return;
13✔
132
        }
133

134
        if (empty($paths)) {
104✔
135
            try {
136
                $this->storage->deleteMatchingObjects($this->bucket, null, \sprintf(
39✔
137
                    '/%s/i',
39✔
138
                    implode('|', $filters)
39✔
139
                ));
18✔
140
            } catch (\Exception $e) {
13✔
141
                $this->logError('The objects could not be deleted from Amazon S3.', [
13✔
142
                    'filter' => implode(', ', $filters),
13✔
143
                    'bucket' => $this->bucket,
13✔
144
                    'exception' => $e,
13✔
145
                ]);
6✔
146
            }
147

148
            return;
39✔
149
        }
150

151
        foreach ($filters as $filter) {
65✔
152
            foreach ($paths as $path) {
65✔
153
                $objectPath = $this->getObjectPath($path, $filter);
65✔
154
                if (!$this->objectExists($objectPath)) {
65✔
155
                    continue;
13✔
156
                }
157

158
                try {
159
                    $this->storage->deleteObject([
52✔
160
                        'Bucket' => $this->bucket,
52✔
161
                        'Key' => $objectPath,
52✔
162
                    ]);
24✔
163
                } catch (\Exception $e) {
39✔
164
                    $this->logError('The object could not be deleted from Amazon S3.', [
39✔
165
                        'objectPath' => $objectPath,
39✔
166
                        'filter' => $filter,
39✔
167
                        'bucket' => $this->bucket,
39✔
168
                        'exception' => $e,
39✔
169
                    ]);
18✔
170
                }
171
            }
172
        }
173
    }
35✔
174

175
    /**
176
     * Sets a single option to be passed when retrieving an objects URL.
177
     *
178
     * If the option is already set, it will be overwritten.
179
     *
180
     * @see S3Client::getObjectUrl() for available options
181
     *
182
     * @param string $key   The name of the option
183
     * @param mixed  $value The value to be set
184
     *
185
     * @return $this
186
     */
187
    public function setGetOption($key, $value)
188
    {
189
        $this->getOptions[$key] = $value;
13✔
190

191
        return $this;
13✔
192
    }
193

194
    /**
195
     * Sets a single option to be passed when saving an object.
196
     *
197
     * If the option is already set, it will be overwritten.
198
     *
199
     * @see S3Client::putObject() for available options
200
     *
201
     * @param string $key   The name of the option
202
     * @param mixed  $value The value to be set
203
     *
204
     * @return $this
205
     */
206
    public function setPutOption($key, $value)
207
    {
208
        $this->putOptions[$key] = $value;
13✔
209

210
        return $this;
13✔
211
    }
212

213
    /**
214
     * Returns the object path within the bucket.
215
     *
216
     * @param string $path   The base path of the resource
217
     * @param string $filter The name of the imagine filter in effect
218
     *
219
     * @return string The path of the object on S3
220
     */
221
    protected function getObjectPath($path, $filter)
222
    {
223
        $path = $this->cachePrefix
156✔
NEW
224
            ? \sprintf('%s/%s/%s', $this->cachePrefix, $filter, $path)
×
225
            : \sprintf('%s/%s', $filter, $path);
156✔
226

227
        return str_replace('//', '/', $path);
156✔
228
    }
229

230
    /**
231
     * Returns the URL for an object saved on Amazon S3.
232
     *
233
     * @param string $path
234
     *
235
     * @return string
236
     */
237
    protected function getObjectUrl($path)
238
    {
239
        return $this->storage->getObjectUrl($this->bucket, $path, 0, $this->getOptions);
39✔
240
    }
241

242
    /**
243
     * Checks whether an object exists.
244
     *
245
     * @param string $objectPath
246
     *
247
     * @return bool
248
     */
249
    protected function objectExists($objectPath)
250
    {
251
        return $this->storage->doesObjectExist($this->bucket, $objectPath);
78✔
252
    }
253

254
    protected function logError($message, array $context = [])
255
    {
256
        if ($this->logger) {
65✔
257
            $this->logger->error($message, $context);
39✔
258
        }
259
    }
35✔
260
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc