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

ContactForm::getAttributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
ccs 1
cts 1
cp 1
crap 1
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