Passed
Push — master ( 037cc0...337528 )
by Andreas
13:19
created

datamanager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 93.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 115
ccs 55
cts 59
cp 0.9322
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 3
A add_post_button() 0 4 1
A get_button_config() 0 8 1
A prepare_url() 0 11 4
A configure() 0 10 1
A handle_save() 0 21 4
A add_dialog_button() 0 5 1
1
<?php
2
/**
3
 * @package midcom.workflow
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\workflow;
10
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use midcom_core_context;
13
use midcom;
14
use midcom\datamanager\controller;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @package midcom.workflow
20
 */
21
class datamanager extends dialog
22
{
23
    /**
24
     * @var controller
25
     */
26
    protected $controller;
27
28
    /**
29
     * @var callable
30
     */
31
    protected $save_callback;
32
33
    /**
34
     * Disable relocate after execute
35
     *
36
     * Returns the uimessage as JSON instead
37
     */
38
    protected bool $relocate;
39
40
    protected ?string $style = null;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 216
    public function configure(OptionsResolver $resolver)
46
    {
47 216
        $resolver
48 216
            ->setDefaults([
49 216
                'controller' => null,
50 216
                'save_callback' => null,
51 216
                'relocate' => true,
52 216
                'style' => null
53 216
            ])
54 216
            ->setAllowedTypes('controller', ['null', controller::class]);
55
    }
56
57 147
    public function get_button_config() : array
58
    {
59 147
        return [
60 147
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit', 'midcom'),
61 147
            MIDCOM_TOOLBAR_GLYPHICON => 'pencil',
62 147
            MIDCOM_TOOLBAR_OPTIONS => [
63 147
                'data-dialog' => 'dialog',
64 147
                'data-dialog-cancel-label' => midcom::get()->i18n->get_string('cancel', 'midcom')
65 147
            ]
66 147
        ];
67
    }
68
69 71
    public function run(Request $request) : Response
70
    {
71 71
        $this->state = $this->controller->handle($request);
72
73 71
        if ($this->state == controller::SAVE) {
74 13
            $script = $this->handle_save();
75 13
            return $this->js_response($script);
76
        }
77 71
        $context = midcom_core_context::get();
78 71
        if ($style = $this->style) {
79
            $callback = function() use ($style) {
80
                midcom::get()->style->show($style, 'POPUP');
0 ignored issues
show
Unused Code introduced by
The call to midcom_helper_style::show() has too many arguments starting with 'POPUP'. ( Ignorable by Annotation )

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

80
                midcom::get()->style->/** @scrutinizer ignore-call */ 
81
                                      show($style, 'POPUP');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
            };
82
        } else {
83 71
            $callback = [$this->controller, 'display_form'];
84
        }
85 71
        $context->set_key(MIDCOM_CONTEXT_SHOWCALLBACK, $callback);
86 71
        return $this->response($context);
87
    }
88
89 12
    protected function handle_save() : string
90
    {
91 12
        $dm = $this->controller->get_datamanager();
92 12
        $object = $dm->get_storage()->get_value();
93 12
        if ($object instanceof \midcom_core_dbaobject) {
94
            // we rebuild the form so that newly created child objects are listed with their proper DB identifiers
95 12
            $dm->set_storage($object);
96 12
            $data = $dm->get_content_html();
97 12
            $data['guid'] = $object->guid;
98
        } else {
99
            $data = $dm->get_content_html();
100
        }
101 12
        if ($this->relocate) {
102 10
            $url = null;
103 10
            if (is_callable($this->save_callback)) {
104 9
                $url = call_user_func($this->save_callback, $this->controller);
105
            }
106
107 10
            return 'refresh_opener(' . $this->prepare_url($url) . ', ' . json_encode($data) . ');';
108
        }
109 2
        return 'close(' . json_encode($data) . ');';
110
    }
111
112 3
    public function add_post_button(string $url, string $label, array $args)
113
    {
114 3
        $this->add_dialog_js();
115 3
        midcom::get()->head->add_jscript('add_post_button(' . $this->prepare_url($url) . ', "' . $label . '", ' . json_encode($args) . ');');
116
    }
117
118 5
    public function add_dialog_button(dialog $dialog, string $url)
119
    {
120 5
        $config = $dialog->get_button_config();
121 5
        $this->add_dialog_js();
122 5
        midcom::get()->head->add_jscript('add_dialog_button(' . $this->prepare_url($url) . ', "' . $config[MIDCOM_TOOLBAR_LABEL] . '", ' . json_encode($config[MIDCOM_TOOLBAR_OPTIONS]) . ');');
123
    }
124
125 18
    private function prepare_url(?string $url) : string
126
    {
127 18
        if ($url === null) {
128 5
            return 'undefined';
129
        }
130
131 13
        if (   !str_starts_with($url, '/')
132 13
            && ! preg_match('|^https?://|', $url)) {
133 3
            $url = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . $url;
0 ignored issues
show
Bug introduced by
Are you sure midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

133
            $url = /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . $url;
Loading history...
134
        }
135 13
        return '"' . $url . '"';
136
    }
137
}
138