Completed
Push — master ( 73f449...670c74 )
by Kristof
04:33
created

DBALRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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
     * @var String
27
     */
28
    protected $idField;
29
30
    /**
31
     * @var String
32
     */
33
    protected $tableName;
34
35
    /**
36
     * @param String $tableName
37
     * @param Connection $connection
38
     * @param String $idField
39
     */
40
    public function __construct(String $tableName, Connection $connection, String $idField)
41
    {
42
        $this->tableName = $tableName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $tableName can also be of type string. However, the property $tableName is declared as type object<ValueObjects\String\String>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
43
        $this->connection = $connection;
44
        $this->idField = $idField;
0 ignored issues
show
Documentation Bug introduced by
It seems like $idField can also be of type string. However, the property $idField is declared as type object<ValueObjects\String\String>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function getEditableOffers(String $uitId)
51
    {
52
        $q = $this->connection->createQueryBuilder();
53
        $q->select($this->idField->toNative())
54
            ->from($this->tableName->toNative())
55
            ->where('user_id = :userId')
56
            ->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...
57
58
        $results = $q->execute();
59
60
        $events = array();
61
        while ($id = $results->fetchColumn(0)) {
62
            $events[] = new String($id);
63
        }
64
65
        return $events;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function markOfferEditableByUser(String $eventId, String $uitId)
72
    {
73
        try {
74
            $this->connection->insert(
75
                $this->tableName->toNative(),
76
                [
77
                    $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...
78
                    '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...
79
                ]
80
            );
81
        } catch (UniqueConstraintViolationException $e) {
82
            // Intentionally catching database exception occurring when the
83
            // permission record is already in place.
84
        }
85
    }
86
}
87