Passed
Push — master ( dd49f8...f56759 )
by Andreas
12:21
created

org_openpsa_invoices_status::get_status_entries()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 70
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 68.7814

Importance

Changes 0
Metric Value
cc 12
eloc 52
nc 40
nop 0
dl 0
loc 70
ccs 16
cts 60
cp 0.2667
crap 68.7814
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package org.openpsa.invoices
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
/**
10
 * @package org.openpsa.invoices
11
 */
12
class org_openpsa_invoices_status extends org_openpsa_widgets_status
13
{
14
    private org_openpsa_invoices_invoice_dba $invoice;
15
16
    private midcom_services_i18n_l10n $l10n;
17
18
    private midcom_services_i18n_l10n $l10n_midcom;
19
20 1
    public function __construct(org_openpsa_invoices_invoice_dba $invoice)
21
    {
22 1
        $this->l10n = midcom::get()->i18n->get_l10n('org.openpsa.invoices');
23 1
        $this->l10n_midcom = midcom::get()->i18n->get_l10n('midcom');
24 1
        $this->invoice = $invoice;
25
    }
26
27 1
    public function get_current_status() : string
28
    {
29 1
        return match ($this->invoice->get_status()) {
30 1
            'unsent' => $this->l10n->get('unsent'),
31
            'open' => sprintf($this->l10n->get('due on %s'), $this->l10n->get_formatter()->date($this->invoice->due)),
32
            'overdue' => '<span class="bad">' . sprintf($this->l10n->get('overdue since %s'), $this->l10n->get_formatter()->date($this->invoice->due)) . '</span>',
33
            'paid' => sprintf($this->l10n->get('paid on %s'), $this->l10n->get_formatter()->date($this->invoice->paid)),
34
            'canceled' => sprintf($this->l10n->get('invoice canceled on %s'), $this->l10n->get_formatter()->date($this->invoice->paid)),
35 1
            'default' => sprintf($this->l10n->get('default on payment on %s'), $this->l10n->get_formatter()->date($this->invoice->defaultdate))
36 1
        };
37
    }
38
39 1
    public function get_status_class() : string
40
    {
41 1
        return $this->invoice->get_status();
42
    }
43
44 1
    public function get_button() : string
45
    {
46 1
        $tooltip = midcom::get()->i18n->get_string('add journal entry', 'org.openpsa.relatedto');
47 1
        $save_label = $this->l10n_midcom->get('save');
48 1
        $cancel_label = $this->l10n_midcom->get('cancel');
49 1
        return '<a id="add-journal-entry" data-guid="' . $this->invoice->guid . '" data-dialog-submit-label="' . $save_label . '" data-dialog-cancel-label="' . $cancel_label . '" title="' . $tooltip . "\">\n" .
50 1
            '<i class="fa fa-plus" title="' . $tooltip . "\"></i></a>\n";
51
    }
52
53 1
    public function get_history() : array
54
    {
55 1
        $entries = array_merge($this->get_status_entries(), $this->get_journal_entries());
56
57 1
        usort($entries, function (array $a, array $b) {
58
            if ($a['timestamp'] == $b['timestamp']) {
59
                return $b['order'] <=> $a['order'];
60
            }
61
            return $b['timestamp'] <=> $a['timestamp'];
62 1
        });
63
64 1
        return $entries;
65
    }
66
67 1
    private function get_status_entries() : array
68
    {
69 1
        $entries = [];
70 1
        if ($this->invoice->defaultdate) {
71
            $entries[] = [
72
                'timestamp' => $this->invoice->defaultdate,
73
                'message' => $this->l10n->get('default'),
74
                'order' => 5
75
            ];
76 1
        } elseif ($this->invoice->cancelationInvoice) {
77
            $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
78
            $cancelation_invoice = new org_openpsa_invoices_invoice_dba($this->invoice->cancelationInvoice);
79
            $cancelation_invoice_link = $prefix . 'invoice/' . $cancelation_invoice->guid . '/';
0 ignored issues
show
Bug introduced by
Are you sure $prefix 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

79
            $cancelation_invoice_link = /** @scrutinizer ignore-type */ $prefix . 'invoice/' . $cancelation_invoice->guid . '/';
Loading history...
80
            $cancelation_invoice_link = "<a href=\"" . $cancelation_invoice_link . "\">" . $this->l10n->get('invoice') . " " . $cancelation_invoice->get_label() . "</a>";
81
            $entries[] = [
82
                'timestamp' => $cancelation_invoice->metadata->created,
83
                'message' => sprintf($this->l10n->get('invoice got canceled by %s'), $cancelation_invoice_link),
84
                'order' => 4
85
            ];
86 1
        } elseif ($this->invoice->paid) {
87
            $entries[] = [
88
                'timestamp' => $this->invoice->paid,
89
                'message' => sprintf($this->l10n->get('marked invoice %s paid'), ''),
90
                'order' => 3
91
            ];
92
        }
93 1
        if (   $this->invoice->due
94 1
            && (   (   $this->invoice->due < time()
95 1
                    && $this->invoice->paid == 0)
96 1
                || $this->invoice->due < $this->invoice->paid)) {
97
            $entries[] = [
98
                'timestamp' => $this->invoice->due,
99
                'message' => $this->l10n->get('overdue'),
100
                'order' => 2
101
            ];
102
        }
103
104 1
        if ($this->invoice->sent) {
105
            if ($mail_time = $this->invoice->get_parameter('org.openpsa.invoices', 'sent_by_mail')) {
106
                $entries[] = [
107
                    'timestamp' => $mail_time,
108
                    'message' => sprintf($this->l10n->get('marked invoice %s sent per mail'), ''),
109
                    'order' => 1
110
                ];
111
            } else {
112
                $entries[] = [
113
                    'timestamp' => $this->invoice->sent,
114
                    'message' => sprintf($this->l10n->get('marked invoice %s sent'), ''),
115
                    'order' => 1
116
                ];
117
            }
118
119
            $reminders = $this->invoice->get_parameter('org.openpsa.invoices', 'sent_payment_reminder');
120
            if ($reminders) {
121
                $reminder_times = json_decode($reminders, true);
122
                foreach ($reminder_times as $reminder_time) {
123
                    $entries[] = [
124
                        'timestamp' => $reminder_time,
125
                        'message' => sprintf($this->l10n->get('marked invoice %s payment reminder sent'), ''),
126
                        'order' => 1
127
                    ];
128
                }
129
            }
130
        }
131 1
        $entries[] = [
132 1
            'timestamp' => $this->invoice->metadata->created,
133 1
            'message' => sprintf($this->l10n->get('invoice %s created'), ''),
134 1
            'order' => 0
135 1
        ];
136 1
        return $entries;
137
    }
138
139 1
    private function get_journal_entries() : array
140
    {
141 1
        $entries = [];
142
143 1
        $mc = org_openpsa_relatedto_journal_entry_dba::new_collector('linkGuid', $this->invoice->guid);
144 1
        $rows = $mc->get_rows(['title', 'metadata.created']);
145
146 1
        foreach ($rows as $row) {
147
            $entries[] = [
148
                'timestamp' => strtotime((string) $row['created']),
149
                'message' => $row['title'],
150
                'order' => 0
151
            ];
152
        }
153 1
        return $entries;
154
    }
155
}
156