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\File\Attributes\Resolver; |
13
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Exception\File\Attributes\Resolver\InvalidFileAttributesException; |
15
|
|
|
use Liip\ImagineBundle\File\Attributes\ContentTypeAttribute; |
16
|
|
|
use Liip\ImagineBundle\File\Attributes\ExtensionAttribute; |
17
|
|
|
use Liip\ImagineBundle\File\FileBlob; |
18
|
|
|
use Liip\ImagineBundle\File\FileBlobInterface; |
19
|
|
|
use Liip\ImagineBundle\File\FileInterface; |
20
|
|
|
use Liip\ImagineBundle\File\FilePath; |
21
|
|
|
use Liip\ImagineBundle\File\FilePathInterface; |
22
|
|
|
use Psr\Log\LoggerAwareTrait; |
23
|
|
|
use Psr\Log\NullLogger; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Rob Frawley 2nd <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class FileAttributesApplier |
29
|
|
|
{ |
30
|
|
|
use LoggerAwareTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FileAttributesResolver |
34
|
|
|
*/ |
35
|
|
|
private $resolver; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param FileAttributesResolver $resolver |
39
|
|
|
*/ |
40
|
|
|
public function __construct(FileAttributesResolver $resolver) |
41
|
|
|
{ |
42
|
|
|
$this->resolver = $resolver; |
43
|
|
|
$this->logger = new NullLogger(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param FileInterface|FilePathInterface $file |
48
|
|
|
* |
49
|
|
|
* @return FileInterface|FilePathInterface |
50
|
|
|
*/ |
51
|
|
|
public function apply(FileInterface $file): FileInterface |
52
|
|
|
{ |
53
|
|
View Code Duplication |
if (!$file->hasContentType()) { |
|
|
|
|
54
|
|
|
$attr = $this->resolver->resolve($file); |
55
|
|
|
|
56
|
|
|
return $this->assignFileAttributes( |
57
|
|
|
$file, |
58
|
|
|
$attr->getContentType(), |
59
|
|
|
$attr->getExtension() |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
if (!$file->hasExtension()) { |
|
|
|
|
64
|
|
|
$attr = $this->resolver->resolve($file); |
65
|
|
|
|
66
|
|
|
return $this->assignFileAttributes( |
67
|
|
|
$file, |
68
|
|
|
$file->getContentType(), |
69
|
|
|
$attr->getExtension() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $file; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param FileInterface|FileBlobInterface|FilePathInterface $file |
78
|
|
|
* @param ContentTypeAttribute $contentType |
79
|
|
|
* @param ExtensionAttribute $extension |
80
|
|
|
* |
81
|
|
|
* @return FileInterface|FileBlobInterface|FilePathInterface |
82
|
|
|
*/ |
83
|
|
|
private function assignFileAttributes(FileInterface $file, ContentTypeAttribute $contentType, ExtensionAttribute $extension): FileInterface |
84
|
|
|
{ |
85
|
|
View Code Duplication |
if (false === $contentType->isValid()) { |
|
|
|
|
86
|
|
|
$this->logger->error($m = sprintf( |
87
|
|
|
'Unable to resolve content type attribute for file %s.', |
88
|
|
|
$file->hasFile() ? $file->getFile()->getPathname() : 'blob' |
|
|
|
|
89
|
|
|
)); |
90
|
|
|
|
91
|
|
|
throw new InvalidFileAttributesException($m); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
if (false === $extension->isValid()) { |
|
|
|
|
95
|
|
|
$this->logger->error($m = sprintf( |
96
|
|
|
'Unable to resolve extension attribute for file %s.', |
97
|
|
|
$file->hasFile() ? $file->getFile()->getPathname() : 'blob' |
|
|
|
|
98
|
|
|
)); |
99
|
|
|
|
100
|
|
|
throw new InvalidFileAttributesException($m); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $file instanceof FilePathInterface |
104
|
|
|
? new FilePath($file->getFile(), $contentType, $extension) |
105
|
|
|
: new FileBlob($file->getContents(), $contentType, $extension); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.