Completed
Pull Request — experimental/3.1 (#2154)
by Kentaro
448:33 queued 441:10
created

ProductRepository::get()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 12
nc 3
nop 1
crap 2
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 81
    public function setApplication(Application $app)
51
    {
52 81
        $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 7
    public function get($productId)
64
    {
65
        // Product
66
        try {
67 7
            $qb = $this->createQueryBuilder('p')
68 7
                ->andWhere('p.id = :id');
69
70
            $product = $qb
71 7
                ->getQuery()
72 7
                ->setParameters(array(
73 7
                    'id' => $productId,
74
                ))
75 7
                ->getSingleResult();
76 1
        } catch (NoResultException $e) {
77 1
            throw new NotFoundHttpException();
78
        }
79
80 6
        return $product;
81
    }
82
83
    /**
84
     * get query builder.
85
     *
86
     * @param  array $searchData
87
     * @return \Doctrine\ORM\QueryBuilder
88
     */
89 14
    public function getQueryBuilderBySearchData($searchData)
90
    {
91 14
        $qb = $this->createQueryBuilder('p')
92 14
            ->andWhere('p.Status = 1');
93
94
        // category
95 14
        $categoryJoin = false;
96 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...
97 2
            $Categories = $searchData['category_id']->getSelfAndDescendants();
98 2
            if ($Categories) {
99
                $qb
100 2
                    ->innerJoin('p.ProductCategories', 'pct')
101 2
                    ->innerJoin('pct.Category', 'c')
102 2
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
103 2
                    ->setParameter('Categories', $Categories);
104 2
                $categoryJoin = true;
105
            }
106
        }
107
108
        // name
109 14
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
110 1
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
111
112 1
            foreach ($keywords as $index => $keyword) {
113 1
                $key = sprintf('keyword%s', $index);
114
                $qb
115 1
                    ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR NORMALIZE(p.search_word) LIKE NORMALIZE(:%s)', $key, $key))
116 1
                    ->setParameter($key, '%' . $keyword . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
117
            }
118
        }
119
120
        // Order By
121
        // 価格低い順
122 14
        $config = $this->app['config'];
123 14
        if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_lower']) {
124
            //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
125 4
            $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
126 4
            $qb->innerJoin('p.ProductClasses', 'pc');
127 4
            $qb->groupBy('p');
128
            // 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...
129
            // 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...
130
            // @see https://github.com/EC-CUBE/ec-cube/issues/1904
131
            // $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...
132 4
            $qb->orderBy('price02_min', 'ASC');
133 4
            $qb->addOrderBy('p.id', 'DESC');
134
            // 価格高い順
135 10
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_higher']) {
136
            $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
137
            $qb->innerJoin('p.ProductClasses', 'pc');
138
            $qb->groupBy('p');
139
            $qb->orderBy('price02_max', 'DESC');
140
            $qb->addOrderBy('p.id', 'DESC');
141
            // 新着順
142 10
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_newer']) {
143 2
            $qb->orderBy('p.create_date', 'DESC');
144
        } else {
145 8
            if ($categoryJoin === false) {
146
                $qb
147 6
                    ->leftJoin('p.ProductCategories', 'pct')
148 6
                    ->leftJoin('pct.Category', 'c');
149
            }
150
            $qb
151 8
                ->addOrderBy('p.id', 'DESC');
152
        }
153
154 14
        return $qb;
155
    }
156
157
    /**
158
     * get query builder.
159
     *
160
     * @param  array $searchData
161
     * @return \Doctrine\ORM\QueryBuilder
162
     */
163 17
    public function getQueryBuilderBySearchDataForAdmin($searchData)
