FormErrorsFlashHelper::addFlashErrors()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Controller\Helper;
12
13
use Symfony\Component\Form\FormError;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
16
use Symfony\Contracts\Translation\TranslatorInterface;
17
18
final class FormErrorsFlashHelper implements FormErrorsFlashHelperInterface
19
{
20
    /** @var FlashBagInterface */
21
    private $flashBag;
22
23
    /** @var TranslatorInterface */
24
    private $translator;
25
26
    public function __construct(FlashBagInterface $flashBag, TranslatorInterface $translator)
27
    {
28
        $this->flashBag = $flashBag;
29
        $this->translator = $translator;
30
    }
31
32
    public function addFlashErrors(FormInterface $form): void
33
    {
34
        if ($form->isValid()) {
35
            return;
36
        }
37
38
        $errors = [];
39
        /** @var FormError $error */
40
        foreach ($form->getErrors(true) as $error) {
41
            $errors[] = $error->getMessage();
42
        }
43
44
        $message = $this->translator->trans('bitbag_sylius_cms_plugin.ui.form_was_submitted_with_errors') . ' ' . rtrim(implode(' ', $errors));
45
46
        $this->flashBag->set('error', $message);
47
    }
48
}
49