getQueryBuilderBySearchDataForAdmin()   F
last analyzed

Complexity

Conditions 19
Paths 1152

Size

Total Lines 111
Code Lines 60

Duplication

Lines 31
Ratio 27.93 %

Code Coverage

Tests 48
CRAP Score 19

Importance

Changes 0
Metric Value
cc 19
eloc 60
nc 1152
nop 1
dl 31
loc 111
rs 2
c 0
b 0
f 0
ccs 48
cts 48
cp 1
crap 19

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
 * 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\Repository;
26
27
use Doctrine\ORM\EntityRepository;
28
use Doctrine\ORM\NoResultException;
29
use Eccube\Application;
30
use Eccube\Util\Str;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
/**
34
 * ProductRepository
35
 *
36
 * This class was generated by the Doctrine ORM. Add your own custom
37
 * repository methods below.
38
 */
39
class ProductRepository extends EntityRepository
40
{
41
42
    /**
43
     * @var \Eccube\Application
44
     */
45
    protected $app;
46
47
    /**
48
     * @param Application $app
49
     */
50 106
    public function setApplication(Application $app)
51
    {
52 106
        $this->app = $app;
53
    }
54
55
    /**
56
     * get Product.
57
     *
58
     * @param  integer $productId
59
     * @return \Eccube\Entity\Product
60
     *
61
     * @throws NotFoundHttpException
62
     */
63 20
    public function get($productId)
64
    {
65
        // Product
66
        try {
67 20
            $qb = $this->createQueryBuilder('p');
68 20
            $qb->addSelect(array('pc', 'cc1', 'cc2', 'pi', 'ps'))
69 20
                ->innerJoin('p.ProductClasses', 'pc')
70 20
                ->leftJoin('pc.ClassCategory1', 'cc1')
71 20
                ->leftJoin('pc.ClassCategory2', 'cc2')
72 20
                ->leftJoin('p.ProductImage', 'pi')
73 20
                ->innerJoin('pc.ProductStock', 'ps')
74 20
                ->where('p.id = :id')
75 20
                ->orderBy('cc1.rank', 'DESC')
76 20
                ->addOrderBy('cc2.rank', 'DESC');
77
78
            $product = $qb
79 20
                ->getQuery()
80 20
                ->setParameters(array(
81 20
                    'id' => $productId,
82
                ))
83 20
                ->getSingleResult();
84 2
        } catch (NoResultException $e) {
85 2
            throw new NotFoundHttpException();
86
        }
87
88 19
        return $product;
89
    }
90
91
    /**
92
     * get query builder.
93
     *
94
     * @param  array $searchData
95
     * @return \Doctrine\ORM\QueryBuilder
96
     */
97 18
    public function getQueryBuilderBySearchData($searchData)
98
    {
99 18
        $qb = $this->createQueryBuilder('p')
100 18
            ->andWhere('p.Status = 1');
101
102
        // category
103 18
        $categoryJoin = false;
104 18 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105 3
            $Categories = $searchData['category_id']->getSelfAndDescendants();
106 3
            if ($Categories) {
107
                $qb
108 3
                    ->innerJoin('p.ProductCategories', 'pct')
109 3
                    ->innerJoin('pct.Category', 'c')
110 3
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
111 3
                    ->setParameter('Categories', $Categories);
112 3
                $categoryJoin = true;
113
            }
114
        }
115
116
        // name
117 18
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
118 2
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
119
120 2
            foreach ($keywords as $index => $keyword) {
121 2
                $key = sprintf('keyword%s', $index);
122
                $qb
123 2
                    ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR NORMALIZE(p.search_word) LIKE NORMALIZE(:%s)', $key, $key))
124 2
                    ->setParameter($key, '%' . $keyword . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
125
            }
126
        }
127
128
        // Order By
129
        // 価格低い順
130 18
        $config = $this->app['config'];
131 18
        if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_lower']) {
132
            //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
133 6
            $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
134 6
            $qb->innerJoin('p.ProductClasses', 'pc');
135 6
            $qb->groupBy('p');
136
            // postgres9.0以下は, groupBy('p.id')が利用できない
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
137
            // mysqlおよびpostgresql9.1以上であればgroupBy('p.id')にすることで性能向上が期待できる.
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
138
            // @see https://github.com/EC-CUBE/ec-cube/issues/1904
139
            // $qb->groupBy('p.id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
140 6
            $qb->orderBy('price02_min', 'ASC');
141 6
            $qb->addOrderBy('p.id', 'DESC');
142
            // 価格高い順
143 12
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_higher']) {
144 1
            $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
145 1
            $qb->innerJoin('p.ProductClasses', 'pc');
146 1
            $qb->groupBy('p');
147 1
            $qb->orderBy('price02_max', 'DESC');
148 1
            $qb->addOrderBy('p.id', 'DESC');
149
            // 新着順
150 11
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_newer']) {
151
            // 在庫切れ商品非表示の設定が有効時対応
152
            // @see https://github.com/EC-CUBE/ec-cube/issues/1998
153 4
            if ($this->app['orm.em']->getFilters()->isEnabled('nostock_hidden') == true) {
154
                $qb->innerJoin('p.ProductClasses', 'pc');
155
            }
156 4
            $qb->orderBy('p.create_date', 'DESC');
157 4
            $qb->addOrderBy('p.id', 'DESC');
158
        } else {
159 7
            if ($categoryJoin === false) {
160
                $qb
161 5
                    ->leftJoin('p.ProductCategories', 'pct')
162 5
                    ->leftJoin('pct.Category', 'c');
163
            }
164
            $qb
165 7
                ->addOrderBy('p.id', 'DESC');
166
        }
