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

DBALLookupService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 14.46 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 12
loc 83
c 0
b 0
f 0
wmc 4
lcom 1
cbo 8
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B itemsOwnedByUser() 12 56 3

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 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