164
    {
165 17
        $qb = $this->createQueryBuilder('p')
166 17
            ->innerJoin('p.ProductClasses', 'pc');
167
168
        // id
169 17 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...
170 6
            $id = preg_match('/^\d+$/', $searchData['id']) ? $searchData['id'] : null;
171
            $qb
172 6
                ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
173 6
                ->setParameter('id', $id)
174 6
                ->setParameter('likeid', '%' . $searchData['id'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
175
        }
176
177
        // code
178
        /*
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...
179
        if (!empty($searchData['code']) && $searchData['code']) {
180
            $qb
181
                ->innerJoin('p.ProductClasses', 'pc')
182
                ->andWhere('pc.code LIKE :code')
183
                ->setParameter('code', '%' . $searchData['code'] . '%');
184
        }
185
186
        // name
187
        if (!empty($searchData['name']) && $searchData['name']) {
188
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
189
            foreach ($keywords as $keyword) {
190
                $qb
191
                    ->andWhere('p.name LIKE :name')
192
                    ->setParameter('name', '%' . $keyword . '%');
193
            }
194
        }
195
       */
196
197
        // category
198 17 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...
199 2
            $Categories = $searchData['category_id']->getSelfAndDescendants();
200 2
            if ($Categories) {
201
                $qb
202 2
                    ->innerJoin('p.ProductCategories', 'pct')
203 2
                    ->innerJoin('pct.Category', 'c')
204 2
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
205 2
                    ->setParameter('Categories', $Categories);
206
            }
207
        }
208
209
        // status
210 17
        if (!empty($searchData['status']) && $searchData['status']->toArray()) {
211
            $qb
212 1
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
213 1
                ->setParameter('Status', $searchData['status']->toArray());
214
        }
215
216
        // link_status
217 17
        if (isset($searchData['link_status'])) {
218
            $qb
219 1
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
220 1
                ->setParameter('Status', $searchData['link_status']);
221
        }
222
223
        // stock status
224 17
        if (isset($searchData['stock_status'])) {
225
            $qb
226 1
                ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
227 1
                ->setParameter('StockUnlimited', $searchData['stock_status']);
228
        }
229
230
        // crate_date
231 17 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...
232 1
            $date = $searchData['create_date_start']
233 1
                ->format('Y-m-d H:i:s');
234
            $qb
235 1
                ->andWhere('p.create_date >= :create_date_start')
236 1
                ->setParameter('create_date_start', $date);
237
        }
238
239 17
        if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
240 1
            $date = clone $searchData['create_date_end'];
241
            $date = $date
242 1
                ->modify('+1 days')
243 1
                ->format('Y-m-d H:i:s');
244
            $qb
245 1
                ->andWhere('p.create_date < :create_date_end')
246 1
                ->setParameter('create_date_end', $date);
247
        }
248
249
        // update_date
250 17 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...
251 1
            $date = $searchData['update_date_start']
252 1
                ->format('Y-m-d H:i:s');
253
            $qb
254 1
                ->andWhere('p.update_date >= :update_date_start')
255 1
                ->setParameter('update_date_start', $date);
256
        }
257 17
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
258 1
            $date = clone $searchData['update_date_end'];
259
            $date = $date
260 1
                ->modify('+1 days')
261 1
                ->format('Y-m-d H:i:s');
262
            $qb
263 1
                ->andWhere('p.update_date < :update_date_end')
264 1
                ->setParameter('update_date_end', $date);
265
        }
266
267
268
        // Order By
269
        $qb
270 17
            ->orderBy('p.update_date', 'DESC');
271
272 17
        return $qb;
273
    }
274
275
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
276
     * get query builder.
277
     *
278
     * @param $Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
279
     * @return \Doctrine\ORM\QueryBuilder
280
     * @see CustomerFavoriteProductRepository::getQueryBuilderByCustomer()
281
     * @deprecated since 3.0.0, to be removed in 3.1
282
     */
283
    public function getFavoriteProductQueryBuilderByCustomer($Customer)
284
    {
285
        $qb = $this->createQueryBuilder('p')
286
            ->innerJoin('p.CustomerFavoriteProducts', 'cfp')
287
            ->where('cfp.Customer = :Customer AND p.Status = 1')
288
            ->setParameter('Customer', $Customer);
289
290
        // Order By
291
        // XXX Paginater を使用した場合に PostgreSQL で正しくソートできない
292
        $qb->addOrderBy('cfp.create_date', 'DESC');
293
294
        return $qb;
295
    }
296
}
297