1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\OpenGraphBundle\Templating\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
7
|
|
|
use Netgen\Bundle\OpenGraphBundle\MetaTag\CollectorInterface; |
8
|
|
|
use Netgen\Bundle\OpenGraphBundle\MetaTag\RendererInterface; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
class NetgenOpenGraphRuntime |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Netgen\Bundle\OpenGraphBundle\MetaTag\CollectorInterface |
15
|
|
|
*/ |
16
|
|
|
protected $tagCollector; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Netgen\Bundle\OpenGraphBundle\MetaTag\RendererInterface |
20
|
|
|
*/ |
21
|
|
|
protected $tagRenderer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Psr\Log\LoggerInterface |
25
|
|
|
*/ |
26
|
|
|
protected $logger; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected $throwExceptions = true; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param \Netgen\Bundle\OpenGraphBundle\MetaTag\CollectorInterface $tagCollector |
37
|
|
|
* @param \Netgen\Bundle\OpenGraphBundle\MetaTag\RendererInterface $tagRenderer |
38
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
CollectorInterface $tagCollector, |
42
|
|
|
RendererInterface $tagRenderer, |
43
|
|
|
LoggerInterface $logger = null |
44
|
|
|
) { |
45
|
|
|
$this->tagCollector = $tagCollector; |
46
|
|
|
$this->tagRenderer = $tagRenderer; |
47
|
|
|
$this->logger = $logger; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets the flag that determines if the exceptions will thrown instead of logged. |
52
|
|
|
* |
53
|
|
|
* @param bool $throwExceptions |
54
|
|
|
*/ |
55
|
|
|
public function setThrowExceptions($throwExceptions = true) |
56
|
|
|
{ |
57
|
|
|
$this->throwExceptions = $throwExceptions; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Renders Open Graph tags for provided content. |
62
|
|
|
* |
63
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Content $content |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function renderOpenGraphTags(Content $content) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
return $this->tagRenderer->render( |
71
|
|
|
$this->getOpenGraphTags($content) |
72
|
|
|
); |
73
|
|
|
} catch (Exception $e) { |
74
|
|
|
if ($this->throwExceptions || !$this->logger instanceof LoggerInterface) { |
75
|
|
|
throw $e; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->logger->error($e->getMessage()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns Open Graph tags for provided content. |
86
|
|
|
* |
87
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Content $content |
88
|
|
|
* |
89
|
|
|
* @return \Netgen\Bundle\OpenGraphBundle\MetaTag\Item[] |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function getOpenGraphTags(Content $content) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
return $this->tagCollector->collect($content); |
95
|
|
|
} catch (Exception $e) { |
96
|
|
|
if ($this->throwExceptions || !$this->logger instanceof LoggerInterface) { |
97
|
|
|
throw $e; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->logger->error($e->getMessage()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return array(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.