Test Failed
Pull Request — master (#490)
by
unknown
03:38
created

ContactForm   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
c 2
b 0
f 0
dl 0
loc 57
rs 10
ccs 9
cts 9
cp 1
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeLabels() 0 7 1
A getRules() 0 8 1
A getFormName() 0 3 1
B handleRequest() 0 24 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Contact;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Yiisoft\Form\FormModel;
9
use Yiisoft\Validator\Rule\Email;
10
use Yiisoft\Validator\Rule\Required;
11
12
final class ContactForm extends FormModel
13
{
14
    private string $name = '';
0 ignored issues
show
introduced by
The private property $name is not used, and could be removed.
Loading history...
15
    private string $email = '';
0 ignored issues
show
introduced by
The private property $email is not used, and could be removed.
Loading history...
16
    private string $subject = '';
0 ignored issues
show
introduced by
The private property $subject is not used, and could be removed.
Loading history...
17
    private string $body = '';
0 ignored issues
show
introduced by
The private property $body is not used, and could be removed.
Loading history...
18
    private ?array $attachFiles = null;
0 ignored issues
show
introduced by
The private property $attachFiles is not used, and could be removed.
Loading history...
19 6
20
    public function getAttributeLabels(): array
21
    {
22 6
        return [
23
            'name' => 'Name',
24
            'email' => 'Email',
25
            'subject' => 'Subject',
26
            'body' => 'Body',
27
        ];
28
    }
29 6
30
    public function handleRequest(ServerRequestInterface $request, ?string $formName = null)
31 6
    {
32
        $body = $request->getParsedBody();
33
        $files = $request->getUploadedFiles();
34 6
        if ($this->load($body, $formName)) {
35
36
            $rawFiles = [];
37 6
            $scope = $formName ?? $this->getFormName();
38 6
39 6
            if ($scope === '' && !empty($data)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to never exist and therefore empty should always be true.
Loading history...
40 6
                $rawFiles = $data;
41
            } elseif (isset($files[$scope])) {
42
                if (!is_array($files[$scope])) {
43
                    return false;
44
                }
45
                $rawFiles = $files[$scope];
46
            }
47
48
            foreach ($rawFiles as $name => $value) {
49
                $this->setAttribute((string) $name, $value);
50
            }
51
            return true;
52
        }
53
        return false;
54
    }
55
56
    public function getFormName(): string
57
    {
58
        return 'ContactForm';
59
    }
60
61
    public function getRules(): array
62
    {
63
        return [
64
            'name' => [new Required()],
65
            'email' => [new Required(), new Email()],
66
            'subject' => [new Required()],
67
            'body' => [new Required()],
68
            'attachFiles' => [new File()],
69
        ];
70
    }
71
}
72