Completed
Pull Request — master (#1922)
by chihiro
36:39
created

PaginatorAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 61
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B paginate() 0 40 4
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
namespace Eccube\Doctrine\ORM\Tools;
25
26
use Doctrine\ORM\Query;
27
use Doctrine\ORM\QueryBuilder;
28
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper;
29
use Knp\Component\Pager\PaginatorInterface;
30
31
class PaginatorAdapter implements PaginatorInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
32
{
33
    /**
34
     * @var PaginatorInterface
35
     */
36
    protected $paginator;
37
38
    /**
39
     * PaginatorAdapter constructor.
40
     *
41
     * @param PaginatorInterface $paginator
42
     */
43 21
    public function __construct(PaginatorInterface $paginator)
44
    {
45 21
        $this->paginator = $paginator;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 21
    public function paginate($target, $page = 1, $limit = 10, array $options = array())
52
    {
53
        // wrap-queriesが未定義またはfalseの場合はデフォルトのページネータで処理.
54 21
        if (empty($options['wrap-queries'])) {
55 2
            return $this->paginator->paginate($target, $page, $limit, $options);
56
        }
57
58 19
        if ($target instanceof QueryBuilder) {
59 19
            $target = $target->getQuery();
60
        }
61
62 19
        if (!$target instanceof Query) {
63
            return $this->paginator->paginate($target, $page, $limit, $options);
64
        }
65
66
        // 総件数の取得.
67 19
        $platform = $target->getEntityManager()->getConnection()->getDatabasePlatform();
68 19
        $rsm = new Query\ResultSetMapping();
69 19
        $rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');
70
        // `wrap-queries: true`時の総件数取得クエリを再現するため,CountOutputWalkerを使う
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
71
        // CountOutputWalkerはdoctrine orm 2.5.xのコードを利用(2.4.xの不具合が修正されてる)
72 19
        $query = Helper::cloneQuery($target);
73 19
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Eccube\Doctrine\ORM\Tools\Pagination\CountOutputWalker');
74 19
        $query->setResultSetMapping($rsm);
75 19
        $totalCount = $query->getSingleScalarResult();
76
77
        // limit, offsetの設定
78 19
        $offset = abs($page - 1) * $limit;
79 19
        $target->setMaxResults($limit);
80 19
        $target->setFirstResult($offset);
81
82
        // pagenatorを空で作成し, 各オプションを設定する
83 19
        $pagination = $this->paginator->paginate(array());
84 19
        $pagination->setCurrentPageNumber($page);
85 19
        $pagination->setItemNumberPerPage($limit);
86 19
        $pagination->setItems($target->getResult());
87 19
        $pagination->setTotalItemCount($totalCount);
88
89 19
        return $pagination;
90
    }
91
}