|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Api\FileException; |
|
8
|
|
|
use Application\FriendlyException; |
|
9
|
|
|
use Application\Repository\NewsRepository; |
|
10
|
|
|
use Application\Traits\HasImage; |
|
11
|
|
|
use Application\Traits\HasSite; |
|
12
|
|
|
use Application\Traits\HasSiteInterface; |
|
13
|
|
|
use Application\Traits\HasSorting; |
|
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
15
|
|
|
use Ecodev\Felix\Model\Traits\HasName; |
|
16
|
|
|
use Ecodev\Felix\Model\Traits\HasUrl; |
|
17
|
|
|
use GraphQL\Doctrine\Attribute as API; |
|
18
|
|
|
use Imagine\Image\ImagineInterface; |
|
19
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
20
|
|
|
use Throwable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A news. |
|
24
|
|
|
*/ |
|
25
|
|
|
#[ORM\Entity(NewsRepository::class)] |
|
26
|
|
|
class News extends AbstractModel implements HasSiteInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private const IMAGE_PATH = 'htdocs/news-images/'; |
|
29
|
|
|
|
|
30
|
|
|
use HasImage { |
|
31
|
|
|
setFile as traitSetFile; |
|
32
|
|
|
} |
|
33
|
|
|
use HasName; |
|
34
|
|
|
use HasSite; |
|
35
|
|
|
use HasSorting; |
|
36
|
|
|
use HasUrl; |
|
37
|
|
|
|
|
38
|
|
|
#[ORM\Column(type: 'text')] |
|
39
|
|
|
private string $description = ''; |
|
40
|
|
|
|
|
41
|
|
|
#[ORM\Column(type: 'boolean', options: ['default' => false])] |
|
42
|
|
|
private bool $isActive = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set description. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setDescription(string $description): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->description = $description; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get description. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getDescription(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->description; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Relative URL to image. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getImageUrl(): string |
|
64
|
|
|
{ |
|
65
|
|
|
return '/news-images/' . $this->getFilename(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function isActive(): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->isActive; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setIsActive(bool $isActive): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->isActive = $isActive; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set the image file and encode it to WebP. |
|
80
|
|
|
*/ |
|
81
|
|
|
#[API\Input(type: '?GraphQL\Upload\UploadType')] |
|
82
|
|
|
public function setFile(UploadedFileInterface $file): void |
|
83
|
|
|
{ |
|
84
|
|
|
global $container; |
|
85
|
|
|
|
|
86
|
|
|
$this->traitSetFile($file); |
|
87
|
|
|
|
|
88
|
|
|
$mime = $this->getMime(); |
|
89
|
|
|
|
|
90
|
|
|
if ($mime === 'image/svg+xml') { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
|
|
/** @var ImagineInterface $imagine */ |
|
96
|
|
|
$imagine = $container->get(ImagineInterface::class); |
|
97
|
|
|
$image = FriendlyException::try(fn () => $imagine->open($this->getPath())); |
|
98
|
|
|
|
|
99
|
|
|
// Resize if too large |
|
100
|
|
|
$size = $image->getSize(); |
|
101
|
|
|
if ($size->getWidth() > 1200 || $size->getHeight() > 1200) { |
|
102
|
|
|
$image = $image->thumbnail(new \Imagine\Image\Box(1200, 1200)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Delete original file |
|
106
|
|
|
unlink($this->getPath()); |
|
107
|
|
|
|
|
108
|
|
|
// Save as WebP |
|
109
|
|
|
$this->setFilename(pathinfo($this->getFilename(), PATHINFO_FILENAME) . '.webp'); |
|
|
|
|
|
|
110
|
|
|
FriendlyException::try(fn () => $image->save($this->getPath())); |
|
111
|
|
|
} catch (Throwable $e) { |
|
112
|
|
|
throw new FileException($file, $e); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|