Passed
Push — master ( 8e0254...5f7ecb )
by Andreas
09:32
created

datamanager::get_schema()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
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 Symfony\Component\Form\FormBuilderInterface;
19
20
/**
21
 * Experimental datamanager class
22
 */
23
class datamanager
24
{
25
    private schemadb $schemadb;
26
27
    private ?schema $schema = null;
28
29
    private ?container $storage = null;
30
31
    private array $defaults = [];
32
33
    private ?renderer $renderer = null;
34
35
    private ?Form $form = null;
36
37 175
    public function __construct(schemadb $schemadb)
38
    {
39 175
        $this->schemadb = $schemadb;
40
    }
41
42 152
    private static function get_factory() : FormFactoryInterface
43
    {
44 152
        return midcom::get()->getContainer()->get('form.factory');
45
    }
46
47 81
    public static function from_schemadb(string $path) : self
48
    {
49 81
        return new static(schemadb::from_path($path));
50
    }
51
52 44
    public function set_defaults(array $defaults) : self
53
    {
54 44
        $this->defaults = $defaults;
55 44
        return $this;
56
    }
57
58 170
    public function set_storage(midcom_core_dbaobject $storage = null, string $schemaname = null) : self
59
    {
60 170
        if (   $schemaname === null
61 170
            && !empty($storage->id)) {
62 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

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

122
            $this->renderer->/** @scrutinizer ignore-call */ 
123
                             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...
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

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

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