Completed
Pull Request — master (#117)
by Kristof
05:07
created

DBALRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getEditableOffers() 0 17 2
A markOfferEditableByUser() 0 15 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jonas
5
 * Date: 27.10.15
6
 * Time: 14:04
7
 */
8
9
namespace CultuurNet\UDB3\Offer\ReadModel\Permission\Doctrine;
10
11
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionQueryInterface;
12
use Doctrine\DBAL\Connection;
13
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
14
use Doctrine\DBAL\Schema\Schema;
15
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionRepositoryInterface;
16
use ValueObjects\String\String;
17
18
class DBALRepository implements PermissionRepositoryInterface, PermissionQueryInterface
19
{
20
    /**
21
     * @var Connection
22
     */
23
    protected $connection;
24
25
    /**
26
     * @param String $tableName
27
     * @param Connection $connection
28
     * @param String $idField
29
     */
30
    public function __construct(String $tableName, Connection $connection, String $idField)
31
    {
32
        $this->tableName = $tableName;
0 ignored issues
show
Bug introduced by
The property tableName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        $this->connection = $connection;
34
        $this->idField = $idField;
0 ignored issues
show
Bug introduced by
The property idField does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function getEditableOffers(String $uitId)
41
    {
42
        $q = $this->connection->createQueryBuilder();
43
        $q->select($this->idField->toNative())
44
            ->from($this->tableName->toNative())
45
            ->where('user_id = :userId')
46
            ->setParameter(':userId', $uitId->toNative());
0 ignored issues
show
Bug introduced by
The method toNative cannot be called on $uitId (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
47
48
        $results = $q->execute();
49
50
        $events = array();
51
        while ($id = $results->fetchColumn(0)) {
52
            $events[] = new String($id);
53
        }
54
55
        return $events;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function markOfferEditableByUser(String $eventId, String $uitId)
62
    {
63
        try {
64
            $this->connection->insert(
65
                $this->tableName->toNative(),
66
                [
67
                    $this->idField->toNative() => $eventId->toNative(),
0 ignored issues
show
Bug introduced by
The method toNative cannot be called on $eventId (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
68
                    'user_id' => $uitId->toNative()
0 ignored issues
show
Bug introduced by
The method toNative cannot be called on $uitId (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
                ]
70
            );
71
        } catch (UniqueConstraintViolationException $e) {
72
            // Intentionally catching database exception occurring when the
73
            // permission record is already in place.
74
        }
75
    }
76
}
77