Completed
Push — master ( a16483...3b8ba7 )
by Andreas
17:21
created

datamanager::set_storage()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 12
nop 2
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 6
rs 9.2222
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 Symfony\Component\Translation\Translator;
14
use midcom\datamanager\extension\transformer\multipleTransformer;
15
use midcom\datamanager\storage\recreateable;
16
use midcom\datamanager\extension\type\schemaType;
17
use midcom\datamanager\extension\type\toolbarType;
18
use midcom\datamanager\storage\container\container;
19
use midcom\datamanager\storage\container\dbacontainer;
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
        if ($name == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $name of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
180 142
            $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
181
            // Replace the dots in the component name with underscores
182 142
            $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

182
            $name = midcom::get()->componentloader->path_to_prefix(/** @scrutinizer ignore-type */ $name);
Loading history...
183
        }
184 144
        if (!$name) {
185
            // Fallback for componentless operation
186 3
            $name = 'midcom_helper_datamanager2';
187
        }
188
189 144
        if (   $this->form === null
190 144
            || $this->form->getName() != $name) {
191
            $config = [
192 144
                'schema' => $this->get_schema()
193
            ];
194 144
            $builder = self::get_factory()->createNamedBuilder($name, schemaType::class, null, $config);
195 144
            $storage = $this->get_storage();
196
197
            $config = [
198 144
                'operations' => $this->schema->get('operations'),
199 144
                'index_method' => 'noindex',
200 144
                'is_create' => $storage instanceof dbacontainer && empty($storage->get_value()->id)
201
            ];
202
203 144
            $builder->add('form_toolbar', toolbarType::class, $config);
204
205 144
            $this->form = $builder->getForm()
206 144
                ->setData($storage);
207
        }
208 144
        return $this->form;
209
    }
210
211 10
    public function get_content_raw() : array
212
    {
213 10
        $ret = [];
214
215 10
        foreach ($this->storage as $field => $value)
216
        {
217 10
            $ret[$field] = $value->get_value();
218 10
            $config = $this->schema->get_field($field);
219 10
            if (!empty($config['type_config']['allow_multiple'])) {
220 1
                $transformer = new multipleTransformer($config);
221 1
                $ret[$field] = $transformer->transform($ret[$field]);
222
            }
223
        }
224
225 10
        return $ret;
226
    }
227
228 2
    public function get_content_csv() : array
229
    {
230 2
        $ret = [];
231
232 2
        $renderer = $this->get_renderer('csv');
233 2
        foreach ($renderer->get_view()->children as $name => $value) {
234 2
            if ($name == 'form_toolbar') {
235 2
                continue;
236
            }
237 2
            $ret[$name] = $renderer->widget($value);
238
        }
239
240 2
        return $ret;
241
    }
242
243 34
    public function get_content_html() : array
244
    {
245 34
        $ret = [];
246
247 34
        $renderer = $this->get_renderer('view');
248 34
        foreach ($renderer->get_view()->children as $name => $value) {
249 34
            if ($name == 'form_toolbar') {
250 34
                continue;
251
            }
252 34
            $ret[$name] = $renderer->widget($value);
253
        }
254 34
        return $ret;
255
    }
256
257 11
    public function display_view($skip_empty = false)
258
    {
259 11
        $renderer = $this->get_renderer('view', $skip_empty);
260 11
        echo $renderer->block($renderer->get_view(), 'form');
261 11
    }
262
263
    public function recreate() : bool
264
    {
265
        $ret = true;
266
        foreach ($this->storage as $field) {
267
            if (   $field instanceof recreateable
268
                && !$field->recreate()) {
269
                $ret = false;
270
            }
271
        }
272
        return $ret;
273
    }
274
}
275