Completed
Push — master ( 8cdaf4...db6695 )
by Marcus
02:35
created

Myorders::preparePage()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 95
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 95
rs 5.4327
cc 8
eloc 68
nc 9
nop 0

How to fix   Long Method   

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
/*
4
    HCSF - A multilingual CMS and Shopsystem
5
    Copyright (C) 2014  Marcus Haase - [email protected]
6
7
    This program is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace HaaseIT\HCSF\Controller\Shop;
22
23
use HaaseIT\HCSF\Customer\Helper as CHelper;
24
use HaaseIT\HCSF\HelperConfig;
25
use HaaseIT\HCSF\Shop\Helper as SHelper;
26
use Zend\ServiceManager\ServiceManager;
27
28
class Myorders extends Base
29
{
30
    /**
31
     * @var \HaaseIT\Toolbox\Textcat
32
     */
33
    private $textcats;
34
35
    /**
36
     * @var \PDO
37
     */
38
    private $db;
39
40
    /**
41
     * Myorders constructor.
42
     * @param ServiceManager $serviceManager
43
     */
44
    public function __construct(ServiceManager $serviceManager)
45
    {
46
        parent::__construct($serviceManager);
47
        $this->textcats = $serviceManager->get('textcats');
48
        $this->db = $serviceManager->get('db');
49
    }
50
51
    public function preparePage()
0 ignored issues
show
Coding Style introduced by
preparePage uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
preparePage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
52
    {
53
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager);
54
        $this->P->cb_pagetype = 'content';
55
56
        if (!CHelper::getUserData()) {
57
            $this->P->oPayload->cl_html = $this->textcats->T("denied_notloggedin");
58
        } else {
59
            require_once PATH_BASEDIR . 'src/shop/functions.shoppingcart.php';
60
61
            $this->P->cb_customcontenttemplate = 'shop/myorders';
62
63
            if (isset($_GET["action"], $_GET["id"]) && $_GET["action"] === 'show') {
64
                $iId = \filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
65
66
                $sql = "SELECT * FROM " . 'orders WHERE o_id = :id AND o_custno = \'' . $_SESSION['user']['cust_no'] . '\' AND o_ordercompleted != \'d\'';
67
                $hResult = $this->db->prepare($sql);
68
                $hResult->bindValue(':id', $iId);
69
                $hResult->execute();
70
71
                if ($hResult->rowCount() == 1) {
72
                    $aOrder = $hResult->fetch();
73
74
                    $this->P->cb_customdata['orderdata']['ordertimestamp'] = date(
75
                        HelperConfig::$core["locale_format_date_time"],
76
                        $aOrder["o_ordertimestamp"]
77
                    );
78
                    $this->P->cb_customdata['orderdata']['orderremarks'] = $aOrder["o_remarks"];
79
                    $this->P->cb_customdata['orderdata']['paymentmethod'] = $this->textcats->T("order_paymentmethod_" . $aOrder["o_paymentmethod"]);
80
                    $this->P->cb_customdata['orderdata']['paymentcompleted'] = (($aOrder["o_paymentcompleted"] == 'y') ? $this->textcats->T("myorders_paymentstatus_completed") : $this->textcats->T("myorders_paymentstatus_open"));
81
                    $this->P->cb_customdata['orderdata']['orderstatus'] = SHelper::showOrderStatusText($this->textcats, $aOrder["o_ordercompleted"]);
82
                    $this->P->cb_customdata['orderdata']['shippingservice'] = $aOrder["o_shipping_service"];
83
                    $this->P->cb_customdata['orderdata']['trackingno'] = $aOrder["o_shipping_trackingno"];
84
85
                    $sql = 'SELECT * FROM orders_items WHERE oi_o_id = :id';
86
                    $hResult = $this->db->prepare($sql);
87
                    $hResult->bindValue(':id', $iId);
88
                    $hResult->execute();
89
90
                    $aItems = $hResult->fetchAll();
91
92
                    $aItemsforShoppingcarttable = [];
93
                    foreach ($aItems as $aValue) {
94
                        $aPrice = [
95
                            'netto_use' => $aValue["oi_price_netto_use"],
96
                            'brutto_use' => $aValue["oi_price_brutto_use"],
97
                        ];
98
                        $aItemsforShoppingcarttable[$aValue["oi_cartkey"]] = [
99
                            'amount' => $aValue["oi_amount"],
100
                            'price' => $aPrice,
101
                            'vat' => $aValue["oi_vat"],
102
                            //'rg' => $aValue["oi_rg"],
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
                            'name' => $aValue["oi_itemname"],
104
                            'img' => $aValue["oi_img"],
105
                        ];
106
                    }
107
108
                    $aShoppingcart = SHelper::buildShoppingCartTable(
109
                        $aItemsforShoppingcarttable,
110
                        true,
111
                        '',
112
                        '',
113
                        $aOrder["o_vatfull"],
114
                        $aOrder["o_vatreduced"]
115
                    );
116
                } else {
117
                    $this->P->cb_customdata['ordernotfound'] = true;
118
                }
119
            } else {
120
                $COList = [
121
                    ['title' => $this->textcats->T("order_head_orderdate"), 'key' => 'o_ordertime', 'width' => 110, 'linked' => false,],
122
                    ['title' => $this->textcats->T("order_head_paymenthethod"), 'key' => 'o_paymentmethod', 'width' => 125, 'linked' => false,],
123
                    ['title' => $this->textcats->T("order_head_paid"), 'key' => 'o_paymentcompleted', 'width' => 60, 'linked' => false,],
124
                    ['title' => $this->textcats->T("order_head_status"), 'key' => 'o_order_status', 'width' => 80, 'linked' => false,],
125
                    ['title' => $this->textcats->T("order_head_shipping_service"), 'key' => 'o_shipping_service', 'width' => 90, 'linked' => false,],
126
                    ['title' => $this->textcats->T("order_head_shipping_trackingno"), 'key' => 'o_shipping_trackingno', 'width' => 130, 'linked' => false,],
127
                    [
128
                        'title' => $this->textcats->T("order_show"),
129
                        'key' => 'o_id',
130
                        'width' => 120,
131
                        'linked' => true,
132
                        'ltarget' => '/_misc/myorders.html',
133
                        'lkeyname' => 'id',
134
                        'lgetvars' => ['action' => 'show',],
135
                    ],
136
                ];
137
138
                $this->P->cb_customdata['listmyorders'] = $this->showMyOrders($COList);
139
            }
140
141
            if (isset($aShoppingcart)) {
142
                $this->P->cb_customdata['shoppingcart'] = $aShoppingcart['shoppingcart'];
143
            }
144
        }
