Completed
Push — master ( 5e1d37...9b264a )
by Beñat
04:46
created

FormWrapper::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[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 LIN3S\WPSymfonyForm\Wrapper;
13
14
use LIN3S\WPSymfonyForm\Action\Action;
15
16
/**
17
 * FormWrapper class.
18
 *
19
 * @author Gorka Laucirica <[email protected]>
20
 */
21
class FormWrapper
22
{
23
    /**
24
     * The fully qualified class name of form.
25
     *
26
     * @var string
27
     */
28
    protected $formClass;
29
30
    /**
31
     * Array of actions.
32
     *
33
     * @var array
34
     */
35
    protected $successActions;
36
37
    /**
38
     * Generates a wrapper for a given form.
39
     *
40
     * @param string   $formClass      Fqcn of form
41
     * @param Action[] $successActions Array of actions
42
     */
43
    public function __construct($formClass, $successActions = [])
44
    {
45
        $this->formClass = $formClass;
46
        foreach ($successActions as $action) {
47
            if (!$action instanceof Action) {
48
                throw new \InvalidArgumentException(
49
                    'All actions passed to the FormWrapper must be an instance of ActionInterface'
50
                );
51
            }
52
        }
53
        $this->successActions = $successActions;
54
    }
55
56
    /**
57
     * Returns a new instance of the Form.
58
     *
59
     * @return \Symfony\Component\Form\Form
60
     */
61
    public function getForm()
62
    {
63
        return new $this->formClass();
64
    }
65
66
    /**
67
     * Returns registered actions to be called in form success.
68
     *
69
     * @return Action[]
70
     */
71
    public function getSuccessActions()
72
    {
73
        return $this->successActions;
74
    }
75
}
76