Completed
Pull Request — master (#1922)
by chihiro
37:39 queued 10:15
created

PaginatorAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Doctrine\ORM\Tools;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\QueryBuilder;
7
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper;
8
use Knp\Component\Pager\PaginatorInterface;
9
10
class PaginatorAdapter implements PaginatorInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
11
{
12
    protected $paginator;
13
14 19
    public function __construct(PaginatorInterface $paginator)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
15
    {
16 19
        $this->paginator = $paginator;
17
    }
18
19 19
    public function paginate($target, $page = 1, $limit = 10, array $options = array())
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
20
    {
21
        // wrap-queriesが未定義またはfalseの場合はデフォルトのページネータで処理.
22 19
        if (empty($options['wrap-queries'])) {
23 11
            return $this->paginator->paginate($target, $page, $limit, $options);
24
        }
25
26 8
        if ($target instanceof QueryBuilder) {
27 8
            $target = $target->getQuery();
28
        }
29
30 8
        if (!$target instanceof Query) {
31
            return $this->paginator->paginate($target, $page, $limit, $options);
32
        }
33
34
        // 総件数の取得.
35 8
        $platform = $target->getEntityManager()->getConnection()->getDatabasePlatform();
36 8
        $rsm = new Query\ResultSetMapping();
37 8
        $rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');
38
        // `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...
39
        // CountOutputWalkerはdoctrine orm 2.5.xのコードを利用(2.4.xの不具合が修正されてる)
40 8
        $query = Helper::cloneQuery($target);
41 8
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Eccube\Doctrine\ORM\Tools\Pagination\CountOutputWalker');
42 8
        $query->setResultSetMapping($rsm);
43 8
        $totalCount = $query->getSingleScalarResult();
44
45
        // limit, offsetの設定
46 8
        $offset = abs($page - 1) * $limit;
47 8
        $target->setMaxResults($limit);
48 8
        $target->setFirstResult($offset);
49
50
        // pagenatorを空で作成し, 各オプションを設定する
51 8
        $pagination = $this->paginator->paginate(array());
52 8
        $pagination->setCurrentPageNumber($page);
53 8
        $pagination->setItemNumberPerPage($limit);
54 8
        $pagination->setItems($target->getResult());
55 8
        $pagination->setTotalItemCount($totalCount);
56
57 8
        return $pagination;
58
    }
59
}