Completed
Pull Request — master (#117)
by Kristof
04:39
created

DBALRepository::markPlaceEditableByUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 15
loc 15
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 7
nc 2
nop 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\Place\ReadModel\Permission\Doctrine;
10
11
use CultuurNet\UDB3\Place\ReadModel\Permission\PermissionQueryInterface;
12
use Doctrine\DBAL\Connection;
13
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
14
use Doctrine\DBAL\Schema\Schema;
15
use CultuurNet\UDB3\Place\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
     */
29
    public function __construct(String $tableName, Connection $connection)
30
    {
31
        $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...
32
        $this->connection = $connection;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38 View Code Duplication
    public function getEditablePlaces(String $uitId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
    {
40
        $q = $this->connection->createQueryBuilder();
41
        $q->select('place_id')
42
            ->from($this->tableName->toNative())
43
            ->where('user_id = :userId')
44
            ->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...
45
46
        $results = $q->execute();
47
48
        $places = array();
49
        while ($id = $results->fetchColumn(0)) {
50
            $places[] = new String($id);
51
        }
52
53
        return $places;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 View Code Duplication
    public function markPlaceEditableByUser(String $placeId, String $uitId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61
        try {
62
            $this->connection->insert(
63
                $this->tableName->toNative(),
64
                [
65
                    'place_id' => $placeId->toNative(),
0 ignored issues
show
Bug introduced by
The method toNative cannot be called on $placeId (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...
66
                    '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...
67
                ]
68
            );
69
        } catch (UniqueConstraintViolationException $e) {
70
            // Intentionally catching database exception occurring when the
71
            // permission record is already in place.
72
        }
73
    }
74
}
75