Completed
Push — master ( 74e4e6...927297 )
by Rémi
02:40
created

MessageParserException::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MessageApp\Parser\Exception;
4
5
use MessageApp\Parser\LocalizedUser;
6
7
class MessageParserException extends \Exception
8
{
9
    const JOIN_MULTIPLE_USERS = 'game.parser.exception.join.multiple-users';
10
    const JOIN_UNREGISTERED_USER = 'game.parser.exception.join.unregistered-users';
11
    const JOIN_NO_GAME = 'game.parser.exception.join.no-game';
12
    const JOIN_YOURSELF = 'game.parser.exception.join.yourself';
13
    const JOIN_GAME_RUNNING = 'game.parser.exception.join.game-running';
14
    const START_NOT_IN = 'game.parser.exception.start.not-in';
15
    const LEAVE_NOT_IN = 'game.parser.exception.leave.not-in';
16
    const CREATE_MULTIPLE = 'game.parser.exception.create.multiple';
17
    const GAME_NOT_FOUND = 'game.parser.exception.game-not-found';
18
    const INVALID_USER = 'parser.exception.invalid-user';
19
    const PARSE_ERROR = 'parser.exception.parse-error';
20
21
    /**
22
     * @var LocalizedUser
23
     */
24
    private $user;
25
26
    /**
27
     * @var string
28
     */
29
    private $codeName;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param LocalizedUser $user
35
     * @param string        $codeName
36
     * @param string        $message
37
     * @param int           $code
38
     * @param \Exception    $previous
39
     */
40 39
    public function __construct(
41
        LocalizedUser $user = null,
42
        $codeName = null,
43
        $message = '',
44
        $code = 0,
45
        \Exception $previous = null
46
    ) {
47 39
        parent::__construct($message, $code, $previous);
48 39
        $this->codeName = $codeName;
49 39
        $this->user = $user;
50 39
    }
51
52
    /**
53
     * Returns the user
54
     *
55
     * @return LocalizedUser
56
     */
57
    public function getUser()
58
    {
59
        return $this->user;
60
    }
61
62
    /**
63
     * @return string
64
     */
65 36
    public function getCodeName()
66
    {
67 36
        return $this->codeName;
68
    }
69
70
    /**
71
     * @param LocalizedUser $user
72
     * @return MessageParserException
73
     */
74 3
    public static function cannotJoinMultipleUsers(LocalizedUser $user)
75
    {
76 3
        return new self($user, self::JOIN_MULTIPLE_USERS, 'You have to provide one (and only one) user to join!');
77
    }
78
79
    /**
80
     * @param LocalizedUser $user
81
     * @return MessageParserException
82
     */
83 3
    public static function cannotJoinUnregisteredUser(LocalizedUser $user)
84
    {
85 3
        return new self($user, self::JOIN_UNREGISTERED_USER, 'You cannot join a user who is not registered!');
86
    }
87
88
    /**
89
     * @param LocalizedUser $user
90
     * @return MessageParserException
91
     */
92 3
    public static function cannotJoinUserWithoutAGame(LocalizedUser $user)
93
    {
94 3
        return new self($user, self::JOIN_NO_GAME, 'You cannot join a player with no game!');
95
    }
96
97
    /**
98
     * @param LocalizedUser $user
99
     * @return MessageParserException
100
     */
101 3
    public static function cannotJoinYourself(LocalizedUser $user)
102
    {
103 3
        return new self($user, self::JOIN_YOURSELF, 'You cannot join yourself!');
104
    }
105
106
    /**
107
     * @param LocalizedUser $user
108
     * @return MessageParserException
109
     */
110 3
    public static function cannotJoinIfGameAlreadyRunning(LocalizedUser $user)
111
    {
112 3
        return new self($user, self::JOIN_GAME_RUNNING, 'You already have a game running!');
113
    }
114
115
    /**
116
     * @param LocalizedUser $user
117
     * @return MessageParserException
118
     */
119 3
    public static function cannotParseMessage(LocalizedUser $user)
120
    {
121 3
        return new self($user, self::PARSE_ERROR, 'Could not parse message!');
122
    }
123
124
    /**
125
     * @param LocalizedUser $user
126
     * @return MessageParserException
127
     */
128 3
    public static function cannotStartGameUserIsNotIn(LocalizedUser $user)
129
    {
130 3
        return new self($user, self::START_NOT_IN, 'You cannot start a game you are not in!');
131
    }
132
133
    /**
134
     * @param LocalizedUser $user
135
     * @return MessageParserException
136
     */
137 3
    public static function cannotLeaveGameUserIsNotIn(LocalizedUser $user)
138
    {
139 3
        return new self($user, self::LEAVE_NOT_IN, 'You cannot leave a game you are not in!');
140
    }
141
142
    /**
143
     * @param LocalizedUser $user
144
     * @return MessageParserException
145
     */
146 3
    public static function cannotFindGameForUser(LocalizedUser $user)
147
    {
148 3
        return new self(
149 2
            $user,
150 3
            self::GAME_NOT_FOUND,
151 1
            'The game or associated player was not found. Try starting/joining a game first.'
152 2
        );
153
    }
154
155
    /**
156
     * @param LocalizedUser $user
157
     * @return MessageParserException
158
     */
159 3
    public static function cannotCreateMultipleGames(LocalizedUser $user)
160
    {
161 3
        return new self($user, self::CREATE_MULTIPLE, 'You already have a game running!');
162
    }
163
164
    /**
165
     * @param LocalizedUser $user
166
     * @return MessageParserException
167
     */
168 6
    public static function invalidUser(LocalizedUser $user)
169
    {
170 6
        return new self($user, self::INVALID_USER, 'User is not valid!');
171
    }
172
}
173