Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

classes/Elgg/SystemLog/SystemLogInsert.php (3 issues)

1
<?php
2
3
namespace Elgg\SystemLog;
4
5
use Elgg\Cache\CompositeCache;
6
use Elgg\Database\Insert;
7
8
/**
9
 * Inserts log entry into the database
10
 */
11
class SystemLogInsert {
12
13
	/**
14
	 * @var CompositeCache
15
	 */
16
	protected static $cache;
17
18
	/**
19
	 * Returns a cache of logged system events
20
	 *
21
	 * @return CompositeCache
22
	 */
23 426
	protected function getCache() {
24 426
		if (!isset(self::$cache)) {
25 1
			$flags = ELGG_CACHE_PERSISTENT | ELGG_CACHE_FILESYSTEM | ELGG_CACHE_RUNTIME;
26 1
			$cache = new CompositeCache('system_log', _elgg_config(), $flags);
27
28 1
			register_shutdown_function(function () use ($cache) {
29
				$cache->clear();
30 1
			});
31
32 1
			self::$cache = $cache;
33
		}
34
35 426
		return self::$cache;
36
	}
37
38
	/**
39
	 * Prepare an object for DB insert
40
	 *
41
	 * @param \Loggable $object Object
42
	 *
43
	 * @return \stdClass
44
	 */
45 426
	protected function prepareObjectForInsert(\Loggable $object) {
46 426
		$insert = new \stdClass();
47
48 426
		$insert->object_id = (int) $object->getSystemLogID();
49 426
		$insert->object_class = get_class($object);
50 426
		$insert->object_type = $object->getType();
51 426
		$insert->object_subtype = $object->getSubtype();
52 426
		$insert->ip_address = _elgg_services()->request->getClientIp() ? : '0.0.0.0';
53 426
		$insert->performed_by_guid = elgg_get_logged_in_user_guid();
54
55 426
		if (isset($object->access_id)) {
1 ignored issue
show
Accessing access_id on the interface Loggable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
56 422
			$insert->access_id = $object->access_id;
57
		} else {
58 25
			$insert->access_id = ACCESS_PUBLIC;
59
		}
60
61 426
		if (isset($object->enabled)) {
1 ignored issue
show
Accessing enabled on the interface Loggable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
62 422
			$insert->enabled = $object->enabled;
63
		} else {
64 25
			$insert->enabled = 'yes';
65
		}
66
67 426
		if (isset($object->owner_guid)) {
1 ignored issue
show
Accessing owner_guid on the interface Loggable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
68 350
			$insert->owner_guid = $object->owner_guid;
69
		} else {
70 340
			$insert->owner_guid = 0;
71
		}
72
73 426
		return $insert;
74
	}
75
76
77
	/**
78
	 * Logs a system event in the database
79
	 *
80
	 * @param \stdClass $object Object to log
81
	 * @param string    $event  Event name
82
	 *
83
	 * @return void
84
	 */
85 429
	public function insert($object, $event) {
86
87 429
		if (!$object instanceof \Loggable) {
88 49
			return;
89
		}
90
91 426
		$object = $this->prepareObjectForInsert($object);
92
93 426
		$logged = $this->getCache()->load("$object->object_id/$event");
94
95 426
		if ($logged == $object) {
96 303
			return;
97
		}
98
99 426
		$qb = Insert::intoTable('system_log');
100 426
		$qb->values([
101 426
			'object_id' => $qb->param($object->object_id, ELGG_VALUE_INTEGER),
102 426
			'object_class' => $qb->param($object->object_class, ELGG_VALUE_STRING),
103 426
			'object_type' => $qb->param($object->object_type, ELGG_VALUE_STRING),
104 426
			'object_subtype' => $qb->param($object->object_subtype, ELGG_VALUE_STRING),
105 426
			'event' => $qb->param($event, ELGG_VALUE_STRING),
106 426
			'performed_by_guid' => $qb->param($object->performed_by_guid, ELGG_VALUE_INTEGER),
107 426
			'owner_guid' => $qb->param($object->owner_guid, ELGG_VALUE_INTEGER),
108 426
			'access_id' => $qb->param($object->access_id, ELGG_VALUE_INTEGER),
109 426
			'enabled' => $qb->param($object->enabled, ELGG_VALUE_STRING),
110 426
			'time_created' => $qb->param(time(), ELGG_VALUE_INTEGER),
111 426
			'ip_address' => $qb->param('ip_address', ELGG_VALUE_STRING),
112
		]);
113
114 426
		execute_delayed_write_query($qb->getSQL(), null, $qb->getParameters());
115
116
		// The only purpose of the cache is to prevent the same event from writing to the database twice
117
		// Setting early expiration to avoid cache from taking up too much memory
118 426
		$this->getCache()->save("$object->object_id/$event", $object, 3600);
119 426
	}
120
121
}