Completed
Push — master ( 3eac5a...b98009 )
by Vladimir
04:57
created

ConfirmationFormCreator::create()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 1
cts 1
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 2
nop 0
crap 4
1
<?php
2
/**
3
 * This file contains a form creator that generates confirmation forms
4
 *
5
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
6
 */
7
8
namespace BZIon\Form\Creator;
9
10
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
13
/**
14
 * Form creator for confirmation dialogs
15
 */
16
class ConfirmationFormCreator implements FormCreatorInterface
17
{
18
    /**
19
     * The primary action of the form (e.g. "Yes" or "Delete"), shown to the user
20
     * @var string
21
     */
22
    private $action;
23
24
    /**
25
     * The URL where the form should redirect on cancellation
26
     * @var string
27
     */
28
    private $originalUrl;
29
30
    /**
31
     * Whether to show "No" instead of cancel
32
     * @var bool
33
     */
34
    private $no;
35
36
    /**
37
     * Whether or not the confirmation is for a destructive action
38
     * @var bool
39 1
     */
40
    private $destructive;
41 1
42 1
    /**
43 1
     * Create a new confirmation form
44 1
     * @param string $action      The text to show on the "Yes" button
45
     * @param string $originalUrl The URL which the user is coming from
46
     * @param bool   $no          Whether to show "No" instead of cancel
47
     * @param bool   $destructive Whether or not to mark the action button as being a destructive operation
48
     */
49 1
    public function __construct($action, $originalUrl, $no = false, $destructive = false)
50
    {
51 1
        $this->action = $action;
52
        $this->originalUrl = $originalUrl;
53
        $this->no = $no;
54 1
        $this->destructive = $destructive;
55 1
    }
56
57 1
    /**
58 1
     * {@inheritdoc}
59 1
     */
60
    public function create()
61 1
    {
62
        $builder = \Service::getFormFactory()->createNamedBuilder('confirm_form');
63
        $cssClasses = ['mr2'];
64
65
        if ($this->destructive) {
66
            $cssClasses[] = 'c-button--red';
67
            $cssClasses[] = 'pattern--diamonds';
68
        } else {
69
            $cssClasses[] = 'c-button--blue';
70
            $cssClasses[] = 'pattern--upward-stripes';
71
        }
72
73
        return $builder
74
            ->add('confirm', SubmitType::class, [
75
                'label' => $this->action,
76
                'attr' => [
77
                    'class' => implode(' ', $cssClasses),
78
                ],
79
            ])
80
            ->add(($this->action == 'Yes' || $this->no) ? 'No' : 'Cancel', SubmitType::class, [
81
82
            ])
83
            ->add('original_url', HiddenType::class, [
84
                'data' => $this->originalUrl,
85
            ])
86
            ->getForm()
87
        ;
88
    }
89
}
90