Passed
Push — master ( 01151b...3d8b44 )
by Kevin
04:44
created

DBALQueryBuilderResult   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 50
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A take() 0 13 1
A __construct() 0 5 2
A count() 0 11 2
A getIterator() 0 6 2
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Zenstruck\Porpaginas\Callback\CallbackPage;
7
use Zenstruck\Porpaginas\Page;
8
use Zenstruck\Porpaginas\Result;
9
use Zenstruck\Porpaginas\ResultPaginator;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class DBALQueryBuilderResult implements Result
15
{
16
    use ResultPaginator;
17
18
    private QueryBuilder $qb;
19
    private $countModifier;
20
    private ?int $count = null;
21
22 45
    public function __construct(QueryBuilder $qb, ?callable $countModifier = null)
23
    {
24 45
        $this->qb = $qb;
25
        $this->countModifier = $countModifier ?: static function(QueryBuilder $qb) {
26 36
            return $qb->select('COUNT(*)');
27 45
        };
28 45
    }
29
30 7
    public function take(int $offset, int $limit): Page
31
    {
32 7
        $qb = clone $this->qb;
33
        $results = static function($offset, $limit) use ($qb) {
34
            return $qb
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetchAll() has been deprecated: Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

34
            return /** @scrutinizer ignore-deprecated */ $qb

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
35 7
                ->setFirstResult($offset)
36 7
                ->setMaxResults($limit)
37 7
                ->execute()
38 7
                ->fetchAll()
39
            ;
40 7
        };
41
42 7
        return new CallbackPage($results, [$this, 'count'], $offset, $limit);
43
    }
44
45 36
    public function count(): int
46
    {
47 36
        if (null !== $this->count) {
48 1
            return $this->count;
49
        }
50
51 36
        $qb = clone $this->qb;
52
53 36
        \call_user_func($this->countModifier, $qb);
54
55 36
        return $this->count = $qb->execute()->fetchColumn();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...tatement::fetchColumn() has been deprecated: Use fetchOne() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
        return $this->count = /** @scrutinizer ignore-deprecated */ $qb->execute()->fetchColumn();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The expression return $this->count = $q...xecute()->fetchColumn() could return the type false which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
56
    }
57
58 34
    public function getIterator(): \Traversable
59
    {
60 34
        $stmt = $this->qb->execute();
61
62 34
        while ($data = $stmt->fetch()) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
        while ($data = /** @scrutinizer ignore-deprecated */ $stmt->fetch()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
63 33
            yield $data;
64
        }
65 34
    }
66
}
67