Passed
Push — develop ( 627810...75d8c1 )
by Jens
02:19
created

ActivityLogStorage::getActivityLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * User: Jens
4
 * Date: 31-8-2017
5
 * Time: 17:24
6
 */
7
8
namespace CloudControl\Cms\storage\storage;
9
10
11
use CloudControl\Cms\components\CmsComponent;
12
13
class ActivityLogStorage extends AbstractStorage
14
{
15
    public function getActivityLog()
16
    {
17
        return $this->repository->activityLog;
18
    }
19
20
    public function add($message, $icon = null)
21
    {
22
        $activity = $this->createActivity($message, $icon);
23
24
        $activityLog = $this->repository->activityLog;
25
        $activityLog[] = $activity;
26
        usort($activityLog, array($this, 'cmp'));
27
        $activityLog = array_slice($activityLog, 0, 100);
28
        $this->repository->activityLog = $activityLog;
29
        $this->repository->save();
30
    }
31
32
    /**
33
     * @param $message
34
     * @param $icon
35
     * @return \stdClass
36
     */
37
    private function createActivity($message, $icon)
38
    {
39
        $stdObj = new \stdClass();
40
        $stdObj->timestamp = time();
41
        $stdObj->message = $message;
42
        $stdObj->icon = $icon;
43
        $ccSessionObj = $_SESSION[CmsComponent::SESSION_PARAMETER_CLOUD_CONTROL];
44
        $stdObj->user = isset($ccSessionObj->username) ? $ccSessionObj->username : 'undefined';
45
        return $stdObj;
46
    }
47
48
    /**
49
     * Compare a redirect by it's title
50
     * @param $a
51
     * @param $b
52
     * @return int
53
     */
54
    public static function cmp($a, $b)
55
    {
56
        return $a->timestamp < $b->timestamp;
57
    }
58
59
}