Passed
Push — master ( d15523...87e584 )
by Andreas
09:33
created

datamanager::get_name()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager;
7
8
use Symfony\Component\Form\FormFactoryInterface;
9
use Symfony\Component\Form\Form;
10
use midcom_core_dbaobject;
11
use midcom_core_context;
12
use midcom;
13
use midcom\datamanager\extension\transformer\multipleTransformer;
14
use midcom\datamanager\storage\recreateable;
15
use midcom\datamanager\extension\type\schemaType;
16
use midcom\datamanager\extension\type\toolbarType;
17
use midcom\datamanager\storage\container\container;
18
use midcom\datamanager\storage\container\dbacontainer;
19
use Symfony\Component\Form\FormBuilderInterface;
20
21
/**
22
 * Experimental datamanager class
23
 */
24
class datamanager
25
{
26
    private schemadb $schemadb;
27
28
    private ?schema $schema = null;
29
30
    private ?container $storage = null;
31
32
    private array $defaults = [];
33
34
    private ?renderer $renderer = null;
35
36
    private ?Form $form = null;
37
38 175
    public function __construct(schemadb $schemadb)
39
    {
40 175
        $this->schemadb = $schemadb;
41
    }
42
43 152
    private static function get_factory() : FormFactoryInterface
44
    {
45 152
        return midcom::get()->getContainer()->get('form.factory');
46
    }
47
48 81
    public static function from_schemadb(string $path) : self
49
    {
50 81
        return new static(schemadb::from_path($path));
51
    }
52
53 44
    public function set_defaults(array $defaults) : self
54
    {
55 44
        $this->defaults = $defaults;
56 44
        return $this;
57
    }
58
59 170
    public function set_storage(midcom_core_dbaobject $storage = null, string $schemaname = null) : self
60
    {
61 170
        if (   $schemaname === null
62 170
            && !empty($storage->id)) {
63 81
            $schemaname = $storage->get_parameter('midcom.helper.datamanager2', 'schema_name');
0 ignored issues
show
Bug introduced by
The method get_parameter() does not exist on null. ( Ignorable by Annotation )

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

63
            /** @scrutinizer ignore-call */ 
64
            $schemaname = $storage->get_parameter('midcom.helper.datamanager2', 'schema_name');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
        }
65
66 170
        $this->set_schema($schemaname);
67
68 170
        $defaults = array_merge($this->schema->get_defaults(), $this->defaults);
69 170
        if ($storage === null) {
70 13
            $this->storage = new storage\container\nullcontainer($this->schema, $defaults);
71
        } else {
72 157
            $this->storage = new storage\container\dbacontainer($this->schema, $storage, $defaults);
73
        }
74
75 170
        if ($this->form !== null) {
76 16
            if ($this->form->isSubmitted()) {
77 13
                $this->form = null;
78
            } else {
79 3
                $this->form->setData($this->storage);
80
            }
81
        }
82
83 170
        return $this;
84
    }
85
86 170
    private function set_schema(?string $name)
87
    {
88 170
        if ($name && !$this->schemadb->has($name)) {
89
            debug_add("Given schema name {$name} was not found, reverting to default.", MIDCOM_LOG_INFO);
90
            $name = null;
91
        }
92
93 170
        $schema = ($name) ? $this->schemadb->get($name) : $this->schemadb->get_first();
94 170
        if ($this->schema !== null && $this->schema->get_name() !== $schema->get_name()) {
95
            $this->form = null;
96
        }
97 170
        $this->schema = $schema;
98
    }
99
100 158
    public function get_schema(string $name = null) : schema
101
    {
102 158
        if ($name) {
103 1
            return $this->schemadb->get($name);
104
        }
105 158
        if ($this->schema === null) {
106 15
            $this->set_schema($name);
107
        }
108 158
        return $this->schema;
109
    }
110
111 156
    public function get_storage() : container
112
    {
113 156
        if (!$this->storage) {
114 12
            $this->set_storage(null);
115
        }
116 156
        return $this->storage;
117
    }
118
119 148
    public function get_renderer($template = null, bool $skip_empty = false) : renderer
120
    {
121 148
        if ($this->renderer === null) {
122 148
            $this->renderer = new renderer(new engine);
123 148
            $this->renderer->set_l10n($this->schema->get_l10n());
0 ignored issues
show
Bug introduced by
The method get_l10n() does not exist on null. ( Ignorable by Annotation )

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

123
            $this->renderer->set_l10n($this->schema->/** @scrutinizer ignore-call */ get_l10n());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method set_l10n() does not exist on null. ( Ignorable by Annotation )

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

123
            $this->renderer->/** @scrutinizer ignore-call */ 
124
                             set_l10n($this->schema->get_l10n());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
        }
125 148
        if ($template) {
126 148
            if (is_string($template)) {
127 148
                $config = \midcom_baseclasses_components_configuration::get('midcom.datamanager', 'config');
128 148
                $templates = $config->get_array('templates');
129 148
                if (!array_key_exists($template, $templates)) {
130
                    throw new \midcom_error('Template ' . $template . ' not found in config');
131
                }
132 148
                $template = new $templates[$template]($this->renderer, $skip_empty);
133
            }
134 148
            $view = $this->get_form()->createView();
135 148
            $this->renderer->set_template($view, $template);
136
        }
137 148
        return $this->renderer;
138
    }
139
140 106
    public function get_controller(string $name = null) : controller
141
    {
142 106
        return new controller($this, $name);
143
    }
144
145 152
    public function get_form(?string $name = null, bool $reset = false) : Form
146
    {
147 152
        if ($reset) {
148
            $this->form = null;
149
        }
150 152
        $name = $this->get_name($name);
151
152 152
        if (   $this->form === null
153 152
            || $this->form->getName() != $name) {
154 150
            $this->build_form($this->get_builder($name));
155
        }
156 152
        return $this->form;
157
    }
158
159 152
    public function get_builder(string $name = null) : FormBuilderInterface
160
    {
161 152
        $config = [
162 152
            'schema' => $this->get_schema()
163 152
        ];
164 152
        $builder = self::get_factory()->createNamedBuilder($this->get_name($name), schemaType::class, null, $config);
165 152
        $builder->add('form_toolbar', toolbarType::class, [
166 152
            'operations' => $this->schema->get('operations'),
167 152
            'index_method' => 'noindex'
168 152
        ]);
169 152
        return $builder;
170
    }
171
172 152
    public function build_form(FormBuilderInterface $builder) : self
173
    {
174 152
        $this->form = $builder->getForm()
175 152
            ->setData($this->get_storage());
176
177 152
        return $this;
178
    }
179
180 152
    private function get_name(?string $name) : string
181
    {
182 152
        if (!$name && $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT)) {
183
            // Replace the dots in the component name with underscores
184 145
            $name = midcom::get()->componentloader->path_to_prefix($name);
185
        }
186 152
        return $name ?: 'midcom_helper_datamanager2';
187
    }
