Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

getQueryBuilderBySearchDataForAdmin()   F

Complexity

Conditions 19
Paths 1152

Size

Total Lines 107
Code Lines 56

Duplication

Lines 31
Ratio 28.97 %

Code Coverage

Tests 44
CRAP Score 19

Importance

Changes 0
Metric Value
cc 19
eloc 56
nc 1152
nop 1
dl 31
loc 107
ccs 44
cts 44
cp 1
crap 19
rs 2
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
 * 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\EntityManager;
28
use Doctrine\ORM\NoResultException;
29
use Eccube\Annotation\Inject;
30
use Eccube\Annotation\Repository;
31
use Eccube\Doctrine\Query\Queries;
32
use Eccube\Util\Str;
33
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
35
/**
36
 * ProductRepository
37
 *
38
 * This class was generated by the Doctrine ORM. Add your own custom
39
 * repository methods below.
40
 *
41
 * @Repository
42
 */
43
class ProductRepository extends AbstractRepository
44
{
45
    /**
46
     * @Inject("eccube.queries")
47
     * @var Queries
48
     */
49
    protected $queries;
50
51
    /**
52
     * @Inject("orm.em")
53
     * @var EntityManager
54
     */
55
    protected $entityManager;
56
57
    /**
58
     * @Inject("config")
59
     * @var array
60
     */
61
    protected $appConfig;
62
63
   /**
64
     * get query builder.
65
     *
66
     * @param  array $searchData
67
     * @return \Doctrine\ORM\QueryBuilder
68
     */
69 15
    public function getQueryBuilderBySearchData($searchData)
70
    {
71 15
        $qb = $this->createQueryBuilder('p')
72 15
            ->andWhere('p.Status = 1');
73
74
        // category
75 15
        $categoryJoin = false;
76 15 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
77 3
            $Categories = $searchData['category_id']->getSelfAndDescendants();
78 3
            if ($Categories) {
79
                $qb
80 3
                    ->innerJoin('p.ProductCategories', 'pct')
81 3
                    ->innerJoin('pct.Category', 'c')
82 3
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
83 3
                    ->setParameter('Categories', $Categories);
84 3
                $categoryJoin = true;
85
            }
86
        }
87
88
        // name
89 15
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
90 1
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
91
92 1
            foreach ($keywords as $index => $keyword) {
93 1
                $key = sprintf('keyword%s', $index);
94
                $qb
95 1
                    ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR NORMALIZE(p.search_word) LIKE NORMALIZE(:%s)', $key, $key))
96 1
                    ->setParameter($key, '%' . $keyword . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
97
            }
98
        }
99
100
        // Order By
101
        // 価格低い順
102 15
        $config = $this->appConfig;
103 15
        if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_lower']) {
104
            //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
105 5
            $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
106 5
            $qb->innerJoin('p.ProductClasses', 'pc');
107 5
            $qb->groupBy('p');
108
            // postgres9.0以下は, groupBy('p.id')が利用できない
109
            // mysqlおよびpostgresql9.1以上であればgroupBy('p.id')にすることで性能向上が期待できる.
110
            // @see https://github.com/EC-CUBE/ec-cube/issues/1904
111
            // $qb->groupBy('p.id');
112 5
            $qb->orderBy('price02_min', 'ASC');
113 5
            $qb->addOrderBy('p.id', 'DESC');
114
            // 価格高い順
115 10
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_higher']) {
116 1
            $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
117 1
            $qb->innerJoin('p.ProductClasses', 'pc');
118 1
            $qb->groupBy('p');
119 1
            $qb->orderBy('price02_max', 'DESC');
120 1
            $qb->addOrderBy('p.id', 'DESC');
121
            // 新着順
122 9
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_newer']) {
123
            // 在庫切れ商品非表示の設定が有効時対応
124
            // @see https://github.com/EC-CUBE/ec-cube/issues/1998
125 2
            if ($this->entityManager->getFilters()->isEnabled('nostock_hidden') == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
126
                $qb->innerJoin('p.ProductClasses', 'pc');
127
            }
128 2
            $qb->orderBy('p.create_date', 'DESC');
129
        } else {
130 7
            if ($categoryJoin === false) {
131
                $qb
132 5
                    ->leftJoin('p.ProductCategories', 'pct')
133 5
                    ->leftJoin('pct.Category', 'c');
134
            }
135
            $qb
136 7
                ->addOrderBy('p.id', 'DESC');
137
        }
138
139 15
        return $this->queries->customize(QueryKey::PRODUCT_SEARCH, $qb, $searchData);
140
    }
141
142
    /**
143
     * get query builder.
144
     *
145
     * @param  array $searchData
146
     * @return \Doctrine\ORM\QueryBuilder
147
     */
