ongr-io /
TranslationsBundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the ONGR package. |
||
| 5 | * |
||
| 6 | * (c) NFQ Technologies UAB <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace ONGR\TranslationsBundle\Service; |
||
| 13 | |||
| 14 | use ONGR\ElasticsearchBundle\Result\DocumentIterator; |
||
| 15 | use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; |
||
| 16 | use ONGR\ElasticsearchDSL\Query\MatchAllQuery; |
||
| 17 | use ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery; |
||
| 18 | use ONGR\ElasticsearchBundle\Service\Repository; |
||
| 19 | use ONGR\TranslationsBundle\Document\Message; |
||
| 20 | use ONGR\TranslationsBundle\Document\Translation; |
||
| 21 | use ONGR\TranslationsBundle\Event\Events; |
||
| 22 | use ONGR\TranslationsBundle\Event\MessageUpdateEvent; |
||
| 23 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
| 24 | use Symfony\Component\HttpFoundation\Request; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Handles translation objects by http requests. |
||
| 28 | */ |
||
| 29 | class TranslationManager |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var Repository |
||
| 33 | */ |
||
| 34 | private $repository; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var HistoryManager |
||
| 38 | */ |
||
| 39 | private $historyManager; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var EventDispatcherInterface |
||
| 43 | */ |
||
| 44 | private $dispatcher; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param Repository $repository Translation repository service. |
||
| 48 | * @param HistoryManager $manager History manager service. |
||
| 49 | * @param EventDispatcherInterface $dispatcher |
||
| 50 | */ |
||
| 51 | public function __construct(Repository $repository, HistoryManager $manager, EventDispatcherInterface $dispatcher) |
||
| 52 | { |
||
| 53 | $this->repository = $repository; |
||
| 54 | $this->historyManager = $manager; |
||
| 55 | $this->dispatcher = $dispatcher; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Edits object from translation. |
||
| 60 | * |
||
| 61 | * @param string $id |
||
| 62 | * @param Request $request Http request object. |
||
| 63 | */ |
||
| 64 | public function edit($id, Request $request) |
||
| 65 | { |
||
| 66 | $content = json_decode($request->getContent(), true); |
||
| 67 | |||
| 68 | if (empty($content)) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | $document = $this->get($id); |
||
| 73 | |||
| 74 | if (isset($content['messages'])) { |
||
| 75 | $this->updateMessages($document, $content['messages']); |
||
|
0 ignored issues
–
show
|
|||
| 76 | unset($content['messages']); |
||
| 77 | } |
||
| 78 | |||
| 79 | try { |
||
| 80 | foreach ($content as $key => $value) { |
||
| 81 | $document->{'set'.ucfirst($key)}($value); |
||
| 82 | } |
||
| 83 | |||
| 84 | $document->setUpdatedAt(new \DateTime()); |
||
| 85 | } catch (\Exception $e) { |
||
| 86 | throw new \LogicException('Illegal variable provided for translation'); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->repository->getManager()->persist($document); |
||
|
0 ignored issues
–
show
It seems like
$document defined by $this->get($id) on line 72 can also be of type null; however, ONGR\ElasticsearchBundle...vice\Manager::persist() does only seem to accept object, maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. Loading history...
|
|||
| 90 | $this->repository->getManager()->commit(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns all active tags from translations |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function getTags() |
||
| 98 | { |
||
| 99 | return $this->getGroupTypeInfo('tags'); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns all active domains from translations |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function getDomains() |
||
| 107 | { |
||
| 108 | return $this->getGroupTypeInfo('domain'); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $id |
||
| 113 | * |
||
| 114 | * @return Translation|object |
||
| 115 | */ |
||
| 116 | public function get($id) |
||
| 117 | { |
||
| 118 | return $this->repository->find($id); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns all translations if filters are not specified |
||
| 123 | * |
||
| 124 | * @param array $filters An array with specified limitations for results |
||
| 125 | * |
||
| 126 | * @return DocumentIterator |
||
| 127 | */ |
||
| 128 | public function getAll(array $filters = null) |
||
| 129 | { |
||
| 130 | $search = $this->repository->createSearch(); |
||
| 131 | $search->addQuery(new MatchAllQuery()); |
||
| 132 | $search->setScroll('2m'); |
||
| 133 | |||
| 134 | if ($filters) { |
||
| 135 | foreach ($filters as $field => $value) { |
||
| 136 | $search->addQuery(new TermsQuery($field, $value)); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return $this->repository->findDocuments($search); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param Translation[] $translations |
||
| 145 | */ |
||
| 146 | public function save($translations) |
||
| 147 | { |
||
| 148 | foreach ($translations as $translation) { |
||
| 149 | $this->repository->getManager()->persist($translation); |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->repository->getManager()->commit(); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Translation $document |
||
| 157 | * @param array $messages |
||
| 158 | */ |
||
| 159 | private function updateMessages(Translation $document, array $messages) |
||
| 160 | { |
||
| 161 | $setMessagesLocales = array_keys($document->getMessagesArray()); |
||
| 162 | $documentMessages = $document->getMessages(); |
||
| 163 | |||
| 164 | foreach ($messages as $locale => $messageText) { |
||
| 165 | if (!empty($messageText) && is_string($messageText)) { |
||
| 166 | if (in_array($locale, $setMessagesLocales)) { |
||
| 167 | foreach ($documentMessages as $message) { |
||
| 168 | if ($message->getLocale() == $locale && $message->getMessage() != $messageText) { |
||
| 169 | $this->historyManager->addHistory($message, $document); |
||
| 170 | $this->updateMessageData($message, $locale, $messages[$locale], new \DateTime()); |
||
| 171 | break; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } else { |
||
| 175 | $documentMessages[] = $this->updateMessageData(new Message(), $locale, $messageText); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | $document->setMessages($documentMessages); |
||
|
0 ignored issues
–
show
$documentMessages is of type array<integer,object<ONG...ndle\Document\Message>>, but the function expects a null|object<ONGR\Elastic...\Collection\Collection>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param Message $message |
||
| 185 | * @param string $locale |
||
| 186 | * @param string $text |
||
| 187 | * @param \DateTime $updatedAt |
||
| 188 | * |
||
| 189 | * @return Message |
||
| 190 | */ |
||
| 191 | private function updateMessageData(Message $message, $locale, $text, $updatedAt = null) |
||
| 192 | { |
||
| 193 | $message->setLocale($locale); |
||
| 194 | $message->setStatus(Message::DIRTY); |
||
| 195 | $message->setMessage($text); |
||
| 196 | |||
| 197 | if ($updatedAt) { |
||
| 198 | $message->setUpdatedAt($updatedAt); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $message; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns a list of available tags or domains. |
||
| 206 | * |
||
| 207 | * @param string $type |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | private function getGroupTypeInfo($type) |
||
| 212 | { |
||
| 213 | $search = $this->repository->createSearch(); |
||
| 214 | $search->addAggregation(new TermsAggregation($type, $type)); |
||
| 215 | $result = $this->repository->findDocuments($search); |
||
| 216 | $aggregation = $result->getAggregation($type); |
||
| 217 | $items = []; |
||
| 218 | |||
| 219 | foreach ($aggregation as $item) { |
||
|
0 ignored issues
–
show
The expression
$aggregation of type null|object<ONGR\Elastic...ation\AggregationValue> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 220 | $items[] = $item['key']; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $items; |
||
| 224 | } |
||
| 225 | } |
||
| 226 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: