1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scheduler\Infrastructure\DBAL; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\DBAL\Statement; |
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use Scheduler\Domain\Model\User\NullUser; |
9
|
|
|
use Scheduler\Domain\Model\User\User; |
10
|
|
|
use Scheduler\Domain\Model\User\UserMapper; |
11
|
|
|
|
12
|
|
|
class DbalUserMapper extends DbalMapper implements UserMapper |
13
|
|
|
{ |
14
|
|
|
const COLUMNS = "id, name, role, email, phone, created_at, updated_at"; |
15
|
|
|
|
16
|
|
|
protected static function findStatement() |
17
|
|
|
{ |
18
|
|
|
return sprintf( |
19
|
|
|
"SELECT %s FROM users WHERE id = ?", |
20
|
|
|
self::COLUMNS |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected static function findByRoleStatement() |
25
|
|
|
{ |
26
|
|
|
return sprintf( |
27
|
|
|
"SELECT %s FROM users WHERE role = ?", |
28
|
|
|
self::COLUMNS |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected static function insertStatement() |
33
|
|
|
{ |
34
|
|
|
return "INSERT INTO users VALUES (?, ?, ?, ?, ?, ?, ?)"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function find($id) |
38
|
|
|
{ |
39
|
|
|
if (! is_int($id)) { |
40
|
|
|
throw new \InvalidArgumentException("The id must be an integer"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$user = $this->abstractFind($id); |
44
|
|
|
|
45
|
|
|
if ($user == null) { |
46
|
|
|
$user = new NullUser(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $user; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function findByRole($role) |
53
|
|
|
{ |
54
|
|
|
$statement = $this->db->prepare(self::findByRoleStatement()); |
55
|
|
|
$statement->bindValue(1, $role, \PDO::PARAM_STR); |
56
|
|
|
$statement->execute(); |
57
|
|
|
|
58
|
|
|
return $this->loadAll($statement); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function doLoad($id, array $resultSet) |
62
|
|
|
{ |
63
|
|
|
extract($resultSet); |
64
|
|
|
$id = (int) $id; |
65
|
|
|
$created = new DateTime($created_at); |
66
|
|
|
$updated = new DateTime($updated_at); |
67
|
|
|
|
68
|
|
|
return new User($id, $name, $role, $email, $phone, $created, $updated); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function insert(User $user) |
72
|
|
|
{ |
73
|
|
|
return $this->abstractInsert($user); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Bind query parameters and execute insert statement |
78
|
|
|
* |
79
|
|
|
* @param User $subject |
80
|
|
|
* @param Statement $insertStatement |
81
|
|
|
* |
82
|
|
|
* @return int The id of the inserted row |
83
|
|
|
*/ |
84
|
|
|
protected function doInsert($subject, Statement $insertStatement) |
85
|
|
|
{ |
86
|
|
|
$insertStatement->bindValue(1, null); |
87
|
|
|
$insertStatement->bindValue(2, $subject->getName(), \PDO::PARAM_STR); |
88
|
|
|
$insertStatement->bindValue(3, $subject->getRole(), \PDO::PARAM_STR); |
89
|
|
|
$insertStatement->bindValue(4, $subject->getEmail(), \PDO::PARAM_STR); |
90
|
|
|
$insertStatement->bindValue(5, $subject->getPhone(), \PDO::PARAM_STR); |
91
|
|
|
$insertStatement->bindValue(6, $subject->getCreated(), Type::DATETIME); |
92
|
|
|
$insertStatement->bindValue(7, $subject->getUpdated(), Type::DATETIME); |
93
|
|
|
$insertStatement->execute(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.