Completed
Push — master ( 534aa6...b0bf1d )
by Andreas
17:36
created

net_nehmer_static_handler_create   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 65.63%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 75
ccs 21
cts 32
cp 0.6563
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _handler_create() 0 20 2
A save_callback() 0 19 4
A load_controller() 0 11 2
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 create page handler
15
 *
16
 * @package net.nehmer.static
17
 */
18
class net_nehmer_static_handler_create extends midcom_baseclasses_components_handler
19
{
20
    /**
21
     * The article which has been created
22
     *
23
     * @var midcom_db_article
24
     */
25
    private $article;
26
27
    /**
28
     * @param string $schemaname
29
     * @param array $defaults
30
     * @return \midcom\datamanager\controller
31
     */
32 2
    private function load_controller($schemaname, array $defaults)
33
    {
34 2
        if ($this->_config->get('simple_name_handling')) {
35 2
            $this->_request_data['schemadb']->get($schemaname)->get_field('name')['hidden'] = true;
36
        }
37 2
        $dm = new datamanager($this->_request_data['schemadb']);
38
39
        return $dm
40 2
            ->set_defaults($defaults)
41 2
            ->set_storage($this->article, $schemaname)
42 2
            ->get_controller();
43
    }
44
45
    /**
46
     * Displays an article create view.
47
     *
48
     * @param Request $request The request object
49
     * @param mixed $handler_id The ID of the handler.
50
     * @param array $args The argument list.
51
     */
52 2
    public function _handler_create(Request $request, $handler_id, array $args)
53
    {
54 2
        $this->_topic->require_do('midgard:create');
55 2
        $this->article = new midcom_db_article();
56 2
        $this->article->topic = $this->_topic->id;
57
58 2
        $defaults = [];
59 2
        if ($handler_id == 'createindex') {
60 1
            $defaults['name'] = 'index';
61
        }
62 2
        $controller = $this->load_controller($args[0], $defaults);
63
64 2
        $title = sprintf($this->_l10n_midcom->get('create %s'), $this->_l10n->get($controller->get_datamanager()->get_schema()->get('description')));
65 2
        midcom::get()->head->set_pagetitle($title);
66
67 2
        $workflow = $this->get_workflow('datamanager', [
68 2
            'controller' => $controller,
69 2
            'save_callback' => [$this, 'save_callback']
70
        ]);
71 2
        return $workflow->run($request);
72
    }
73
74
    public function save_callback(controller $controller)
75
    {
76
        // Reindex the article
77
        $indexer = midcom::get()->indexer;
78
        net_nehmer_static_viewer::index($controller->get_datamanager(), $indexer, $this->_topic);
79
80
        if ($this->_config->get('callback_function')) {
81
            if ($this->_config->get('callback_snippet')) {
82
                midcom_helper_misc::include_snippet_php($this->_config->get('callback_snippet'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('callback_snippet') can also be of type false; however, parameter $path of midcom_helper_misc::include_snippet_php() 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

82
                midcom_helper_misc::include_snippet_php(/** @scrutinizer ignore-type */ $this->_config->get('callback_snippet'));
Loading history...
83
            }
84
85
            $callback = $this->_config->get('callback_function');
86
            $callback($this->article, $this->_topic);
87
        }
88
89
        if ($this->article->name == 'index') {
90
            return '';
91
        }
92
        return $this->article->name . '/';
93
    }
94
}
95