Completed
Push — master ( 699d35...4f061e )
by Mikołaj
01:56
created

FormErrorsFlashHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag. 
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project. 
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected]. 
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Controller\Helper;
14
15
use Symfony\Component\Form\FormInterface;
16
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
final class FormErrorsFlashHelper implements FormErrorsFlashHelperInterface
20
{
21
    /** @var FlashBagInterface */
22
    private $flashBag;
23
24
    /** @var TranslatorInterface */
25
    private $translator;
26
27
    public function __construct(FlashBagInterface $flashBag, TranslatorInterface $translator)
28
    {
29
        $this->flashBag = $flashBag;
30
        $this->translator = $translator;
31
    }
32
33
    public function addFlashErrors(FormInterface $form): void
34
    {
35
        if ($form->isValid()) {
36
            return;
37
        }
38
39
        $errors = [];
40
41
        foreach ($form->getErrors(true) as $error) {
42
            $errors[] = $error->getMessage();
43
        }
44
45
        $message = $this->translator->trans('bitbag_sylius_cms_plugin.ui.form_was_submitted_with_errors') . ' ' . rtrim(implode($errors, " "));
46
47
        $this->flashBag->set('error', $message);
48
    }
49
}
50