Passed
Push — master ( bf42cc...349842 )
by Peter
05:57
created

Form   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 1
b 0
f 0
dl 0
loc 103
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addIdentifier() 0 11 1
A addToName() 0 8 1
A addToEmail() 0 8 1
A create() 0 19 2
A __construct() 0 3 1
A addName() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Form\Factory;
6
7
use AbterPhp\Contact\Domain\Entities\Form as Entity;
8
use AbterPhp\Framework\Constant\Html5;
9
use AbterPhp\Framework\Form\Component\Option;
10
use AbterPhp\Framework\Form\Container\FormGroup;
11
use AbterPhp\Framework\Form\Element\Input;
12
use AbterPhp\Framework\Form\Element\MultiSelect;
13
use AbterPhp\Framework\Form\Element\Select;
14
use AbterPhp\Framework\Form\Factory\Base;
15
use AbterPhp\Framework\Form\Factory\IFormFactory;
16
use AbterPhp\Framework\Form\IForm;
17
use AbterPhp\Framework\Form\Label\Label;
18
use AbterPhp\Framework\I18n\ITranslator;
19
use Opulence\Orm\IEntity;
20
use Opulence\Sessions\ISession;
21
22
class Form extends Base
23
{
24
    /**
25
     * Form constructor.
26
     *
27
     * @param ISession      $session
28
     * @param ITranslator   $translator
29
     */
30
    public function __construct(ISession $session, ITranslator $translator)
31
    {
32
        parent::__construct($session, $translator);
33
    }
34
35
    /**
36
     * @param string       $action
37
     * @param string       $method
38
     * @param string       $showUrl
39
     * @param IEntity|null $entity
40
     *
41
     * @return IForm
42
     */
43
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
44
    {
45
        if (!($entity instanceof Entity)) {
46
            throw new \InvalidArgumentException(IFormFactory::ERR_MSG_ENTITY_MISSING);
47
        }
48
49
        $this->createForm($action, $method)
50
            ->addDefaultElements()
51
            ->addName($entity)
52
            ->addIdentifier($entity)
53
            ->addToName($entity)
54
            ->addToEmail($entity)
55
            ->addDefaultButtons($showUrl);
56
57
        $form = $this->form;
58
59
        $this->form = null;
60
61
        return $form;
62
    }
63
64
    /**
65
     * @param Entity $entity
66
     *
67
     * @return $this
68
     */
69
    protected function addName(Entity $entity): Form
70
    {
71
        $input = new Input('name', 'name', $entity->getName());
72
        $label = new Label('name', 'contact:formName');
73
74
        $this->form[] = new FormGroup($input, $label);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param Entity $entity
81
     *
82
     * @return $this
83
     */
84
    protected function addIdentifier(Entity $entity): Form
85
    {
86
        $this->form[] = new Input(
87
            'identifier',
88
            'identifier',
89
            $entity->getIdentifier(),
90
            [],
91
            [Html5::ATTR_TYPE => Input::TYPE_HIDDEN]
92
        );
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param Entity $entity
99
     *
100
     * @return $this
101
     */
102
    protected function addToName(Entity $entity): Form
103
    {
104
        $input = new Input('to_name', 'to_name', $entity->getToName());
105
        $label = new Label('to_name', 'contact:formToName');
106
107
        $this->form[] = new FormGroup($input, $label);
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param Entity $entity
114
     *
115
     * @return $this
116
     */
117
    protected function addToEmail(Entity $entity): Form
118
    {
119
        $input = new Input('to_email', 'to_email', $entity->getToEmail());
120
        $label = new Label('to_email', 'contact:formToEmail');
121
122
        $this->form[] = new FormGroup($input, $label);
123
124
        return $this;
125
    }
126
}
127