Completed
Push — master ( e327bd...2b2b50 )
by Nikolas
03:41
created

ActionForm::readParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 3
eloc 8
nc 3
nop 3
1
<?php
2
namespace rtens\domin\delivery\web;
3
4
use rtens\domin\Action;
5
use rtens\domin\Parameter;
6
use watoki\curir\delivery\WebRequest;
7
use watoki\reflect\type\ClassType;
8
9
class ActionForm {
10
11
    /** @var array */
12
    private $model;
13
14
    /** @var array|Element[] */
15
    private $headElements = [];
16
17
    public function __construct(WebRequest $request, WebApplication $app, Action $action, $actionId) {
18
        $actionParameter = new Parameter($actionId, new ClassType(get_class($action)));
19
        $actionField = $this->getActionField($actionParameter, $app);
20
21
        $this->model = $actionField->render($actionParameter, $this->readParameters($request, $app, $action));
0 ignored issues
show
Documentation Bug introduced by
It seems like $actionField->render($ac...equest, $app, $action)) of type string is incompatible with the declared type array of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
22
        $this->headElements = $actionField->headElements($actionParameter);
23
    }
24
25
    public function getModel() {
26
        return $this->model;
27
    }
28
29
    public function getHeadElements() {
30
        return $this->headElements;
31
    }
32
33
    /**
34
     * @param Parameter $actionParameter
35
     * @param WebApplication $app
36
     * @return WebField
37
     * @throws \Exception
38
     */
39 View Code Duplication
    private function getActionField(Parameter $actionParameter, WebApplication $app) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
        $actionField = $app->fields->getField($actionParameter);
41
        if ($actionField instanceof WebField) {
42
            return $actionField;
43
        }
44
        throw new \Exception(get_class($actionField) . " must implement WebField");
45
    }
46
47
    private function readParameters(WebRequest $request, WebApplication $app, Action $action) {
48
        $reader = new RequestParameterReader($request);
49
        $values = [];
50
51
        foreach ($action->parameters() as $parameter) {
52
            if ($reader->has($parameter)) {
53
                $field = $app->fields->getField($parameter);
54
                $values[$parameter->getName()] = $field->inflate($parameter, $reader->read($parameter));
55
            }
56
        }
57
        return $values;
58
    }
59
}