Passed
Push — master ( bc355e...639701 )
by Adam
25:37
created

PostSaved::broadcastAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Coyote\Events;
4
5
use Coyote\Http\Resources\PostResource;
6
use Coyote\Post;
7
use Illuminate\Broadcasting\Channel;
8
use Illuminate\Broadcasting\InteractsWithSockets;
9
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10
use Illuminate\Queue\SerializesModels;
11
12
class PostSaved implements ShouldBroadcast
13
{
14
    use SerializesModels, InteractsWithSockets;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by Coyote\Events\PostSaved: $id, $relations, $class, $connection, $keyBy
Loading history...
15
16
    /**
17
     * @var Post
18
     */
19
    public Post $post;
20
21
    /**
22
     * @var bool
23
     */
24
    public bool $wasRecentlyCreated;
25
26
    /**
27
     * Create a new event instance.
28
     *
29
     * @param Post $post
30
     */
31
    public function __construct(Post $post)
32
    {
33
        $this->post = $post;
34
        $this->wasRecentlyCreated = $post->wasRecentlyCreated;
35
    }
36
37
    /**
38
     * @return Channel|Channel[]
39
     */
40
    public function broadcastOn()
41
    {
42
        return new Channel('topic:' . $this->post->topic_id);
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function broadcastWith(): array
49
    {
50
        $request = clone request();
51
        // assign null to user.
52
        $request->setUserResolver(function () {
53
            return null;
54
        });
55
56
        return (new PostResource($this->post))->resolve($request);
57
    }
58
59
60
}
61