188
189 10
    public function get_content_raw() : array
190
    {
191 10
        $ret = [];
192
193 10
        foreach ($this->storage as $field => $value)
194
        {
195 10
            $ret[$field] = $value->get_value();
196 10
            $config = $this->schema->get_field($field);
0 ignored issues
show
Bug introduced by
It seems like $field can also be of type null; however, parameter $name of midcom\datamanager\schema::get_field() does only seem to accept string, 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

196
            $config = $this->schema->get_field(/** @scrutinizer ignore-type */ $field);
Loading history...
197 10
            if (!empty($config['type_config']['allow_multiple'])) {
198 1
                $transformer = new multipleTransformer($config);
199 1
                $ret[$field] = $transformer->transform($ret[$field]);
200
            }
201
        }
202
203 10
        return $ret;
204
    }
205
206 4
    public function get_content_csv() : array
207
    {
208 4
        return $this->render('csv');
209
    }
210
211 40
    public function get_content_html() : array
212
    {
213 40
        return $this->render('view');
214
    }
215
216 46
    public function render(string $type) : array
217
    {
218 46
        $ret = [];
219
220 46
        $renderer = $this->get_renderer($type);
221 46
        foreach ($renderer->get_view()->children as $name => $value) {
222 46
            if ($name == 'form_toolbar') {
223 46
                continue;
224
            }
225 46
            $ret[$name] = $renderer->widget($value);
226
        }
227 46
        return $ret;
228
    }
229
230 13
    public function display_view(bool $skip_empty = false)
231
    {
232 13
        $renderer = $this->get_renderer('view', $skip_empty);
233 13
        echo $renderer->block($renderer->get_view(), 'form');
234
    }
235
236
    public function recreate() : bool
237
    {
238
        $ret = true;
239
        foreach ($this->storage as $field) {
240
            if (   $field instanceof recreateable
241
                && !$field->recreate()) {
242
                $ret = false;
243
            }
244
        }
245
        return $ret;
246
    }
247
}
248