Completed
Pull Request — master (#526)
by Michael
02:11
created

Log   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 151
ccs 0
cts 93
cp 0
rs 10
c 1
b 0
f 0
wmc 20

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setObjectType() 0 3 1
A getUserObject() 0 3 1
A getObjectId() 0 3 1
A delete() 0 3 1
A setUser() 0 7 2
A getUser() 0 3 1
A setComment() 0 3 1
A setAction() 0 3 1
A save() 0 25 3
A setObjectId() 0 3 1
A getObjectDescription() 0 16 3
A getTimestamp() 0 3 1
A getComment() 0 3 1
A getObjectType() 0 3 1
A getAction() 0 3 1
1
<?php
2
3
/**
4
 * Log short summary.
5
 *
6
 * Log description.
7
 *
8
 * @version 1.0
9
 * @author stwalkerster
10
 */
11
class Log extends DataObject
12
{
13
	private $objectid;
14
	private $objecttype;
15
	private $user;
16
	private $action;
17
	private $timestamp;
18
	private $comment;
19
20
	public function save()
21
	{
22
		if ($this->isNew) {
23
			$statement = $this->dbObject->prepare(<<<SQL
24
                INSERT INTO log (objectid, objecttype, user, action, timestamp, comment) 
25
                VALUES (:id, :type, :user, :action, CURRENT_TIMESTAMP(), :comment);
26
SQL
27
			);
28
29
			$statement->bindValue(":id", $this->objectid);
30
			$statement->bindValue(":type", $this->objecttype);
31
			$statement->bindValue(":user", $this->user);
32
			$statement->bindValue(":action", $this->action);
33
			$statement->bindValue(":comment", $this->comment);
34
35
			if ($statement->execute()) {
36
				$this->isNew = false;
37
				$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...
38
			}
39
			else {
40
				throw new Exception($statement->errorInfo());
0 ignored issues
show
Bug introduced by
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
				throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
41
			}
42
		}
43
		else {
44
			throw new Exception("Updating logs is not available");
45
		}
46
	}
47
48
	public function delete()
49
	{
50
		throw new Exception("Deleting logs is not available.");
51
	}
52
53
	public function getObjectId()
54
	{
55
		return $this->objectid;
56
	}
57
58
	/**
59
	 * Summary of setObjectId
60
	 * @param int $objectId
61
	 */
62
	public function setObjectId($objectId)
63
	{
64
		$this->objectid = $objectId;
65
	}
66
67
	public function getObjectType()
68
	{
69
		return $this->objecttype;
70
	}
71
72
	/**
73
	 * Summary of setObjectType
74
	 * @param string $objectType
75
	 */
76
	public function setObjectType($objectType)
77
	{
78
		$this->objecttype = $objectType;
79
	}
80
81
	public function getUser()
82
	{
83
		return $this->user;
84
	}
85
	
86
	/**
87
	 * Summary of getUserObject
88
	 * @return User|null
89
	 */
90
	public function getUserObject()
91
	{
92
		return User::getById($this->user, $this->dbObject);
93
	}
94
95
	/**
96
	 * Summary of setUser
97
	 * @param User $user
98
	 */
99
	public function setUser($user)
100
	{
101
		if (is_a($user, "User")) {
102
			$this->user = $user->getId();   
103
		}
104
		else {
105
			$this->user = $user;   
106
		}
107
	}
108
109
	public function getAction()
110
	{
111
		return $this->action;
112
	}
113
114
	/**
115
	 * Summary of setAction
116
	 * @param string $action 
117
	 */
118
	public function setAction($action)
119
	{
120
		$this->action = $action;
121
	}
122
123
	public function getTimestamp()
124
	{
125
		return $this->timestamp;
126
	}
127
128
	public function getComment()
129
	{
130
		return $this->comment;
131
	}
132
133
	/**
134
	 * Summary of setComment
135
	 * @param string $comment 
136
	 */
137
	public function setComment($comment)
138
	{
139
		$this->comment = $comment;
140
	}
141
	
142
	/**
143
	 * Let's be really sneaky here, and fake this to the object description of the logged object.
144
	 * @return string
145
	 */
146
	public function getObjectDescription()
147
	{
148
		$type = $this->objecttype;
149
		
150
		if ($type == "") {
151
			return "";
152
		}
153
154
		/** @var DataObject $object */
155
		$object = $type::getById($this->objectid, $this->dbObject);
156
157
		if ($object === false) {
0 ignored issues
show
introduced by
The condition $object === false is always false.
Loading history...
158
			return '[' . $this->objecttype . " " . $this->objectid . ']';
159
		}
160
		
161
		return $object->getObjectDescription();
162
	}
163
}
164