Failed Conditions
Branch newinternal (0e936a)
by Simon
03:37
created

Log   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 16
c 4
b 0
f 0
lcom 2
cbo 2
dl 0
loc 155
ccs 0
cts 77
cp 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
B save() 0 26 3
A delete() 0 4 1
A getObjectId() 0 4 1
A setObjectId() 0 4 1
A getObjectType() 0 4 1
A setObjectType() 0 4 1
A getUser() 0 4 1
A setUser() 0 9 2
A getAction() 0 4 1
A setAction() 0 4 1
A getTimestamp() 0 4 1
A getComment() 0 4 1
A setComment() 0 4 1
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();
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $this->dbObject->lastInsertId() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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
Bug introduced by
It seems like $user is not always an object, but can also be of type integer. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
116
		}
117
		else {
118
			$this->user = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user can also be of type object<Waca\DataObjects\User>. However, the property $user is declared as type integer. 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...
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