Passed
Push — master ( 366a1b...cc2a52 )
by Andreas
23:47
created

net_nehmer_blog_handler_view::_seek_comments()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 0
dl 0
loc 24
ccs 0
cts 12
cp 0
crap 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.blog
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
11
/**
12
 * Blog index page handler
13
 *
14
 * @package net.nehmer.blog
15
 */
16
class net_nehmer_blog_handler_view extends midcom_baseclasses_components_handler
17
{
18
    use net_nehmer_blog_handler;
0 ignored issues
show
introduced by
The trait net_nehmer_blog_handler requires some properties which are not provided by net_nehmer_blog_handler_view: $admin, $name, $auth, $url, $guid, $revised, $metadata, $user
Loading history...
19
20
    /**
21
     * The article to display
22
     *
23
     * @var midcom_db_article
24
     */
25
    private $_article;
26
27
    /**
28
     * The Datamanager of the article to display.
29
     *
30
     * @var datamanager
31
     */
32
    private $_datamanager;
33
34
    /**
35
     * Simple helper which references all important members to the request data listing
36
     * for usage within the style listing.
37
     */
38 1
    private function _prepare_request_data()
39
    {
40 1
        $this->_request_data['article'] = $this->_article;
41 1
        $this->_request_data['datamanager'] = $this->_datamanager;
42
43 1
        $buttons = [];
44 1
        $workflow = $this->get_workflow('datamanager');
45 1
        if ($this->_article->can_do('midgard:update')) {
46 1
            $buttons[] = $workflow->get_button("edit/{$this->_article->guid}/", [
47 1
                MIDCOM_TOOLBAR_ACCESSKEY => 'e',
48
            ]);
49
        }
50
51 1
        if (   $this->_article->topic === $this->_topic->id
52 1
            && $this->_article->can_do('midgard:delete')) {
53 1
            $delete = $this->get_workflow('delete', ['object' => $this->_article]);
54 1
            $buttons[] = $delete->get_button("delete/{$this->_article->guid}/");
55
        }
56 1
        $this->_view_toolbar->add_items($buttons);
57 1
    }
58
59
    /**
60
     * Handle actual article display
61
     */
62 1
    public function _handler_view(string $handler_id, array $args, array &$data)
63
    {
64 1
        $qb = midcom_db_article::new_query_builder();
65 1
        $this->article_qb_constraints($qb);
66
67 1
        $qb->begin_group('OR');
68 1
        $qb->add_constraint('name', '=', $args[0]);
69 1
        $qb->add_constraint('guid', '=', $args[0]);
70 1
        $qb->end_group();
71 1
        $this->_article = $qb->get_result(0);
0 ignored issues
show
Documentation Bug introduced by
It seems like $qb->get_result(0) can also be of type false. However, the property $_article is declared as type midcom_db_article. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
72 1
        if (!$this->_article) {
73
            throw new midcom_error_notfound('Could not find ' . $args[0]);
74
        }
75
76 1
        if ($handler_id == 'view-raw') {
77
            midcom::get()->skip_page_style = true;
78
        }
79
80 1
        $this->_datamanager = new datamanager($data['schemadb']);
81 1
        $this->_datamanager->set_storage($this->_article);
82
83 1
        if ($this->_config->get('comments_enable')) {
84
            if ($node = net_nehmer_comments_interface::get_node($this->_topic, $this->_config->get('comments_topic'))) {
85
                $this->_request_data['comments_url'] = $node[MIDCOM_NAV_RELATIVEURL] . "comment/{$this->_article->guid}";
86
                if (   $this->_topic->can_do('midgard:update')
87
                    && $this->_topic->can_do('net.nehmer.comments:moderation')) {
88
                    net_nehmer_comments_viewer::add_head_elements();
89
                }
90
            }
91
            // TODO: Should we tell admin to create a net.nehmer.comments folder?
92
        }
93
94 1
        $this->add_breadcrumb($this->get_url($this->_article), $this->_article->title);
95
96 1
        $this->_prepare_request_data();
97
98 1
        $this->bind_view_to_object($this->_article, $this->_datamanager->get_schema()->get_name());
99 1
        midcom::get()->metadata->set_request_metadata($this->_article->metadata->revised, $this->_article->guid);
100 1
        midcom::get()->head->set_pagetitle("{$this->_topic->extra}: {$this->_article->title}");
101 1
        $data['view_article'] = $this->_datamanager->get_content_html();
102 1
        return $this->show('view');
103
    }
104
}
105