]> BookStack Code Mirror - bookstack/blob - app/Services/ActivityService.php
f0c52758641551a8d4301210fc30769284096827
[bookstack] / app / Services / ActivityService.php
1 <?php namespace Oxbow\Services;
2
3 use Illuminate\Support\Facades\Auth;
4 use Oxbow\Activity;
5 use Oxbow\Entity;
6
7 class ActivityService
8 {
9     protected $activity;
10     protected $user;
11
12     /**
13      * ActivityService constructor.
14      * @param $activity
15      */
16     public function __construct(Activity $activity)
17     {
18         $this->activity = $activity;
19         $this->user = Auth::user();
20     }
21
22     /**
23      * Add activity data to database.
24      * @para Entity $entity
25      * @param $activityKey
26      * @param int $bookId
27      */
28     public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
29     {
30         $this->activity->user_id = $this->user->id;
31         $this->activity->book_id = $bookId;
32         $this->activity->key = strtolower($activityKey);
33         if($extra !== false) {
34             $this->activity->extra = $extra;
35         }
36         $entity->activity()->save($this->activity);
37     }
38
39     /**
40      * Adds a activity history with a message & without binding to a entitiy.
41      * @param $activityKey
42      * @param int $bookId
43      * @param bool|false $extra
44      */
45     public function addMessage($activityKey, $bookId = 0, $extra = false)
46     {
47         $this->activity->user_id = $this->user->id;
48         $this->activity->book_id = $bookId;
49         $this->activity->key = strtolower($activityKey);
50         if($extra !== false) {
51             $this->activity->extra = $extra;
52         }
53         $this->activity->save();
54     }
55
56     /**
57      * Gets the latest activity.
58      * @param int $count
59      * @param int $page
60      */
61     public function latest($count = 20, $page = 0)
62     {
63         return $this->activity->orderBy('created_at', 'desc')
64             ->skip($count*$page)->take($count)->get();
65     }
66
67 }