Completed
Push — master ( a433d4...897506 )
by Kevin
06:44
created

RSMQueryResult   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 16.22 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 2
cbo 5
dl 12
loc 74
ccs 25
cts 25
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A take() 12 12 1
A count() 0 4 1
A getIterator() 0 4 1
A getQuery() 0 4 1
A createQuery() 0 7 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
namespace Zenstruck\Porpaginas\Doctrine;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\NativeQuery;
8
use Doctrine\ORM\Query\ResultSetMappingBuilder;
9
use Zenstruck\Porpaginas\Callback\CallbackPage;
10
use Zenstruck\Porpaginas\Result;
11
12
/**
13
 * @author Florian Klein <[email protected]>
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class RSMQueryResult implements Result
17
{
18
    private $em;
19
    private $rsm;
20
    private $qb;
21
    private $queryBuilderResult;
22
23
    /**
24
     * @param EntityManagerInterface  $em
25
     * @param ResultSetMappingBuilder $rsm
26
     * @param QueryBuilder            $qb
27
     * @param callable|null           $countQueryBuilderModifier
28 8
     */
29
    public function __construct(EntityManagerInterface $em, ResultSetMappingBuilder $rsm, QueryBuilder $qb, callable $countQueryBuilderModifier = null)
30 8
    {
31 8
        $this->em = $em;
32 8
        $this->rsm = $rsm;
33 8
        $this->qb = $qb;
34 8
        $this->queryBuilderResult = new DBALQueryBuilderResult($qb, $countQueryBuilderModifier);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39 5
     */
40 View Code Duplication
    public function take($offset, $limit)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41 5
    {
42 5
        $qb = clone $this->qb;
43 5
        $results = function ($offset, $limit) use ($qb) {
44 5
            $qb->setFirstResult($offset)
45
                ->setMaxResults($limit);
46 5
47 5
            return $this->createQuery($qb)->execute();
48
        };
49 5
50 5
        return new CallbackPage($results, [$this, 'count'], $offset, $limit);
51
    }
52 5
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function count()
57
    {
58 3
        return $this->queryBuilderResult->count();
59
    }
60 3
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getIterator()
65
    {
66 3
        return new \ArrayIterator($this->getQuery()->execute());
67
    }
68 3
69 3
    /**
70
     * @return NativeQuery
71 3
     */
72 2
    public function getQuery()
73 3
    {
74 3
        return $this->createQuery($this->qb);
75
    }
76
77
    /**
78
     * @param QueryBuilder $qb
79
     *
80
     * @return NativeQuery
81
     */
82
    private function createQuery(QueryBuilder $qb)
83
    {
84
        $query = $this->em->createNativeQuery($qb->getSQL(), $this->rsm);
85
        $query->setParameters($this->qb->getParameters());
86
87
        return $query;
88
    }
89
}
90