148 22
    public function getQueryBuilderBySearchDataForAdmin($searchData)
149
    {
150 22
        $qb = $this->createQueryBuilder('p')
151 22
            ->innerJoin('p.ProductClasses', 'pc');
152
153
        // id
154 22 View Code Duplication
        if (isset($searchData['id']) && Str::isNotBlank($searchData['id'])) {
155 9
            $id = preg_match('/^\d+$/', $searchData['id']) ? $searchData['id'] : null;
156
            $qb
157 9
                ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
158 9
                ->setParameter('id', $id)
159 9
                ->setParameter('likeid', '%' . $searchData['id'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
160
        }
161
162
        // code
163
        /*
164
        if (!empty($searchData['code']) && $searchData['code']) {
165
            $qb
166
                ->innerJoin('p.ProductClasses', 'pc')
167
                ->andWhere('pc.code LIKE :code')
168
                ->setParameter('code', '%' . $searchData['code'] . '%');
169
        }
170
171
        // name
172
        if (!empty($searchData['name']) && $searchData['name']) {
173
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
174
            foreach ($keywords as $keyword) {
175
                $qb
176
                    ->andWhere('p.name LIKE :name')
177
                    ->setParameter('name', '%' . $keyword . '%');
178
            }
179
        }
180
       */
181
182
        // category
183 22 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
184 2
            $Categories = $searchData['category_id']->getSelfAndDescendants();
185 2
            if ($Categories) {
186
                $qb
187 2
                    ->innerJoin('p.ProductCategories', 'pct')
188 2
                    ->innerJoin('pct.Category', 'c')
189 2
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
190 2
                    ->setParameter('Categories', $Categories);
191
            }
192
        }
193
194
        // status
195 22
        if (!empty($searchData['status']) && $searchData['status']) {
196
            $qb
197 1
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
198 1
                ->setParameter('Status', $searchData['status']);
199
        }
200
201
        // link_status
202 22
        if (isset($searchData['link_status'])) {
203
            $qb
204 1
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
205 1
                ->setParameter('Status', $searchData['link_status']);
206
        }
207
208
        // stock status
209 22
        if (isset($searchData['stock_status'])) {
210
            $qb
211 2
                ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
212 2
                ->setParameter('StockUnlimited', $searchData['stock_status']);
213
        }
214
215
        // crate_date
216 22
        if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
217 1
            $date = $searchData['create_date_start'];
218
            $qb
219 1
                ->andWhere('p.create_date >= :create_date_start')
220 1
                ->setParameter('create_date_start', $date);
221
        }
222
223 22
        if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
224 1
            $date = clone $searchData['create_date_end'];
225
            $date = $date
226 1
                ->modify('+1 days');
227
            $qb
228 1
                ->andWhere('p.create_date < :create_date_end')
229 1
                ->setParameter('create_date_end', $date);
230
        }
231
232
        // update_date
233 22 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
234 1
            $date = $searchData['update_date_start'];
235
            $qb
236 1
                ->andWhere('p.update_date >= :update_date_start')
237 1
                ->setParameter('update_date_start', $date);
238
        }
239 22 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
240 1
            $date = clone $searchData['update_date_end'];
241
            $date = $date
242 1
                ->modify('+1 days');
243
            $qb
244 1
                ->andWhere('p.update_date < :update_date_end')
245 1
                ->setParameter('update_date_end', $date);
246
        }
247
248
249
        // Order By
250
        $qb
251 22
            ->orderBy('p.update_date', 'DESC');
252
253 22
        return $this->queries->customize(QueryKey::PRODUCT_SEARCH_ADMIN, $qb, $searchData);
254
    }
255
256
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
257
     * get query builder.
258
     *
259
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
260
     * @return \Doctrine\ORM\QueryBuilder
261
     * @see CustomerFavoriteProductRepository::getQueryBuilderByCustomer()
262
     * @deprecated since 3.0.0, to be removed in 3.1
263
     */
264 1 View Code Duplication
    public function getFavoriteProductQueryBuilderByCustomer($Customer)
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...
265
    {
266 1
        $qb = $this->createQueryBuilder('p')
267 1
            ->innerJoin('p.CustomerFavoriteProducts', 'cfp')
268 1
            ->where('cfp.Customer = :Customer AND p.Status = 1')
269 1
            ->setParameter('Customer', $Customer);
270
271
        // Order By
272
        // XXX Paginater を使用した場合に PostgreSQL で正しくソートできない
273 1
        $qb->addOrderBy('cfp.create_date', 'DESC');
274
275 1
        return $this->queries->customize(QueryKey::PRODUCT_GET_FAVORITE, $qb, ['customer' => $Customer]);
276
    }
277
}
278