Completed
Push — master ( d06e67...9410bd )
by Andreas
17:48 queued 08:20
created

datamanager::get_name()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 1
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;
27
28
    /**
29
     * @var schema
30
     */
31
    private $schema;
32
33
    /**
34
     * @var storage\container\container
35
     */
36
    private $storage;
37
38
    /**
39
     * @var array
40
     */
41
    private $defaults = [];
42
43
    /**
44
     * @var renderer
45
     */
46
    private $renderer;
47
48
    /**
49
     * @var Form
50
     */
51
    private $form;
52
53 238
    public function __construct(schemadb $schemadb)
54
    {
55 238
        $this->schemadb = $schemadb;
56 238
    }
57
58 144
    private static function get_factory() : FormFactoryInterface
59
    {
60 144
        return midcom::get()->getContainer()->get('form.factory');
61
    }
62
63 79
    public static function from_schemadb($path) : self
64
    {
65 79
        return new static(schemadb::from_path($path));
66
    }
67
68 40
    public function set_defaults(array $defaults) : self
69
    {
70 40
        $this->defaults = $defaults;
71 40
        return $this;
72
    }
73
74
    /**
75
     * @param midcom_core_dbaobject $storage
76
     * @param string $schema
77
     */
78 233
    public function set_storage(midcom_core_dbaobject $storage = null, $schemaname = null) : self
79
    {
80 233
        if (   $schemaname === null
81 233
            && !empty($storage->id)) {
82 72
            $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

82
            /** @scrutinizer ignore-call */ 
83
            $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...
83
        }
84
85 233
        $this->set_schema($schemaname);
86
87 233
        $defaults = array_merge($this->schema->get_defaults(), $this->defaults);
88 233
        if ($storage === null) {
89 13
            $this->storage = new storage\container\nullcontainer($this->schema, $defaults);
90
        } else {
91 220
            $this->storage = new storage\container\dbacontainer($this->schema, $storage, $defaults);
92
        }
93
94 233
        if ($this->form !== null) {
95 3
            if ($this->form->isSubmitted()) {
96 2
                $this->form = null;
97
            } else {
98 1
                $this->form->setData($this->storage);
99
            }
100
        }
101
102 233
        return $this;
103
    }
104
105 233
    private function set_schema($name)
106
    {
107 233
        if ($name && !$this->schemadb->has($name)) {
108
            debug_add("Given schema name {$name} was not found, reverting to default.", MIDCOM_LOG_INFO);
109
            $name = null;
110
        }
111
112 233
        $schema = ($name) ? $this->schemadb->get($name) : $this->schemadb->get_first();
113 233
        if ($this->schema !== null && $this->schema->get_name() !== $schema->get_name()) {
114
            $this->form = null;
115
        }
116 233
        $this->schema = $schema;
117 233
    }
118
119
    /**
120
     * @param string $name
121
     */
122 225
    public function get_schema($name = null) : schema
123
    {
124 225
        if ($name) {
125 1
            return $this->schemadb->get($name);
126
        }
127 225
        if ($this->schema === null) {
128 12
            $this->set_schema($name);
129
        }
130 225
        return $this->schema;
131
    }
132
133 146
    public function get_storage() : container
134
    {
135 146
        if (!$this->storage) {
136 12
            $this->set_storage(null);
137
        }
138 146
        return $this->storage;
139
    }
140
141 140
    public function get_renderer($template = null, $skip_empty = false) : renderer
142
    {
143 140
        if ($this->renderer === null) {
144 140
            $this->renderer = new renderer(new engine);
145 140
            $this->renderer->set_l10n($this->schema->get_l10n());
146
        }
147 140
        if ($template) {
148 140
            if (is_string($template)) {
149 140
                $config = \midcom_baseclasses_components_configuration::get('midcom.datamanager', 'config');
150 140
                $templates = $config->get('templates');
151 140
                if (!array_key_exists($template, $templates)) {
152
                    throw new \midcom_error('Template ' . $template . ' not found in config');
153
                }
154 140
                $template = new $templates[$template]($this->renderer, $skip_empty);
155
            }
156 140
            $view = $this->get_form()->createView();
157 140
            $this->renderer->set_template($view, $template);
158
        }
159 140
        return $this->renderer;
160
    }
