Completed
Push — feature/admin-cp ( 7edd29...e0b794 )
by Vladimir
05:17
created

ConfirmationFormCreator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B create() 0 29 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
     */
40
    private $destructive;
41
42
    /**
43
     * Create a new confirmation form
44
     * @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
    public function __construct($action, $originalUrl, $no = false, $destructive = false)
50
    {
51
        $this->action = $action;
52
        $this->originalUrl = $originalUrl;
53
        $this->no = $no;
54
        $this->destructive = $destructive;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function create()
61
    {
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