145
    }
146
147
    private function showMyOrders($COList)
148
    {
149
        $return = '';
150
        $sql = 'SELECT * FROM orders WHERE o_custno = :custno ORDER BY o_ordertimestamp DESC';
151
152
        $hResult = $this->db->prepare($sql);
153
        $hResult->bindValue(':custno', CHelper::getUserData('cust_no'));
154
        $hResult->execute();
155
156
        if ($hResult->rowCount() >= 1) {
157
            $aData = [];
158
            while ($aRow = $hResult->fetch()) {
159
                $sStatus = SHelper::showOrderStatusText($this->textcats, $aRow["o_ordercompleted"]);
160
161
                if ($aRow["o_paymentmethod"] === 'prepay') {
162
                    $sPaymentmethod = $this->textcats->T("order_paymentmethod_prepay");
163
                }
164
                elseif ($aRow["o_paymentmethod"] === 'paypal') {
165
                    $sPaymentmethod = $this->textcats->T("order_paymentmethod_paypal");
166
                }
167
                elseif ($aRow["o_paymentmethod"] === 'debit') {
168
                    $sPaymentmethod = $this->textcats->T("order_paymentmethod_debit");
169
                }
170
                elseif ($aRow["o_paymentmethod"] === 'invoice') {
171
                    $sPaymentmethod = $this->textcats->T("order_paymentmethod_invoice");
172
                } else {
173
                    $sPaymentmethod = ucwords($aRow["o_paymentmethod"]);
174
                }
175
176
                if ($aRow["o_paymentcompleted"] === 'y') {
177
                    $sPaymentstatus = ucwords($this->textcats->T("misc_yes"));
178
                } else {
179
                    $sPaymentstatus = ucwords($this->textcats->T("misc_no"));
180
                }
181
182
                $aData[] = [
183
                    'o_id' => $aRow["o_id"],
184
                    'o_order_status' => $sStatus,
185
                    'o_ordertime' => date(
186
                        HelperConfig::$customer['locale_format_date_time'],
187
                        $aRow["o_ordertimestamp"]
188
                    ),
189
                    'o_paymentmethod' => $sPaymentmethod,
190
                    'o_paymentcompleted' => $sPaymentstatus,
191
                    'o_shipping_service' => $aRow["o_shipping_service"],
192
                    'o_shipping_trackingno' => $aRow["o_shipping_trackingno"],
193
                ];
194
            }
195
            $return .= \HaaseIT\Toolbox\Tools::makeListtable($COList, $aData, $this->serviceManager->get('twig'));
196
        } else {
197
            $return .= $this->textcats->T("myorders_no_orders_to_display");
198
        }
199
200
        return $return;
201
    }
202
}