Completed
Push — master ( 474902...a16483 )
by Andreas
17:23
created

datamanager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 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
    private static function add_translation_resource(Translator $translator, $object)
0 ignored issues
show
Unused Code introduced by
The method add_translation_resource() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
64
    {
65
        $rc = new \ReflectionClass($object);
66
        $path = dirname($rc->getFileName());
67
        $lang = $translator->getLocale();
68
        $translator->addResource('xlf', $path . '/Resources/translations/validators.' . $lang . '.xlf', $lang);
69
    }
70
71 79
    public static function from_schemadb($path) : self
72
    {
73 79
        return new static(schemadb::from_path($path));
74
    }
75
76 40
    public function set_defaults(array $defaults) : self
77
    {
78 40
        $this->defaults = $defaults;
79 40
        return $this;
80
    }
81
82
    /**
83
     * @param midcom_core_dbaobject $storage
84
     * @param string $schema
85
     */
86 233
    public function set_storage(midcom_core_dbaobject $storage = null, $schemaname = null) : self
87
    {
88 233
        if (   $schemaname === null
89 233
            && !empty($storage->id)) {
90 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

90
            /** @scrutinizer ignore-call */ 
91
            $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...
91
        }
92
93 233
        $this->set_schema($schemaname);
94
95 233
        $defaults = array_merge($this->schema->get_defaults(), $this->defaults);
96 233
        if ($storage === null) {
97 13
            $this->storage = new storage\container\nullcontainer($this->schema, $defaults);
98
        } else {
99 220
            $this->storage = new storage\container\dbacontainer($this->schema, $storage, $defaults);
100
        }
101
102 233
        if ($this->form !== null) {
103 3
            if ($this->form->isSubmitted()) {
104 2
                $this->form = null;
105
            } else {
106 1
                $this->form->setData($this->storage);
107
            }
108
        }
109
110 233
        return $this;
111
    }
112
113 233
    private function set_schema($name)
114
    {
115 233
        if ($name && !$this->schemadb->has($name)) {
116
            debug_add("Given schema name {$name} was not found, reverting to default.", MIDCOM_LOG_INFO);
117
            $name = null;
118
        }
119
120 233
        $schema = ($name) ? $this->schemadb->get($name) : $this->schemadb->get_first();
121 233
        if ($this->schema !== null && $this->schema->get_name() !== $schema->get_name()) {
122
            $this->form = null;
123
        }
124 233
        $this->schema = $schema;
125 233
    }
126
127
    /**
128
     * @param string $name
129
     */
130 225
    public function get_schema($name = null) : schema
131
    {
132 225
        if ($name) {
133 1
            return $this->schemadb->get($name);
134
        }
135 225
        if ($this->schema === null) {
136 12
            $this->set_schema($name);
137
        }
138 225
        return $this->schema;
139
    }
140
141 146
    public function get_storage() : container
142
    {
143 146
        if (!$this->storage) {
144 12
            $this->set_storage(null);
145
        }
146 146
        return $this->storage;
147
    }
148
149 140
    public function get_renderer($template = null, $skip_empty = false) : renderer
150
    {
151 140
        if ($this->renderer === null) {
152 140
            $this->renderer = new renderer(new engine);
153 140
            $this->renderer->set_l10n($this->schema->get_l10n());
154
        }
155 140
        if ($template) {
156 140
            if (is_string($template)) {
157 140
                $config = \midcom_baseclasses_components_configuration::get('midcom.datamanager', 'config');
158 140
                $templates = $config->get('templates');
159 140
                if (!array_key_exists($template, $templates)) {
160
                    throw new \midcom_error('Template ' . $template . ' not found in config');
161
                }
162 140
                $template = new $templates[$template]($this->renderer, $skip_empty);
163
            }
164 140
            $view = $this->get_form()->createView();
165 140
            $this->renderer->set_template($view, $template);
166
        }
167 140
        return $this->renderer;
168
    }
169
170
    /**
171
     * @param string $name
172
     */
173 104
    public function get_controller($name = null) : controller
174
    {
175 104
        return new controller($this, $name);
176
    }
177
178
    /**
179
     * @param string $name
180
     * @param boolean $reset
181
     */
182 144
    public function get_form($name = null, $reset = false) : Form
183
    {
184 144
        if ($reset) {
185
            $this->form = null;
186
        }
187 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...
188 142
            $name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
189
            // Replace the dots in the component name with underscores
190 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

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