161
162
    /**
163
     * @param string $name
164
     */
165 104
    public function get_controller($name = null) : controller
166
    {
167 104
        return new controller($this, $name);
168
    }
169
170
    /**
171
     * @param string $name
172
     * @param boolean $reset
173
     */
174 144
    public function get_form($name = null, $reset = false) : Form
175
    {
176 144
        if ($reset) {
177
            $this->form = null;
178
        }
179 144
        $name = $this->get_name($name);
180 142
181
        if (   $this->form === null
182 142
            || $this->form->getName() != $name) {
183
            $this->build_form($this->get_builder($name));
184 144
        }
185
        return $this->form;
186 3
    }
187
188
    public function get_builder(string $name = null) : FormBuilderInterface
189 144
    {
190 144
        $config = [
191
            'schema' => $this->get_schema()
192 144
        ];
193
        return self::get_factory()->createNamedBuilder($this->get_name($name), schemaType::class, null, $config);
194 144
    }
195 144
196
    public function build_form(FormBuilderInterface $builder) : self
197
    {
198 144
        $storage = $this->get_storage();
199 144
200 144
        $config = [
201
            'operations' => $this->schema->get('operations'),
202
            'index_method' => 'noindex',
203 144
            'is_create' => $storage instanceof dbacontainer && empty($storage->get_value()->id)
204
        ];
205 144
        $builder->add('form_toolbar', toolbarType::class, $config);
206 144
207
        $this->form = $builder->getForm()
208 144
            ->setData($storage);
209
210
        return $this;
211 10
    }
212
213 10
    private function get_name(?string $name) : string
214
    {
215 10
        if (!$name) {
216
            $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
217 10
            // Replace the dots in the component name with underscores
218 10
            $name = midcom::get()->componentloader->path_to_prefix($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type false; however, parameter $path of midcom_helper__componentloader::path_to_prefix() 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

218
            $name = midcom::get()->componentloader->path_to_prefix(/** @scrutinizer ignore-type */ $name);
Loading history...
219 10
        }
220 1
        return $name ?: 'midcom_helper_datamanager2';
221 1
    }
222
223
    public function get_content_raw() : array
224
    {
225 10
        $ret = [];
226
227
        foreach ($this->storage as $field => $value)
228 2
        {
229
            $ret[$field] = $value->get_value();
230 2
            $config = $this->schema->get_field($field);
231
            if (!empty($config['type_config']['allow_multiple'])) {
232 2
                $transformer = new multipleTransformer($config);
233 2
                $ret[$field] = $transformer->transform($ret[$field]);
234 2
            }
235 2
        }
236
237 2
        return $ret;
238
    }
239
240 2
    public function get_content_csv() : array
241
    {
242
        $ret = [];
243 34
244
        $renderer = $this->get_renderer('csv');
245 34
        foreach ($renderer->get_view()->children as $name => $value) {
246
            if ($name == 'form_toolbar') {
247 34
                continue;
248 34
            }
249 34
            $ret[$name] = $renderer->widget($value);
250 34
        }
251
252 34
        return $ret;
253
    }
254 34
255
    public function get_content_html() : array
256
    {
257 11
        $ret = [];
258
259 11
        $renderer = $this->get_renderer('view');
260 11
        foreach ($renderer->get_view()->children as $name => $value) {
261 11
            if ($name == 'form_toolbar') {
262
                continue;
263
            }
264
            $ret[$name] = $renderer->widget($value);
265
        }
266
        return $ret;
267
    }
268
269
    public function display_view($skip_empty = false)
270
    {
271
        $renderer = $this->get_renderer('view', $skip_empty);
272
        echo $renderer->block($renderer->get_view(), 'form');
273
    }
274
275
    public function recreate() : bool
276
    {
277
        $ret = true;
278
        foreach ($this->storage as $field) {
279
            if (   $field instanceof recreateable
280
                && !$field->recreate()) {
281
                $ret = false;
282
            }
283
        }
284
        return $ret;
285
    }
286
}
287