Failed Conditions
Pull Request — 3.0.9-dev (#1439)
by
unknown
216:43 queued 201:24
created

AdminController::index()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 73
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 10.9572
Metric Value
dl 0
loc 73
ccs 8
cts 33
cp 0.2424
rs 8.6829
cc 4
eloc 41
nc 3
nop 2
crap 10.9572

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
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin;
26
27
use Doctrine\ORM\NoResultException;
28
use Doctrine\ORM\Query\ResultSetMapping;
29
use Eccube\Application;
30
use Eccube\Common\Constant;
31
use Eccube\Controller\AbstractController;
32
use Symfony\Component\HttpFoundation\Request;
33
34
class AdminController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36 3 View Code Duplication
    public function login(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        if ($app->isGranted('ROLE_ADMIN')) {
39
            return $app->redirect($app->url('admin_homepage'));
40
        }
41
42
        /* @var $form \Symfony\Component\Form\FormInterface */
43
        $form = $app['form.factory']
44
            ->createNamedBuilder('', 'admin_login')
45
            ->getForm();
46
47 3
        return $app->render('login.twig', array(
48
            'error' => $app['security.last_error']($request),
49 3
            'form' => $form->createView(),
50
        ));
51 3
    }
52
53 1
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
introduced by
Missing function doc comment
Loading history...
54
    {
55
        // install.phpのチェック.
56
        if (isset($app['config']['eccube_install']) && $app['config']['eccube_install'] == 1) {
57
            $file = $app['config']['root_dir'] . '/html/install.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
58
            if (file_exists($file)) {
59
                $app->addWarning('admin.install.warning', 'admin');
60
            }
61
        }
62
63
        // 受注マスター検索用フォーム
64
        $searchOrderForm = $app['form.factory']
65
            ->createBuilder('admin_search_order')
66
            ->getForm();
67
        // 商品マスター検索用フォーム
68
        $searchProductForm = $app['form.factory']
69
            ->createBuilder('admin_search_product')
70
            ->getForm();
71
        // 会員マスター検索用フォーム
72
        $searchCustomerForm = $app['form.factory']
73
            ->createBuilder('admin_search_customer')
74
            ->getForm();
75
76
        /**
77
         * 受注状況.
78
         */
79 1
        $excludes = array();
80
        $excludes[] = $app['config']['order_pending'];
81
        $excludes[] = $app['config']['order_processing'];
82
        $excludes[] = $app['config']['order_cancel'];
83
        $excludes[] = $app['config']['order_deliv'];
84
85
        // 受注ステータスごとの受注件数.
86
        $Orders = $this->getOrderEachStatus($app['orm.em'], $excludes);
87
        // 受注ステータスの一覧.
88
        $OrderStatuses = $this->findOrderStatus($app['orm.em'], $excludes);
89
90
        /**
91
         * 売り上げ状況
92
         */
93 1
        $excludes = array();
94
        $excludes[] = $app['config']['order_processing'];
95
        $excludes[] = $app['config']['order_cancel'];
96
        $excludes[] = $app['config']['order_pending'];
97
98
        // 今日の売上/件数
99
        $salesToday = $this->getSalesByDay($app['orm.em'], new \DateTime(), $excludes);
100
        // 昨日の売上/件数
101
        $salesYesterday = $this->getSalesByDay($app['orm.em'], new \DateTime('-1 day'), $excludes);
102
        // 今月の売上/件数
103
        $salesThisMonth = $this->getSalesByMonth($app['orm.em'], new \DateTime(), $excludes);
104
105
        /**
106
         * ショップ状況
107
         */
108
        // 在庫切れ商品数
109
        $countNonStockProducts = $this->countNonStockProducts($app['orm.em']);
110
        // 本会員数
111
        $countCustomers = $this->countCustomers($app['orm.em']);
112
113 1
        return $app->render('index.twig', array(
114 1
            'searchOrderForm' => $searchOrderForm->createView(),
115 1
            'searchProductForm' => $searchProductForm->createView(),
116 1
            'searchCustomerForm' => $searchCustomerForm->createView(),
117
            'Orders' => $Orders,
118
            'OrderStatuses' => $OrderStatuses,
119
            'salesThisMonth' => $salesThisMonth,
120
            'salesToday' => $salesToday,
121
            'salesYesterday' => $salesYesterday,
122
            'countNonStockProducts' => $countNonStockProducts,
123
            'countCustomers' => $countCustomers,
124
        ));
125 1
    }
126
127
    /**
128
     * 在庫なし商品の検索結果を表示する.
129
     * 
130
     * @param Application $app
131
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
132
     * @return \Symfony\Component\HttpFoundation\Response
133
     */
134 1
    public function searchNonStockProducts(Application $app, Request $request)
135
    {
136
        // 商品マスター検索用フォーム
137
        $form = $app['form.factory']
138
            ->createBuilder('admin_search_product')
139
            ->getForm();
140
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
141
        if ('POST' === $request->getMethod()) {
142
            $form->handleRequest($request);
143
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
144
            if ($form->isValid()) {
145
                // 在庫なし商品の検索条件をセッションに付与し, 商品マスタへリダイレクトする.
146
                $searchData = array();
147
                $searchData['stock_status'] = Constant::DISABLED;    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
148
                $session = $request->getSession();
149
                $session->set('eccube.admin.product.search', $searchData);
150
151
                return $app->redirect($app->url('admin_product_page', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
152
                    'page_no' => 1,
153
                    'status' => $app['config']['admin_product_stock_status'])));
154
            }
155
        }
156
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
157
        return $app->redirect($app->url('admin_homepage'));
158 1
    }
