Completed
Pull Request — master (#1912)
by chihiro
35:08
created

ProductRepository   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 244
Duplicated Lines 24.59 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 60
loc 244
ccs 95
cts 95
cp 1
rs 8.8
wmc 36
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 19 2
C getQueryBuilderBySearchData() 11 66 14
F getQueryBuilderBySearchDataForAdmin() 49 111 19
A getFavoriteProductQueryBuilderByCustomer() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Eccube\Util\Str;
28
use Doctrine\ORM\EntityRepository;
29
use Doctrine\ORM\NoResultException;
30
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
31
32
/**
33
 * ProductRepository
34
 *
35
 * This class was generated by the Doctrine ORM. Add your own custom
36
 * repository methods below.
37
 */
38
class ProductRepository extends EntityRepository
39
{
40
41
    /**
42
     * get Product.
43
     *
44
     * @param  integer $productId
45
     * @return \Eccube\Entity\Product
46
     *
47
     * @throws NotFoundHttpException
48
     */
49 7
    public function get($productId)
50
    {
51
        // Product
52
        try {
53 7
            $qb = $this->createQueryBuilder('p')
54 7
                ->andWhere('p.id = :id');
55
56
            $product = $qb
57 7
                ->getQuery()
58 7
                ->setParameters(array(
59 7
                    'id' => $productId,
60
                ))
61 7
                ->getSingleResult();
62 1
        } catch (NoResultException $e) {
63 1
            throw new NotFoundHttpException();
64
        }
65
66 6
        return $product;
67
    }
68
69
    /**
70
     * get query builder.
71
     *
72
     * @param  array $searchData
73
     * @return \Doctrine\ORM\QueryBuilder
74
     */
75 14
    public function getQueryBuilderBySearchData($searchData)
76
    {
77 14
        $qb = $this->createQueryBuilder('p')
78 14
            ->andWhere('p.Status = 1');
79
80
        // category
81 14
        $categoryJoin = false;
82 14 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...
83 2
            $Categories = $searchData['category_id']->getSelfAndDescendants();
84 2
            if ($Categories) {
85
                $qb
86 2
                    ->innerJoin('p.ProductCategories', 'pct')
87 2
                    ->innerJoin('pct.Category', 'c')
88 2
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
89 2
                    ->setParameter('Categories', $Categories);
90 2
                $categoryJoin = true;
91
            }
92
        }
93
94
        // name
95 14
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
96 4
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
97
98 4
            foreach ($keywords as $index => $keyword) {
99 4
                $key = sprintf('keyword%s', $index);
100
                $qb
101 4
                    ->andWhere(sprintf('p.name LIKE :%s OR p.search_word LIKE :%s', $key, $key))
102 4
                    ->setParameter($key, '%' . $keyword . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
103
            }
104
        }
105
106
        // Order By
107
        // 価格低い順
108 14
        if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == '1') {
109
            //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
110 4
            $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
111 4
            $qb->innerJoin('p.ProductClasses', 'pc');
112 4
            $qb->groupBy('p');
113
            // 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...
114
            // 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...
115
            // @see https://github.com/EC-CUBE/ec-cube/issues/1904
116
            // $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...
117 4
            $qb->orderBy('price02_min', 'ASC');
118 4
            $qb->addOrderBy('p.id', 'DESC');
119
            // 価格高い順
120 10
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == '3') {
121 1
            $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
122 1
            $qb->innerJoin('p.ProductClasses', 'pc');
123 1
            $qb->groupBy('p');
124 1
            $qb->orderBy('price02_min', 'DESC');
125 1
            $qb->addOrderBy('p.id', 'ASC');
126
            // 新着順
127 9
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == '2') {
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 14
        return $qb;
140
    }
141
142
    /**
143
     * get query builder.
144
     *
145
     * @param  array $searchData
146
     * @return \Doctrine\ORM\QueryBuilder
147
     */
148 13
    public function getQueryBuilderBySearchDataForAdmin($searchData)
149
    {
150 13
        $qb = $this->createQueryBuilder('p')
151 13
            ->innerJoin('p.ProductClasses', 'pc');
152
153
        // id
154 13 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...
155 3
            $id = preg_match('/^\d+$/', $searchData['id']) ? $searchData['id'] : null;
156
            $qb
157 3
                ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
158 3
                ->setParameter('id', $id)
159 3
                ->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
        /*
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...
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 13 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...
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 13
        if (!empty($searchData['status']) && $searchData['status']->toArray()) {
196
            $qb
197 1
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
198 1
                ->setParameter('Status', $searchData['status']->toArray());
199
        }
200
201
        // link_status
202 13
        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 13
        if (isset($searchData['stock_status'])) {
210
            $qb
211 1
                ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
212 1
                ->setParameter('StockUnlimited', $searchData['stock_status']);
213
        }
214
215
        // crate_date
216 13 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...
217 1
            $date = $searchData['create_date_start']
218 1
                ->format('Y-m-d H:i:s');
219
            $qb
220 1
                ->andWhere('p.create_date >= :create_date_start')
221 1
                ->setParameter('create_date_start', $date);
222
        }
223
224 13 View Code Duplication
        if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
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...
225 1
            $date = clone $searchData['create_date_end'];
226
            $date = $date
227 1
                ->modify('+1 days')
228 1
                ->format('Y-m-d H:i:s');
229
            $qb
230 1
                ->andWhere('p.create_date < :create_date_end')
231 1
                ->setParameter('create_date_end', $date);
232
        }
233
234
        // update_date
235 13 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...
236 1
            $date = $searchData['update_date_start']
237 1
                ->format('Y-m-d H:i:s');
238
            $qb
239 1
                ->andWhere('p.update_date >= :update_date_start')
240 1
                ->setParameter('update_date_start', $date);
241
        }
242 13 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
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...
243 1
            $date = clone $searchData['update_date_end'];
244
            $date = $date
245 1
                ->modify('+1 days')
246 1
                ->format('Y-m-d H:i:s');
247
            $qb
248 1
                ->andWhere('p.update_date < :update_date_end')
249 1
                ->setParameter('update_date_end', $date);
250
        }
251
252
253
        // Order By
254
        $qb
255 13
            ->orderBy('p.update_date', 'DESC');
256
257 13
        return $qb;
258
    }
259
260
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
261
     * get query builder.
262
     *
263
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
264
     * @return \Doctrine\ORM\QueryBuilder
265
     * @see CustomerFavoriteProductRepository::getQueryBuilderByCustomer()
266
     * @deprecated since 3.0.0, to be removed in 3.1
267
     */
268
    public function getFavoriteProductQueryBuilderByCustomer($Customer)
269
    {
270
        $qb = $this->createQueryBuilder('p')
271
            ->innerJoin('p.CustomerFavoriteProducts', 'cfp')
272
            ->where('cfp.Customer = :Customer AND p.Status = 1')
273
            ->setParameter('Customer', $Customer);
274
275
        // Order By
276
        // XXX Paginater を使用した場合に PostgreSQL で正しくソートできない
277
        $qb->addOrderBy('cfp.create_date', 'DESC');
278
279
        return $qb;
280
    }
281
}
282