Passed
Pull Request — master (#91)
by Alexander
12:28
created

ContactController::contact()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 39
rs 8.6026
nc 11
nop 1
cc 7
1
<?php
2
3
namespace App\Contact;
4
5
use App\Controller;
6
use App\Parameters;
0 ignored issues
show
Bug introduced by
The type App\Parameters was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Log\LoggerInterface;
10
use Yiisoft\Aliases\Aliases;
11
use Yiisoft\Http\Method;
12
use Yiisoft\View\WebView;
13
use Yiisoft\Yii\Web\User\User;
14
use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface;
15
16
class ContactController extends Controller
17
{
18
    private ContactMailer $mailer;
19
    private LoggerInterface $logger;
20
    private Parameters $parameters;
21
22
    public function __construct(
23
        DataResponseFactoryInterface $responseFactory,
24
        Aliases $aliases,
25
        WebView $view,
26
        User $user,
27
        ContactMailer $mailer,
28
        LoggerInterface $logger,
29
        Parameters $parameters
30
    ) {
31
        $this->mailer = $mailer;
32
        $this->logger = $logger;
33
        parent::__construct($responseFactory, $user, $aliases, $view);
34
        $this->parameters = $parameters;
35
    }
36
37
    protected function getId(): string
38
    {
39
        return 'contact';
40
    }
41
42
    public function contact(ServerRequestInterface $request): ResponseInterface
43
    {
44
        $body = $request->getParsedBody();
45
        $parameters = [
46
            'body' => $body,
47
        ];
48
        if ($request->getMethod() === Method::POST) {
49
            $sent = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $sent is dead and can be removed.
Loading history...
50
            $error = '';
51
52
            try {
53
                foreach (['subject', 'name', 'email', 'content'] as $name) {
54
                    if (empty($body[$name])) {
55
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required');
56
                    }
57
                }
58
59
                $message = new Message($body['name'], $body['email'], $body['subject'], $body['content']);
60
61
                $files = $request->getUploadedFiles();
62
                if (!empty($files['file']) && $files['file']->getError() === UPLOAD_ERR_OK) {
63
                    $message->addFile($files['file']);
64
                }
65
66
                $this->mailer->send($message);
67
68
                $sent = true;
69
            } catch (\Throwable $e) {
70
                throw $e;
71
                $this->logger->error($e);
0 ignored issues
show
Unused Code introduced by
$this->logger->error($e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
72
                $error = $e->getMessage();
73
            }
74
            $parameters['sent'] = $sent;
75
            $parameters['error'] = $error;
76
        }
77
78
        $parameters['csrf'] = $request->getAttribute('csrf_token');
79
80
        return $this->render('form', $parameters);
81
    }
82
}
83