159
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
160 1
    protected function findOrderStatus($em, array $excludes)
161
    {
162
        $qb = $em
163 1
            ->getRepository('Eccube\Entity\Master\OrderStatus')
164
            ->createQueryBuilder('os');
165
166
        return $qb
167
            ->where($qb->expr()->notIn('os.id', $excludes))
168 1
            ->getQuery()
169
            ->getResult();
170 1
    }
171
172 1
    protected function getOrderEachStatus($em, array $excludes)
173
    {
174
        $sql = 'SELECT
175
                    t1.status as status,
176
                    COUNT(t1.order_id) as count
177
                FROM
178
                    dtb_order t1
179
                WHERE
180
                    t1.del_flg = 0
181
                    AND t1.status NOT IN (:excludes)
182
                GROUP BY
183
                    t1.status
184
                ORDER BY
185 1
                    t1.status';
186
        $rsm = new ResultSetMapping();;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
187
        $rsm->addScalarResult('status', 'status');
188
        $rsm->addScalarResult('count', 'count');
189
        $query = $em->createNativeQuery($sql, $rsm);
190
        $query->setParameters(array(':excludes' => $excludes));
191
        $result = $query->getResult();
192 1
        $orderArray = array();
193
        foreach ($result as $row) {
194
            $orderArray[$row['status']] = $row['count'];
195 1
        }
196
197 1
        return $orderArray;
198 1
    }
199
200 1 View Code Duplication
    protected function getSalesByMonth($em, $dateTime, array $excludes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202
        // concat... for pgsql
203
        // http://stackoverflow.com/questions/1091924/substr-does-not-work-with-datatype-timestamp-in-postgres-8-3
204
        $dql = 'SELECT
205
                  SUBSTRING(CONCAT(o.order_date, \'\'), 1, 7) AS order_month,
206
                  SUM(o.payment_total) AS order_amount,
207
                  COUNT(o) AS order_count
208
                FROM
209
                  Eccube\Entity\Order o
210
                WHERE
211
                    o.del_flg = 0
212
                    AND o.OrderStatus NOT IN (:excludes)
213
                    AND SUBSTRING(CONCAT(o.order_date, \'\'), 1, 7) = SUBSTRING(:targetDate, 1, 7)
214
                GROUP BY
215 1
                  order_month';
216
217
        $q = $em
218 1
            ->createQuery($dql)
219 1
            ->setParameter(':excludes', $excludes)
220
            ->setParameter(':targetDate', $dateTime);
221
222 1
        $result = array();
223
        try {
224 1
            $result = $q->getSingleResult();
225
        } catch (NoResultException $e) {
226
            // 結果がない場合は空の配列を返す.
227 1
        }
228 1
        return $result;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
229 1
    }
230
231 View Code Duplication
    protected function getSalesByDay($em, $dateTime, array $excludes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233
        // concat... for pgsql
234
        // http://stackoverflow.com/questions/1091924/substr-does-not-work-with-datatype-timestamp-in-postgres-8-3
235
        $dql = 'SELECT
236
                  SUBSTRING(CONCAT(o.order_date, \'\'), 1, 10) AS order_day,
237
                  SUM(o.payment_total) AS order_amount,
238
                  COUNT(o) AS order_count
239
                FROM
240
                  Eccube\Entity\Order o
241
                WHERE
242
                    o.del_flg = 0
243
                    AND o.OrderStatus NOT IN (:excludes)
244
                    AND SUBSTRING(CONCAT(o.order_date, \'\'), 1, 10) = SUBSTRING(:targetDate, 1, 10)
245
                GROUP BY
246
                  order_day';
247
248
        $q = $em
249
            ->createQuery($dql)
250
            ->setParameter(':excludes', $excludes)
251
            ->setParameter(':targetDate', $dateTime);
252
253
        $result = array();
254
        try {
255
            $result = $q->getSingleResult();
256
        } catch (NoResultException $e) {
257
            // 結果がない場合は空の配列を返す.
258
        }
259
        return $result;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
260
    }
261
262 1
    protected function countNonStockProducts($em)
263
    {
264
        /** @var $qb \Doctrine\ORM\QueryBuilder */
265 1
        $qb = $em->getRepository('Eccube\Entity\Product')
266 1
            ->createQueryBuilder('p')
267 1
            ->select('count(p.id)')
268 1
            ->innerJoin('p.ProductClasses', 'pc')
269 1
            ->where('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
270
            ->setParameter('StockUnlimited', Constant::DISABLED);
271
272
        return $qb
273 1
            ->getQuery()
274
            ->getSingleScalarResult();
275
    }
276
277 1
    protected function countCustomers($em)
278
    {
279
        $Status = $em
280 1
            ->getRepository('Eccube\Entity\Master\CustomerStatus')
281
            ->find(2);
282
283
        /** @var $qb \Doctrine\ORM\QueryBuilder */
284 1
        $qb = $em->getRepository('Eccube\Entity\Customer')
285 1
            ->createQueryBuilder('c')
286 1
            ->select('count(c.id)')
287 1
            ->where('c.Status = :Status')
288
            ->setParameter('Status', $Status);
289
290
        return $qb
291 1
            ->getQuery()
292
            ->getSingleScalarResult();
293
    }
294
}