Completed
Push — master ( ade58c...3e282b )
by Rémi
03:35
created

MessageParserException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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