FormService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 68
rs 10
c 2
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setModel() 0 3 1
A __construct() 0 8 1
A addMessages() 0 10 1
A formHtml() 0 15 1
1
<?php
2
3
namespace Jidaikobo\Kontiki\Services;
4
5
use Slim\Views\PhpRenderer;
6
use Jidaikobo\Kontiki\Models\ModelInterface;
7
use Jidaikobo\Kontiki\Renderers\FormRenderer;
8
use Jidaikobo\Kontiki\Handlers\FormHandler;
9
10
/**
11
 * FormService
12
 *
13
 * A service class to handle form rendering and processing.
14
 */
15
class FormService
16
{
17
    private PhpRenderer $view;
18
    private FormRenderer $formRenderer;
19
    private FormHandler $formHandler;
20
    private ?ModelInterface $model = null;
21
22
    public function __construct(
23
        FormRenderer $formRenderer,
24
        FormHandler $formHandler,
25
        PhpRenderer $view
26
    ) {
27
        $this->formRenderer = $formRenderer;
28
        $this->formHandler = $formHandler;
29
        $this->view = $view;
30
    }
31
32
    public function setModel(ModelInterface $model): void
33
    {
34
        $this->model = $model;
35
    }
36
37
    /**
38
     * Generate form HTML without additional processing.
39
     *
40
     * @param string $action    The form action URL.
41
     * @param array  $fields    The form fields definitions.
42
     * @param string $csrfToken CSRF Token.
43
     * @param array  $formVars  variables.
44
     *
45
     * @return string The generated HTML for the form.
46
     */
47
    public function formHtml(
48
        string $action,
49
        array $fields,
50
        string $csrfToken,
51
        array $formVars
52
    ): string {
53
        $this->formRenderer->setFields($fields);
54
        $this->view->addAttribute('formVars', $formVars);
55
56
        return $this->view->fetch(
57
            'forms/edit.php',
58
            [
59
                'actionAttribute' => env('BASEPATH', '') . $action,
60
                'csrfToken' => $csrfToken,
61
                'formHtml' => $this->formRenderer->render()
62
            ]
63
        );
64
    }
65
66
    /**
67
     * Process form HTML by adding errors and success messages.
68
     *
69
     * @param string $formHtml The raw form HTML to process.
70
     *
71
     * @return string The processed HTML with errors and success messages.
72
     */
73
    public function addMessages(
74
        string $formHtml,
75
        array $errors,
76
        array $success = array()
77
    ): string {
78
        $this->formHandler->setHtml($formHtml);
79
        $this->formHandler->setModel($this->model);
0 ignored issues
show
Bug introduced by
It seems like $this->model can also be of type null; however, parameter $model of Jidaikobo\Kontiki\Handlers\FormHandler::setModel() does only seem to accept Jidaikobo\Kontiki\Models\ModelInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $this->formHandler->setModel(/** @scrutinizer ignore-type */ $this->model);
Loading history...
80
        $this->formHandler->addErrors($errors);
81
        $this->formHandler->addSuccessMessages($success);
82
        return $this->formHandler->getHtml();
83
    }
84
}
85