Completed
Push — wip/steps ( b870e4...a9b84a )
by Romain
02:24
created

FormActionControllerTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A initializeFormz() 0 21 3
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Controller;
15
16
use Romm\Formz\Controller\Processor\ControllerProcessor;
17
use Romm\Formz\Middleware\Scope\MainScope;
18
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
19
20
/**
21
 * @mixin ActionController
22
 */
23
trait FormActionControllerTrait
24
{
25
    /**
26
     * In case an exception (any exception type) is thrown during the
27
     * middlewares execution, it can be automatically caught by FormZ, and the
28
     * request will be forwarded to an action of the controller.
29
     *
30
     * You can use it for instance to log your exception in some log service; to
31
     * render a view that contains a message explaining to the user that
32
     * something went wrong.
33
     *
34
     * Just fill the property below with the name of an existing action of the
35
     * controller. The method will have a single parameter which is the
36
     * exception.
37
     *
38
     * @var string
39
     */
40
    protected $actionForException;
41
42
    /**
43
     * @todo
44
     *
45
     * @var string
46
     */
47
    protected $formScope = MainScope::class;
48
49
    /**
50
     * IMPORTANT: if you need to override this method in your own controller, do
51
     * not forget to call `parent::initializeAction()`!
52
     */
53
    private function initializeFormz()
54
    {
55
        $settings = is_array($this->settings)
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
            ? $this->settings
57
            : [];
58
59
        $processor = ControllerProcessor::prepare($this->request, $this->arguments, $this->formScope, $settings);
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property arguments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
61
        if (null !== $this->actionForException) {
62
            $vendorName = $this->request->getControllerVendorName();
63
            $extensionName = $this->request->getControllerExtensionName();
64
            $controllerName = $this->request->getControllerName();
65
66
            $processor->setExceptionCallback(function ($exception) use ($vendorName, $controllerName, $extensionName) {
67
                $this->request->setControllerVendorName($vendorName);
68
                $this->forward($this->actionForException, $controllerName, $extensionName, ['exception' => $exception]);
0 ignored issues
show
Bug introduced by
It seems like forward() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
            });
70
        }
71
72
        $processor->dispatch();
73
    }
74
}
75