Passed
Push — master ( b8999c...a2f941 )
by Curtis
09:12 queued 03:15
created

Timeline::timeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.9332
1
<?php
2
3
namespace App\Http\Response\enso\ActivityLog;
4
5
use Illuminate\Contracts\Support\Responsable;
6
use Illuminate\Database\Eloquent\Collection;
7
use LaravelEnso\Core\Http\Resources\User;
8
use App\Models\enso\activitylogs\ActivityLog;
9
class Timeline implements Responsable
10
{
11
    private const Chunk = 50;
12
13
    private Collection $timeline;
14
15
    public function toResponse($request)
16
    {
17
        $this->timeline = $this->timeline($request);
18
19
        return $this->days()->map(fn ($day) => [
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->days()->ma...ion(...) { /* ... */ }) returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the return type mandated by Illuminate\Contracts\Sup...sponsable::toResponse() of Symfony\Component\HttpFoundation\Response.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
20
            'date' => $day,
21
            'entries' => $this->dayEntries($day)
22
                ->map(fn ($entry) => $this->resource($entry)),
23
        ]);
24
    }
25
26
    public function timeline($request)
27
    {
28
        $filters = json_decode($request->get('filters'));
29
30
        return ActivityLog::latest()
31
            ->with('createdBy.person', 'createdBy.avatar')
32
            ->skip($request->get('offset'))
33
            ->between($filters->interval->min, $filters->interval->max)
34
            ->forUsers($filters->userIds)
35
            ->forEvents($filters->events)
36
            ->forRoles($filters->roleIds)
37
            ->take(self::Chunk)
38
            ->get();
39
    }
40
41
    private function resource($entry)
42
    {
43
        return [
44
            'id' => $entry->id,
45
            'meta' => $entry->meta,
46
            'time' => $entry->created_at->format('H:i A'),
47
            'owner' => new User($entry->createdBy),
48
        ];
49
    }
50
51
    private function dayEntries($day)
52
    {
53
        return $this->timeline
54
            ->filter(fn ($entry) => $entry->created_at->format('Y-m-d') === $day)
55
            ->values();
56
    }
57
58
    private function days()
59
    {
60
        return $this->timeline
61
            ->map(fn ($entry) => $entry->created_at->format('Y-m-d'))
62
            ->unique()
63
            ->values();
64
    }
65
}
66