Completed
Push — master ( 7d1f70...9a034d )
by ReliQ
02:43
created

NewMessage::broadcastOn()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace ReliQArts\Mardin\Events;
4
5
use League\Fractal\Manager;
6
use League\Fractal\Resource\Item;
7
use Illuminate\Broadcasting\Channel;
8
use Illuminate\Queue\SerializesModels;
9
use ReliQArts\Mardin\Contracts\Message;
10
use Illuminate\Broadcasting\PrivateChannel;
11
use Illuminate\Broadcasting\InteractsWithSockets;
12
use ReliQArts\Mardin\Transformers\MessageTransformer;
13
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
14
15
class NewMessage implements ShouldBroadcast
16
{
17
    use InteractsWithSockets, SerializesModels;
18
19
    /**
20
     * The name of the queue on which to place the event.
21
     *
22
     * @var string
23
     */
24
    public $broadcastQueue = 'mardin';
25
26
    /**
27
     * @var array Channels for message.
28
     */
29
    private $channels;
30
31
    /**
32
     * @var Message Message.
33
     */
34
    public $message;
35
36
    /**
37
     * Create a new event instance.
38
     *
39
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
40
     */
41
    public function __construct(Message $message)
42
    {
43
        $this->message = $message;
44
        $this->channels = [];
45
    }
46
47
    /**
48
     * The event's broadcast name.
49
     *
50
     * @return string
51
     */
52
    public function broadcastAs()
53
    {
54
        return 'newMessage';
55
    }
56
57
    /**
58
     * Get the channels the event should broadcast on.
59
     *
60
     * @return Channel|array
61
     */
62
    public function broadcastOn()
63
    {
64
        // Get channels for Broadcast
65
        foreach ($this->message->thread->participantsUserIds() as $recipient) {
0 ignored issues
show
Bug introduced by
Accessing thread on the interface ReliQArts\Mardin\Contracts\Message suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
66
            if ($recipient == $this->message->user->id) {
0 ignored issues
show
Bug introduced by
Accessing user on the interface ReliQArts\Mardin\Contracts\Message suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
                continue;
68
            }
69
            $this->channels[] = new PrivateChannel("Mardin.Messages.User.{$recipient}");
70
        }
71
72
        // Set Message as Fractal Item
73
        $fractal = new Manager;
74
        $fractal->parseIncludes('thread');
75
        $this->message = $fractal->createData(new Item($this->message, new MessageTransformer))->toArray();
76
77
        return $this->channels;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->channels; (array) is incompatible with the return type declared by the interface Illuminate\Contracts\Bro...dBroadcast::broadcastOn of type Illuminate\Broadcasting\...\Broadcasting\Channel[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
78
    }
79
}
80