stwalkerster /
waca
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Waca\DataObjects; |
||
| 3 | |||
| 4 | use DateTimeImmutable; |
||
| 5 | use Exception; |
||
| 6 | use Waca\DataObject; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class representing a log entry |
||
| 10 | */ |
||
| 11 | class Log extends DataObject |
||
| 12 | { |
||
| 13 | /** @var int */ |
||
| 14 | private $objectid; |
||
| 15 | /** @var string */ |
||
| 16 | private $objecttype; |
||
| 17 | /** @var int */ |
||
| 18 | private $user; |
||
| 19 | /** @var string */ |
||
| 20 | private $action; |
||
| 21 | private $timestamp; |
||
| 22 | /** @var string|null */ |
||
| 23 | private $comment; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @throws Exception |
||
| 27 | */ |
||
| 28 | public function save() |
||
| 29 | { |
||
| 30 | if ($this->isNew()) { |
||
| 31 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 32 | INSERT INTO log (objectid, objecttype, user, action, timestamp, comment) |
||
| 33 | VALUES (:id, :type, :user, :action, CURRENT_TIMESTAMP(), :comment); |
||
| 34 | SQL |
||
| 35 | ); |
||
| 36 | |||
| 37 | $statement->bindValue(":id", $this->objectid); |
||
| 38 | $statement->bindValue(":type", $this->objecttype); |
||
| 39 | $statement->bindValue(":user", $this->user); |
||
| 40 | $statement->bindValue(":action", $this->action); |
||
| 41 | $statement->bindValue(":comment", $this->comment); |
||
| 42 | |||
| 43 | if ($statement->execute()) { |
||
| 44 | $this->id = $this->dbObject->lastInsertId(); |
||
| 45 | } |
||
| 46 | else { |
||
| 47 | throw new Exception($statement->errorInfo()); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | else { |
||
| 51 | throw new Exception("Updating logs is not available"); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @throws Exception |
||
| 57 | */ |
||
| 58 | public function delete() |
||
| 59 | { |
||
| 60 | throw new Exception("Deleting logs is not available."); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return int |
||
| 65 | */ |
||
| 66 | public function getObjectId() |
||
| 67 | { |
||
| 68 | return $this->objectid; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Summary of setObjectId |
||
| 73 | * |
||
| 74 | * @param int $objectId |
||
| 75 | */ |
||
| 76 | public function setObjectId($objectId) |
||
| 77 | { |
||
| 78 | $this->objectid = $objectId; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function getObjectType() |
||
| 85 | { |
||
| 86 | return $this->objecttype; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Summary of setObjectType |
||
| 91 | * |
||
| 92 | * @param string $objectType |
||
| 93 | */ |
||
| 94 | public function setObjectType($objectType) |
||
| 95 | { |
||
| 96 | $this->objecttype = $objectType; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return int |
||
| 101 | */ |
||
| 102 | public function getUser() |
||
| 103 | { |
||
| 104 | return $this->user; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Summary of setUser |
||
| 109 | * |
||
| 110 | * @param User|integer $user |
||
| 111 | */ |
||
| 112 | public function setUser($user) |
||
| 113 | { |
||
| 114 | if (is_a($user, User::class)) { |
||
| 115 | $this->user = $user->getId(); |
||
|
0 ignored issues
–
show
|
|||
| 116 | } |
||
| 117 | else { |
||
| 118 | $this->user = $user; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getAction() |
||
| 126 | { |
||
| 127 | return $this->action; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Summary of setAction |
||
| 132 | * |
||
| 133 | * @param string $action |
||
| 134 | */ |
||
| 135 | public function setAction($action) |
||
| 136 | { |
||
| 137 | $this->action = $action; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return DateTimeImmutable |
||
| 142 | */ |
||
| 143 | public function getTimestamp() |
||
| 144 | { |
||
| 145 | return new DateTimeImmutable($this->timestamp); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string|null |
||
| 150 | */ |
||
| 151 | public function getComment() |
||
| 152 | { |
||
| 153 | return $this->comment; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Summary of setComment |
||
| 158 | * |
||
| 159 | * @param string $comment |
||
| 160 | */ |
||
| 161 | public function setComment($comment) |
||
| 162 | { |
||
| 163 | $this->comment = $comment; |
||
| 164 | } |
||
| 165 | } |
||
| 166 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: