These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\NodeBundle\Helper; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface; |
||
7 | use Kunstmaan\NodeBundle\Validation\URLValidator; |
||
8 | use Psr\Log\LoggerInterface; |
||
9 | use Symfony\Component\Routing\RouterInterface; |
||
10 | |||
11 | /** |
||
12 | * A helper for replacing url's |
||
13 | */ |
||
14 | class URLHelper |
||
15 | { |
||
16 | use URLValidator; |
||
17 | |||
18 | /** |
||
19 | * @var EntityManager |
||
20 | */ |
||
21 | private $em; |
||
22 | |||
23 | /** |
||
24 | * @var RouterInterface |
||
25 | */ |
||
26 | private $router; |
||
27 | |||
28 | /** |
||
29 | * @var LoggerInterface |
||
30 | */ |
||
31 | private $logger; |
||
32 | |||
33 | /** |
||
34 | * @var array|null |
||
35 | */ |
||
36 | private $nodeTranslationMap = null; |
||
37 | |||
38 | /** |
||
39 | * @var array|null |
||
40 | */ |
||
41 | private $mediaMap = null; |
||
42 | |||
43 | /** |
||
44 | * @var DomainConfigurationInterface |
||
45 | */ |
||
46 | private $domainConfiguration; |
||
47 | |||
48 | /** |
||
49 | * @param EntityManager $em |
||
50 | * @param RouterInterface $router |
||
51 | * @param LoggerInterface $logger |
||
52 | * @param DomainConfigurationInterface $domainConfiguration |
||
53 | */ |
||
54 | public function __construct(EntityManager $em, RouterInterface $router, LoggerInterface $logger, DomainConfigurationInterface $domainConfiguration) |
||
0 ignored issues
–
show
|
|||
55 | { |
||
56 | $this->em = $em; |
||
57 | $this->router = $router; |
||
58 | $this->logger = $logger; |
||
59 | $this->domainConfiguration = $domainConfiguration; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Replace a given text, according to the node translation id and the multidomain site id. |
||
64 | * |
||
65 | * @param $text |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | public function replaceUrl($text) |
||
70 | { |
||
71 | if ($this->isEmailAddress($text)) { |
||
72 | $text = sprintf('%s:%s', 'mailto', $text); |
||
73 | } |
||
74 | |||
75 | if ($this->isInternalLink($text)) { |
||
76 | preg_match_all("/\[(([a-z_A-Z\.]+):)?NT([0-9]+)\]/", $text, $matches, PREG_SET_ORDER); |
||
77 | |||
78 | if (count($matches) > 0) { |
||
79 | $map = $this->getNodeTranslationMap(); |
||
80 | foreach ($matches as $match) { |
||
0 ignored issues
–
show
The expression
$matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
81 | $nodeTranslationFound = false; |
||
82 | $fullTag = $match[0]; |
||
83 | $hostId = $match[2]; |
||
84 | $hostConfig = $this->domainConfiguration->getFullHostById($hostId); |
||
85 | $hostBaseUrl = $this->domainConfiguration->getHostBaseUrl($hostConfig['host']); |
||
86 | |||
87 | $nodeTranslationId = $match[3]; |
||
88 | |||
89 | foreach ($map as $nodeTranslation) { |
||
0 ignored issues
–
show
The expression
$map of type array|null is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
90 | if ($nodeTranslation['id'] == $nodeTranslationId) { |
||
91 | $urlParams = ['url' => $nodeTranslation['url']]; |
||
92 | $nodeTranslationFound = true; |
||
93 | // Only add locale if multilingual site |
||
94 | if ($this->domainConfiguration->isMultiLanguage($hostConfig['host'])) { |
||
95 | $urlParams['_locale'] = $nodeTranslation['lang']; |
||
96 | } |
||
97 | |||
98 | // Only add other site, when having this. |
||
99 | if ($hostId) { |
||
100 | $urlParams['otherSite'] = $hostId; |
||
101 | } |
||
102 | |||
103 | $url = $this->router->generate('_slug', $urlParams); |
||
104 | |||
105 | $text = str_replace($fullTag, $hostId ? $hostBaseUrl . $url : $url, $text); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (!$nodeTranslationFound) { |
||
110 | $this->logger->error('No NodeTranslation found in the database when replacing url tag ' . $fullTag); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if ($this->isInternalMediaLink($text)) { |
||
117 | preg_match_all("/\[(([a-z_A-Z]+):)?M([0-9]+)\]/", $text, $matches, PREG_SET_ORDER); |
||
118 | |||
119 | if (count($matches) > 0) { |
||
120 | $map = $this->getMediaMap(); |
||
121 | foreach ($matches as $match) { |
||
0 ignored issues
–
show
The expression
$matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
122 | $mediaFound = false; |
||
123 | $fullTag = $match[0]; |
||
124 | $mediaId = $match[3]; |
||
125 | |||
126 | foreach ($map as $mediaItem) { |
||
0 ignored issues
–
show
The expression
$map of type array|null is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
127 | if ($mediaItem['id'] == $mediaId) { |
||
128 | $mediaFound = true; |
||
129 | $text = str_replace($fullTag, $mediaItem['url'], $text); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | if (!$mediaFound) { |
||
134 | $this->logger->error('No Media found in the database when replacing url tag ' . $fullTag); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | return $text; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get a map of all node translations. Only called once for caching. |
||
145 | * |
||
146 | * @return array|null |
||
147 | * |
||
148 | * @throws \Doctrine\DBAL\DBALException |
||
149 | */ |
||
150 | View Code Duplication | private function getNodeTranslationMap() |
|
151 | { |
||
152 | if (is_null($this->nodeTranslationMap)) { |
||
153 | $sql = 'SELECT id, url, lang FROM kuma_node_translations'; |
||
154 | $stmt = $this->em->getConnection()->prepare($sql); |
||
155 | $stmt->execute(); |
||
156 | $this->nodeTranslationMap = $stmt->fetchAll(); |
||
157 | } |
||
158 | |||
159 | return $this->nodeTranslationMap; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get a map of all media items. Only called once for caching. |
||
164 | * |
||
165 | * @return array|null |
||
166 | * |
||
167 | * @throws \Doctrine\DBAL\DBALException |
||
168 | */ |
||
169 | View Code Duplication | private function getMediaMap() |
|
170 | { |
||
171 | if (is_null($this->mediaMap)) { |
||
172 | $sql = 'SELECT id, url FROM kuma_media'; |
||
173 | $stmt = $this->em->getConnection()->prepare($sql); |
||
174 | $stmt->execute(); |
||
175 | $this->mediaMap = $stmt->fetchAll(); |
||
176 | } |
||
177 | |||
178 | return $this->mediaMap; |
||
179 | } |
||
180 | } |
||
181 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.