/
OGExtractor.php
40 lines (33 loc) · 1.78 KB
/
OGExtractor.php
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
<?php
namespace SeoBundle\MetaData\Extractor\ThirdParty\CoreShop;
use Pimcore\Tool;
use SeoBundle\MetaData\Extractor\ExtractorInterface;
use SeoBundle\Model\SeoMetaDataInterface;
final class OGExtractor implements ExtractorInterface
{
public function supports(mixed $element): bool
{
return $element instanceof \CoreShop\Component\SEO\Model\SEOOpenGraphAwareInterface;
}
public function updateMetadata(mixed $element, ?string $locale, SeoMetaDataInterface $seoMetadata): void
{
if (method_exists($element, 'getMetaTitle') && !empty($element->getOGTitle($locale))) {
$seoMetadata->addExtraProperty('og:title', $element->getOGTitle($locale));
} elseif (method_exists($element, 'getName') && !empty($element->getName($locale))) {
$seoMetadata->addExtraProperty('og:title', $element->getName($locale));
}
if (method_exists($element, 'getOGDescription') && !empty($element->getOGDescription($locale))) {
$seoMetadata->addExtraProperty('og:description', $element->getOGDescription($locale));
} elseif (method_exists($element, 'getShortDescription') && !empty($element->getShortDescription($locale))) {
$seoMetadata->addExtraProperty('og:description', $element->getShortDescription($locale));
}
if (method_exists($element, 'getOGType') && !empty($element->getOGType())) {
$seoMetadata->addExtraProperty('og:type', $element->getOGType());
}
if (method_exists($element, 'getImage') && !empty($element->getImage())) {
$path = $element->getImage()->getThumbnail('coreshop_seo');
$ogImage = (str_starts_with('http', $path) ? '' : Tool::getHostUrl()).$path;
$seoMetadata->addExtraProperty('og:image', $ogImage);
}
}
}