Passed
Push — master ( 178a27...59b625 )
by Peter
02:19
created

Contact::addFromPhone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Form\Factory;
6
7
use AbterPhp\Admin\Form\Factory\Base;
8
use AbterPhp\Framework\Constant\Html5;
9
use AbterPhp\Framework\Form\Container\FormGroup;
10
use AbterPhp\Framework\Form\Element\Input;
11
use AbterPhp\Framework\Form\Element\Textarea;
12
use AbterPhp\Framework\Form\Form;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Contact\Form\Factory\Form. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use AbterPhp\Framework\Form\IForm;
14
use AbterPhp\Framework\Form\Label\Label;
15
use AbterPhp\Framework\Html\Component\Button;
16
use Opulence\Framework\Http\CsrfTokenChecker;
17
use Opulence\Orm\IEntity;
18
19
class Contact extends Base
20
{
21
    /**
22
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
23
     *
24
     * @param string       $action
25
     * @param string       $method
26
     * @param string       $showUrl
27
     * @param IEntity|null $entity
28
     *
29
     * @return Form
30
     */
31
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
32
    {
33
        $this->createForm($action, $method)
34
            ->addDefaultElements()
35
            ->addFromName()
36
            ->addFromEmail()
37
            ->addSubject()
38
            ->addBody()
39
            ->addSubmit();
40
41
        $form = $this->form;
42
43
        $this->form = null;
44
45
        return $form;
46
    }
47
48
    /**
49
     * @return $this
50
     */
51
    protected function addDefaultElements(): Base
52
    {
53
        $name  = CsrfTokenChecker::TOKEN_INPUT_NAME;
54
        $value = (string)$this->session->get($name);
55
56
        $attributes = [Html5::ATTR_TYPE => Input::TYPE_HIDDEN];
57
58
        $this->form[] = new Input($name, $name, $value, [], $attributes);
59
60
        return $this;
61
    }
62
63
    /**
64
     * @return $this
65
     */
66
    protected function addFromName(): Contact
67
    {
68
        $input = new Input('from_name', 'from_name', '');
69
        $label = new Label('from_name', 'contact:fromName');
70
71
        $this->form[] = new FormGroup($input, $label, null);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return $this
78
     */
79
    protected function addFromEmail(): Contact
80
    {
81
        $input = new Input('from_email', 'from_email', '', [], [Html5::ATTR_TYPE => Input::TYPE_EMAIL]);
82
        $label = new Label('from_email', 'contact:fromEmail');
83
84
        $this->form[] = new FormGroup($input, $label);
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return $this
91
     */
92
    protected function addSubject(): Contact
93
    {
94
        $input = new Input(
95
            'subject',
96
            'subject',
97
            ''
98
        );
99
        $label = new Label('subject', 'contact:subject');
100
101
        $this->form[] = new FormGroup($input, $label);
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return $this
108
     */
109
    protected function addBody(): Contact
110
    {
111
        $input = new Textarea('body', 'body', '', [], [Html5::ATTR_ROWS => '15']);
112
        $label = new Label('body', 'contact:body');
113
114
        $this->form[] = new FormGroup($input, $label);
115
116
        return $this;
117
    }
118
119
    /**
120
     * @return $this
121
     */
122
    protected function addSubmit(): Contact
123
    {
124
        $content = $this->translator->translate('contact:submit');
125
126
        $this->form[] = new Button($content, [Button::INTENT_PRIMARY]);
127
128
        return $this;
129
    }
130
}
131