-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathImage.php
More file actions
77 lines (63 loc) · 2.9 KB
/
Image.php
File metadata and controls
77 lines (63 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace Netgen\Bundle\OpenGraphBundle\Handler\FieldType;
use Exception;
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidVariationException;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\Variation\VariationHandler;
use Ibexa\Core\FieldType\Image\Value;
use Ibexa\Core\Helper\FieldHelper;
use Ibexa\Core\MVC\Exception\SourceImageNotFoundException;
use Netgen\Bundle\OpenGraphBundle\Exception\FieldEmptyException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\RequestStack;
use function ltrim;
use function str_starts_with;
final class Image extends Handler
{
public function __construct(
FieldHelper $fieldHelper,
private readonly VariationHandler $imageVariationService,
private readonly RequestStack $requestStack,
private ?LoggerInterface $logger = null,
) {
parent::__construct($fieldHelper);
$this->logger ??= new NullLogger();
}
protected function getFieldValue(Field $field, string $tagName, array $params = []): string
{
if (!$this->fieldHelper->isFieldEmpty($this->content, $field->fieldDefIdentifier)) {
$variationName = !empty($params[1]) ? $params[1] : 'opengraph';
try {
$variationUri = $this->imageVariationService->getVariation($field, $this->content->getVersionInfo(), $variationName)->uri;
if (str_starts_with($variationUri, '/') && ($request = $this->requestStack->getCurrentRequest()) !== null) {
$variationUri = $request->getUriForPath('/' . ltrim($variationUri, '/'));
}
return $variationUri;
} catch (InvalidVariationException $e) {
$this->logger->error("Open Graph image handler: Couldn't get variation '{$variationName}' for image with id {$field->value->id}");
} catch (SourceImageNotFoundException $e) {
$this->logger->error(
"Open Graph image handler: Couldn't create variation '{$variationName}' for image with id {$field->value->id} because source image can't be found",
);
} catch (Exception $e) {
$this->logger->error(
"Open Graph image handler: Error while getting variation '{$variationName}' for image with id {$field->value->id}: " . $e->getMessage(),
);
}
}
throw new FieldEmptyException($field->fieldDefIdentifier);
}
protected function getFallbackValue(string $tagName, array $params = []): string
{
if (!empty($params[2]) && ($request = $this->requestStack->getCurrentRequest()) !== null) {
return $request->getUriForPath('/' . ltrim($params[2], '/'));
}
return '';
}
protected function supports(Field $field): bool
{
return $field->value instanceof Value;
}
}