Completed
Push — master ( 5d1105...8d5fa1 )
by Konstantinos
18:00
created

ConversationJoinEvent::getNewMembers()   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
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file contains a conversation event
4
 *
5
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
6
 */
7
8
namespace BZIon\Event;
9
10
/**
11
 * Event thrown when players join a conversation
12
 */
13
class ConversationJoinEvent extends Event
14
{
15
    /**
16
     * @var \Conversation
17
     */
18
    protected $conversation;
19
20
    /**
21
     * @var \Model[]
22
     */
23
    protected $members;
24
25
    /**
26
     * Create a new event
27
     *
28
     * @param \Conversation   $conversation   The conversation in question
29
     * @param \Model[] $members The players and teams who joined the conversation
30
     */
31
    public function __construct(\Conversation $conversation, array $members)
32
    {
33
        $this->conversation  = $conversation;
34
        $this->members = $members;
35
    }
36
37
    /**
38
     * Get the conversation that was renamed
39
     *
40
     * @return \Conversation
41
     */
42
    public function getConversation()
43
    {
44
        return $this->conversation;
45
    }
46
47
    /**
48
     * Get the Players and Teams who joined the conversation
49
     *
50
     * @return \Model[]
51
     */
52
    public function getNewMembers()
53
    {
54
        return $this->members;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function serialize()
61
    {
62
        $players = $teams = array();
63
64
        foreach ($this->members as $member) {
65
            if ($member instanceof \Player) {
66
                $players[] = $member;
67
            } else {
68
                $teams[] = $member;
69
            }
70
        }
71
72
        return serialize(array(
73
            'conversation' => $this->conversation->getId(),
74
            'players'      => \Player::mapToIDs($players),
75
            'teams'        => \Team::mapToIDs($teams)
76
        ));
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function unserialize($data)
83
    {
84
        $data = unserialize($data);
85
86
        $conversation = \Conversation::get($data['conversation']);
87
88
        $players = \Player::arrayIdToModel($data['players']);
89
        $teams = \Team::arrayIdToModel($data['teams']);
90
91
        $this->__construct($conversation, array_merge($players, $teams));
0 ignored issues
show
Bug introduced by
It seems like $conversation defined by \Conversation::get($data['conversation']) on line 86 can be null; however, BZIon\Event\ConversationJoinEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
92
    }
93
}
94