ProductRepository::setApplication()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
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 5
     */
50
    public function setApplication(Application $app)
51
    {
52
        $this->app = $app;
53 5
    }
54
55
    /**
56
     * get Product.
57 5
     *
58
     * @param  integer $productId
59
     * @return \Eccube\Entity\Product
60 5
     *
61 1
     * @throws NotFoundHttpException
62
     */
63
    public function get($productId)
64 5
    {
65
        // Product
66 4
        try {
67
            $qb = $this->createQueryBuilder('p');
68
            $qb->addSelect(array('pc', 'cc1', 'cc2', 'pi', 'ps'))
69
                ->innerJoin('p.ProductClasses', 'pc')
70
                ->leftJoin('pc.ClassCategory1', 'cc1')
71
                ->leftJoin('pc.ClassCategory2', 'cc2')
72
                ->leftJoin('p.ProductImage', 'pi')
73
                ->innerJoin('pc.ProductStock', 'ps')
74
                ->where('p.id = :id')
75 9
                ->orderBy('cc1.rank', 'DESC')
76
                ->addOrderBy('cc2.rank', 'DESC');
77 9
78
            $product = $qb
79
                ->getQuery()
80
                ->setParameters(array(
81 9
                    'id' => $productId,
82 9
                ))
83
                ->getSingleResult();
84 2
        } catch (NoResultException $e) {
85
            throw new NotFoundHttpException();
86 2
        }
87 2
88
        return $product;
89
    }
90 2
91
    /**
92
     * get query builder.
93
     *
94
     * @param  array $searchData
95 8
     * @return \Doctrine\ORM\QueryBuilder
96
     */
97
    public function getQueryBuilderBySearchData($searchData)
98
    {
99
        $qb = $this->createQueryBuilder('p')
100
            ->andWhere('p.Status = 1');
101
102
        // category
103
        $categoryJoin = false;
104 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
105
            $Categories = $searchData['category_id']->getSelfAndDescendants();
106
            if ($Categories) {
107
                $qb
108 4
                    ->innerJoin('p.ProductCategories', 'pct')
109
                    ->innerJoin('pct.Category', 'c')
110
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
111
                    ->setParameter('Categories', $Categories);
112
                $categoryJoin = true;
113
            }
114
        }
115 4
116
        // name
117
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
118 4
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
119
120 2
            foreach ($keywords as $index => $keyword) {
121
                $key = sprintf('keyword%s', $index);
122
                $qb
123
                    ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR NORMALIZE(p.search_word) LIKE NORMALIZE(:%s)', $key, $key))
124
                    ->setParameter($key, '%' . $keyword . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
125 5
            }
126
        }
127 9
128
        // Order By
129
        // 価格低い順
130
        $config = $this->app['config'];
131
        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
            $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
134
            $qb->innerJoin('p.ProductClasses', 'pc');
135
            $qb->groupBy('p');
136 13
            // 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 13
            // @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
            $qb->orderBy('price02_min', 'ASC');
141
            $qb->addOrderBy('p.id', 'DESC');
142 10
            // 価格高い順
143
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_higher']) {
144
            $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
145 3
            $qb->innerJoin('p.ProductClasses', 'pc');
146 3
            $qb->groupBy('p');
147
            $qb->orderBy('price02_max', 'DESC');
148
            $qb->addOrderBy('p.id', 'DESC');
149
            // 新着順
150
        } 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
            if ($this->app['orm.em']->getFilters()->isEnabled('nostock_hidden') == true) {
154
                $qb->innerJoin('p.ProductClasses', 'pc');
155
            }
156
            $qb->orderBy('p.create_date', 'DESC');
157
            $qb->addOrderBy('p.id', 'DESC');
158
        } else {
159
            if ($categoryJoin === false) {
160
                $qb
161
                    ->leftJoin('p.ProductCategories', 'pct')
162
                    ->leftJoin('pct.Category', 'c');
163
            }
164
            $qb
165
                ->addOrderBy('p.id', 'DESC');
166
        }
167
168
        return $qb;
169
    }
170
171 13
    /**
172
     * get query builder.
173 2
     *
174
     * @param  array $searchData
175 2
     * @return \Doctrine\ORM\QueryBuilder
176 2
     */
177
    public function getQueryBuilderBySearchDataForAdmin($searchData)
178
    {
179
        $qb = $this->createQueryBuilder('p')
180
            ->innerJoin('p.ProductClasses', 'pc');
181
182
        // id
183 12 View Code Duplication
        if (isset($searchData['id']) && Str::isNotBlank($searchData['id'])) {
184
            $id = preg_match('/^\d+$/', $searchData['id']) ? $searchData['id'] : null;
185
            $qb
186
                ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
187
                ->setParameter('id', $id)
188
                ->setParameter('likeid', '%' . $searchData['id'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
189
        }
190 13
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 13
                ->setParameter('code', '%' . $searchData['code'] . '%');
198
        }
199 1
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 13
                $qb
205
                    ->andWhere('p.name LIKE :name')
206
                    ->setParameter('name', '%' . $keyword . '%');
207
            }
208 1
        }
209
       */
210
211
        // category
212 13 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
213 1
            $Categories = $searchData['category_id']->getSelfAndDescendants();
214
            if ($Categories) {
215 1
                $qb
216
                    ->innerJoin('p.ProductCategories', 'pct')
217
                    ->innerJoin('pct.Category', 'c')
218 1
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
219
                    ->setParameter('Categories', $Categories);
220
            }
221
        }
222
223 13
        // status
224
        if (!empty($searchData['status']) && $searchData['status']->toArray()) {
225
            $qb
226
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
227 1
                ->setParameter('Status', $searchData['status']->toArray());
228
        }
229
230 13
        // link_status
231 1
        if (isset($searchData['link_status'])) {
232
            $qb
233 1
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
234
                ->setParameter('Status', $searchData['link_status']);
235
        }
236 1
237
        // stock status
238
        if (isset($searchData['stock_status'])) {
239
            $qb
240
                ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
241
                ->setParameter('StockUnlimited', $searchData['stock_status']);
242
        }
243
244
        // crate_date
245 13 View Code Duplication
        if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
246
            $date = $searchData['create_date_start']
247
                ->format('Y-m-d H:i:s');
248
            $qb
249
                ->andWhere('p.create_date >= :create_date_start')
250
                ->setParameter('create_date_start', $date);
251
        }
252
253
        if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
254 2
            $date = clone $searchData['create_date_end'];
255
            $date = $date
256 2
                ->modify('+1 days')
257 2
                ->format('Y-m-d H:i:s');
258 2
            $qb
259
                ->andWhere('p.create_date < :create_date_end')
260
                ->setParameter('create_date_end', $date);
261
        }
262
263
        // update_date
264 2 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
265
            $date = $searchData['update_date_start']
266
                ->format('Y-m-d H:i:s');
267
            $qb
268
                ->andWhere('p.update_date >= :update_date_start')
269
                ->setParameter('update_date_start', $date);
270
        }
271
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
272
            $date = clone $searchData['update_date_end'];
273
            $date = $date
274
                ->modify('+1 days')
275
                ->format('Y-m-d H:i:s');
276
            $qb
277
                ->andWhere('p.update_date < :update_date_end')
278
                ->setParameter('update_date_end', $date);
279
        }
280
281
282
        // Order By
283
        $qb
284
            ->orderBy('p.update_date', 'DESC');
285
286
        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