1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Factory\Uploadable; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Doctrine\Persistence\ObjectRepository; |
18
|
|
|
use League\Flysystem\Filesystem; |
19
|
|
|
use Silverback\ApiComponentsBundle\Imagine\Entity\ImagineCachedFileMetadata; |
20
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\MediaObject; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Daniel West <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class MediaObjectFactory |
26
|
|
|
{ |
27
|
|
|
private ObjectRepository $respository; |
28
|
|
|
|
29
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
30
|
|
|
{ |
31
|
|
|
$this->respository = $entityManager->getRepository(ImagineCachedFileMetadata::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function create(Filesystem $filesystem, string $filename, string $imagineFilter = null): MediaObject |
35
|
|
|
{ |
36
|
|
|
$mediaObject = new MediaObject(); |
37
|
|
|
$mediaObject->contentUrl = 'https://www.website.com/path'; |
38
|
|
|
$mediaObject->fileSize = $filesystem->fileSize($filename); |
39
|
|
|
$mediaObject->mimeType = $filesystem->mimeType($filename); |
40
|
|
|
$mediaObject->imagineFilter = $imagineFilter; |
41
|
|
|
|
42
|
|
|
if (false !== strpos($mediaObject->mimeType, 'image/')) { |
43
|
|
|
$file = $filesystem->read($filename); |
44
|
|
|
if ('image/svg+xml' === $mediaObject->mimeType) { |
45
|
|
|
$xmlget = simplexml_load_string(file_get_contents($file)); |
46
|
|
|
$xmlattributes = $xmlget->attributes(); |
47
|
|
|
$mediaObject->width = (int) $xmlattributes->width; |
48
|
|
|
$mediaObject->height = (int) $xmlattributes->height; |
49
|
|
|
} else { |
50
|
|
|
[ $mediaObject->width, $mediaObject->height ] = @getimagesize($file); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $mediaObject; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function createFromImagine(string $contentUrl, string $path, string $imagineFilter): MediaObject |
58
|
|
|
{ |
59
|
|
|
$mediaObject = new MediaObject(); |
60
|
|
|
$mediaObject->contentUrl = $contentUrl; |
61
|
|
|
$mediaObject->imagineFilter = $imagineFilter; |
62
|
|
|
|
63
|
|
|
/** @var ImagineCachedFileMetadata|null $cachedFileMetadata */ |
64
|
|
|
$cachedFileMetadata = $this->respository |
65
|
|
|
->findOneBy( |
66
|
|
|
[ |
67
|
|
|
'filter' => $imagineFilter, |
68
|
|
|
'path' => $path, |
69
|
|
|
] |
70
|
|
|
); |
71
|
|
|
if ($cachedFileMetadata) { |
72
|
|
|
$mediaObject->fileSize = $cachedFileMetadata->fileSize; |
73
|
|
|
$mediaObject->mimeType = $cachedFileMetadata->mimeType; |
74
|
|
|
$mediaObject->width = $cachedFileMetadata->width; |
75
|
|
|
$mediaObject->height = $cachedFileMetadata->height; |
76
|
|
|
} else { |
77
|
|
|
$mediaObject->width = $mediaObject->height = $mediaObject->fileSize = -1; |
78
|
|
|
$mediaObject->mimeType = ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $mediaObject; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|