167
168 18
        return $qb;
169
    }
170
171
    /**
172
     * get query builder.
173
     *
174
     * @param  array $searchData
175
     * @return \Doctrine\ORM\QueryBuilder
176
     */
177 23
    public function getQueryBuilderBySearchDataForAdmin($searchData)
178
    {
179 23
        $qb = $this->createQueryBuilder('p')
180 23
            ->innerJoin('p.ProductClasses', 'pc');
181
182
        // id
183 23 View Code Duplication
        if (isset($searchData['id']) && Str::isNotBlank($searchData['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
184 10
            $id = preg_match('/^\d+$/', $searchData['id']) ? $searchData['id'] : null;
185
            $qb
186 10
                ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
187 10
                ->setParameter('id', $id)
188 10
                ->setParameter('likeid', '%' . $searchData['id'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
189
        }
190
191
        // code
192
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
193
        if (!empty($searchData['code']) && $searchData['code']) {
194
            $qb
195
                ->innerJoin('p.ProductClasses', 'pc')
196
                ->andWhere('pc.code LIKE :code')
197
                ->setParameter('code', '%' . $searchData['code'] . '%');
198
        }
199
200
        // name
201
        if (!empty($searchData['name']) && $searchData['name']) {
202
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
203
            foreach ($keywords as $keyword) {
204
                $qb
205
                    ->andWhere('p.name LIKE :name')
206
                    ->setParameter('name', '%' . $keyword . '%');
207
            }
208
        }
209
       */
210
211
        // category
212 23 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
213 2
            $Categories = $searchData['category_id']->getSelfAndDescendants();
214 2
            if ($Categories) {
215
                $qb
216 2
                    ->innerJoin('p.ProductCategories', 'pct')
217 2
                    ->innerJoin('pct.Category', 'c')
218 2
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
219 2
                    ->setParameter('Categories', $Categories);
220
            }
221
        }
222
223
        // status
224 23
        if (!empty($searchData['status']) && $searchData['status']->toArray()) {
225
            $qb
226 1
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
227 1
                ->setParameter('Status', $searchData['status']->toArray());
228
        }
229
230
        // link_status
231 23
        if (isset($searchData['link_status'])) {
232
            $qb
233 4
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
234 4
                ->setParameter('Status', $searchData['link_status']);
235
        }
236
237
        // stock status
238 23
        if (isset($searchData['stock_status'])) {
239
            $qb
240 2
                ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
241 2
                ->setParameter('StockUnlimited', $searchData['stock_status']);
242
        }
243
244
        // crate_date
245 23 View Code Duplication
        if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
246 1
            $date = $searchData['create_date_start']
247 1
                ->format('Y-m-d H:i:s');
248
            $qb
249 1
                ->andWhere('p.create_date >= :create_date_start')
250 1
                ->setParameter('create_date_start', $date);
251
        }
252
253 23
        if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
254 1
            $date = clone $searchData['create_date_end'];
255
            $date = $date
256 1
                ->modify('+1 days')
257 1
                ->format('Y-m-d H:i:s');
258
            $qb
259 1
                ->andWhere('p.create_date < :create_date_end')
260 1
                ->setParameter('create_date_end', $date);
261
        }
262
263
        // update_date
264 23 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
265 1
            $date = $searchData['update_date_start']
266 1
                ->format('Y-m-d H:i:s');
267
            $qb
268 1
                ->andWhere('p.update_date >= :update_date_start')
269 1
                ->setParameter('update_date_start', $date);
270
        }
271 23
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
272 1
            $date = clone $searchData['update_date_end'];
273
            $date = $date
274 1
                ->modify('+1 days')
275 1
                ->format('Y-m-d H:i:s');
276
            $qb
277 1
                ->andWhere('p.update_date < :update_date_end')
278 1
                ->setParameter('update_date_end', $date);
279
        }
280
281
282
        // Order By
283
        $qb
284 23
            ->orderBy('p.update_date', 'DESC');
285
286 23
        return $qb;
287
    }
288
289
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
290
     * get query builder.
291
     *
292
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
293
     * @return \Doctrine\ORM\QueryBuilder
294
     * @see CustomerFavoriteProductRepository::getQueryBuilderByCustomer()
295
     * @deprecated since 3.0.0, to be removed in 3.1
296
     */
297
    public function getFavoriteProductQueryBuilderByCustomer($Customer)
298
    {
299
        $qb = $this->createQueryBuilder('p')
300
            ->innerJoin('p.CustomerFavoriteProducts', 'cfp')
301
            ->where('cfp.Customer = :Customer AND p.Status = 1')
302
            ->setParameter('Customer', $Customer);
303
304
        // Order By
305
        // XXX Paginater を使用した場合に PostgreSQL で正しくソートできない
306
        $qb->addOrderBy('cfp.create_date', 'DESC');
307
308
        return $qb;
309
    }
310
}
311