Completed
Pull Request — master (#384)
by Kristof
04:13 queued 34s
created

DBALLookupService::itemsOwnedByUser()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 56

Duplication

Lines 12
Ratio 21.43 %

Importance

Changes 0
Metric Value
dl 12
loc 56
c 0
b 0
f 0
rs 8.9599
cc 3
nc 2
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CultuurNet\UDB3\MyOrganizers\ReadModel\Doctrine;
4
5
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
6
use CultuurNet\UDB3\MyOrganizers\MyOrganizersLookupServiceInterface;
7
use CultuurNet\UDB3\MyOrganizers\PartOfCollection;
8
use Doctrine\DBAL\Connection;
9
use PDO;
10
use ValueObjects\Number\Natural;
11
use ValueObjects\StringLiteral\StringLiteral;
12
13
class DBALLookupService implements MyOrganizersLookupServiceInterface
14
{
15
    use DBALHelperTrait;
16
17
    /** @var StringLiteral */
18
    private $tableName;
19
20
    /**
21
     * @var IriGeneratorInterface
22
     */
23
    private $iriGenerator;
24
25
    /**
26
     * @param Connection $connection
27
     * @param StringLiteral $tableName
28
     */
29
    public function __construct(
30
        Connection $connection,
31
        StringLiteral $tableName,
32
        IriGeneratorInterface $iriGenerator
33
    ) {
34
        $this->connection = $connection;
35
        $this->tableName = $tableName;
36
        $this->iriGenerator = $iriGenerator;
37
    }
38
39
    public function itemsOwnedByUser(
40
        string $userId,
41
        Natural $limit,
42
        Natural $start
43
    ): PartOfCollection {
44
        $queryBuilder = $this->connection->createQueryBuilder();
45
46
        $expr = $this->connection->getExpressionBuilder();
47
        $itemIsOwnedByUser = $expr->eq(Columns::UID, $this->parameter(Columns::UID));
48
49
        $queryBuilder->select(Columns::ID)
50
            ->from($this->tableName->toNative())
51
            ->where($itemIsOwnedByUser)
52
            ->orderBy(Columns::UPDATED, 'DESC')
53
            ->setMaxResults($limit->toNative())
54
            ->setFirstResult($start->toNative());
55
56
        $queryBuilder->setParameter(Columns::UID, $userId);
57
58
        $parameters = $queryBuilder->getParameters();
59
60
        $results = $queryBuilder->execute();
61
62
        // @todo transform @id here to an object that has a full URL
63
        // when json-encoded
64
        $organizers = array_map(
65
            function ($resultRow) {
66
                return [
67
                    '@id' => $this->iriGenerator->iri($resultRow[Columns::ID]),
68
                    '@type' => 'Organizer',
69
                ];
70
            },
71
            $results->fetchAll(PDO::FETCH_ASSOC)
72
        );
73
74
        $itemCount = count($organizers);
75
76
        // We can skip an additional query to determine to total items count
77
        // if the amount of rows on the first page does not reach the limit.
78
        $onFirstPage = $queryBuilder->getFirstResult() === 0;
79
        $hasSinglePage = $itemCount < $queryBuilder->getMaxResults();
80 View Code Duplication
        if ($onFirstPage && $hasSinglePage) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81
            $totalItems = $itemCount;
82
        } else {
83
            $q = $this->connection->createQueryBuilder();
84
85
            $totalItems = $q->resetQueryParts()->select('COUNT(*) AS total')
86
                ->from($this->tableName->toNative())
87
                ->where($itemIsOwnedByUser)
88
                ->setParameters($parameters)
89
                ->execute()
90
                ->fetchColumn(0);
91
        }
92
93
        return new PartOfCollection($organizers, new Natural($totalItems));
94
    }
95
}
96