Passed
Push — master ( 38481c...dc3f28 )
by Andreas
13:10
created

net_nehmer_static_handler_admin::save_callback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.static
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midcom\datamanager\datamanager;
10
use midcom\datamanager\controller;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * n.n.static admin page handler
15
 *
16
 * @package net.nehmer.static
17
 */
18
class net_nehmer_static_handler_admin extends midcom_baseclasses_components_handler
19
{
20
    private midcom_db_article $article;
21
22 1
    private function load_controller() : controller
23
    {
24 1
        if (    $this->_config->get('simple_name_handling')
25 1
             && !midcom::get()->auth->admin) {
26 1
            foreach ($this->_request_data['schemadb']->all() as $schema) {
27 1
                $schema->get_field('name')['readonly'] = true;
28
            }
29
        }
30 1
        $dm = new datamanager($this->_request_data['schemadb']);
31
32 1
        return $dm
33 1
            ->set_storage($this->article)
34 1
            ->get_controller();
35
    }
36
37
    /**
38
     * Displays an article edit view.
39
     */
40 1
    public function _handler_edit(Request $request, string $guid)
41
    {
42 1
        $this->article = new midcom_db_article($guid);
43
44
        // Relocate for the correct content topic, let the true content topic take care of the ACL
45 1
        if ($this->article->topic !== $this->_topic->id) {
46
            $nap = new midcom_helper_nav();
47
            $node = $nap->get_node($this->article->topic);
48
49
            if (!empty($node[MIDCOM_NAV_ABSOLUTEURL])) {
50
                return new midcom_response_relocate($node[MIDCOM_NAV_ABSOLUTEURL] . "edit/{$guid}/");
51
            }
52
            throw new midcom_error_notfound("The article with GUID {$guid} was not found.");
53
        }
54
55 1
        $this->article->require_do('midgard:update');
56 1
        midcom::get()->head->set_pagetitle(sprintf($this->_l10n_midcom->get('edit %s'), $this->article->title));
57
58 1
        $workflow = $this->get_workflow('datamanager', [
59 1
            'controller' => $this->load_controller(),
60 1
            'save_callback' => $this->save_callback(...)
61 1
        ]);
62 1
        if ($this->article->can_do('midgard:delete')) {
63 1
            $delete = $this->get_workflow('delete', ['object' => $this->article]);
64 1
            $workflow->add_dialog_button($delete, $this->router->generate('delete', ['guid' => $this->article->guid]));
0 ignored issues
show
Bug introduced by
The method add_dialog_button() does not exist on midcom\workflow\dialog. Did you maybe mean add_dialog_js()? ( Ignorable by Annotation )

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

64
            $workflow->/** @scrutinizer ignore-call */ 
65
                       add_dialog_button($delete, $this->router->generate('delete', ['guid' => $this->article->guid]));

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...
65
        }
66 1
        return $workflow->run($request);
67
    }
68
69
    public function save_callback(controller $controller)
70
    {
71
        // Reindex the article
72
        $indexer = midcom::get()->indexer;
73
        net_nehmer_static_viewer::index($controller->get_datamanager(), $indexer, $this->_topic);
74
        if ($this->article->name == 'index') {
75
            return $this->router->generate('index');
76
        }
77
        return $this->router->generate('view', ['name' => $this->article->name]);
78
    }
79
80
    /**
81
     * Displays an article delete confirmation view.
82
     */
83 1
    public function _handler_delete(Request $request, string $guid)
84
    {
85 1
        $this->article = new midcom_db_article($guid);
86 1
        if ($this->article->topic !== $this->_topic->id) {
87
            throw new midcom_error_forbidden('Article does not belong to this topic');
88
        }
89 1
        $workflow = $this->get_workflow('delete', ['object' => $this->article]);
90 1
        return $workflow->run($request);
91
    }
92
}
93