Completed
Push — master ( 6242f2...1d29a1 )
by Petr
03:18
created

User::addWaitstate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4286
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * @author: Rottenwood
4
 * @date  : 15.11.15 22:23
5
 */
6
7
namespace Rottenwood\KingdomBundle\Entity\Infrastructure;
8
9
use Doctrine\ORM\Mapping as ORM;
10
use Rottenwood\KingdomBundle\Entity\Room;
11
use Symfony\Component\Validator\Constraints as Assert;
12
use FOS\UserBundle\Model\User as BaseUser;
13
14
/**
15
 * @ORM\Table(name="users")
16
 * @ORM\Entity()
17
 * @ORM\InheritanceType("SINGLE_TABLE")
18
 * @ORM\DiscriminatorColumn(name="species", type="string")
19
 * @ORM\DiscriminatorMap({"human" = "Rottenwood\KingdomBundle\Entity\Human", "robot" = "Rottenwood\KingdomBundle\Entity\Robot"})
20
 */
21
abstract class User extends BaseUser
22
{
23
24
    const AVATAR_PATH = '/img/avatars/';
25
    const AVATAR_EXTENSION = 'jpg';
26
    const GENDER_MALE = 'male';
27
    const GENDER_FEMALE = 'female';
28
29
    /**
30
     * @ORM\Column(name="id", type="integer")
31
     * @ORM\Id
32
     * @ORM\GeneratedValue(strategy="AUTO")
33
     * @var int
34
     */
35
    protected $id;
36
37
    /**
38
     * Русское имя персонажа
39
     * @ORM\Column(name="character_name", type="string", length=25, unique=true)
40
     * @var string
41
     */
42
    protected $name;
43
44
    /**
45
     * Комната в которой находится персонаж
46
     * @ORM\ManyToOne(targetEntity="Rottenwood\KingdomBundle\Entity\Room")
47
     * @ORM\JoinColumn(name="room", referencedColumnName="id", nullable=false)
48
     * @var Room
49
     */
50
    protected $room;
51
52
    /**
53
     * Дата регистрации
54
     * @ORM\Column(name="register_date", type="datetime")
55
     * @var \DateTime
56
     */
57
    protected $registerDate;
58
59
    /**
60
     * Пол персонажа
61
     * @ORM\Column(name="gender", type="string", length=6, nullable=false)
62
     * @var string
63
     */
64
    protected $gender = self::GENDER_MALE;
65
66
    /**
67
     * Изображение персонажа (аватар)
68
     * @ORM\Column(name="avatar", type="string", length=255, nullable=true)
69
     * @var string
70
     */
71
    protected $avatar;
72
73
    /**
74
     * @ORM\Column(name="last_action_waitstate", type="integer", nullable=true)
75
     * @var int
76
     */
77
    protected $waitstate;
78
79
    public function __construct()
80
    {
81
        parent::__construct();
82
83
        $this->waitstate = time();
84
        $this->registerDate = new \DateTime();
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getName()
91
    {
92
        return $this->name;
93
    }
94
95
    /**
96
     * @param string $name
97
     */
98
    public function setName($name)
99
    {
100
        $this->name = $name;
101
    }
102
103
    /**
104
     * @return Room
105
     */
106
    public function getRoom()
107
    {
108
        return $this->room;
109
    }
110
111
    /**
112
     * @param Room $room
113
     */
114
    public function setRoom($room)
115
    {
116
        $this->room = $room;
117
    }
118
119
    /**
120
     * @return \DateTime
121
     */
122
    public function getRegisterDate()
123
    {
124
        return $this->registerDate;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getAvatar()
131
    {
132
        return $this->avatar ? sprintf('%s%s.%s', self::AVATAR_PATH, $this->avatar, self::AVATAR_EXTENSION) : '';
133
    }
134
135
    /**
136
     * @param string $avatar
137
     */
138
    public function setAvatar($avatar)
139
    {
140
        $this->avatar = $avatar;
141
    }
142
143
    /**
144
     * @param string $gender
145
     */
146
    public function setGender($gender)
147
    {
148
        $this->gender = $gender;
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function isMale()
155
    {
156
        return $this->gender == self::GENDER_MALE;
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function isFemale()
163
    {
164
        return $this->gender == self::GENDER_FEMALE;
165
    }
166
167
    /**
168
     * @return int
169
     */
170
    public function getWaitstate()
171
    {
172
        return $this->waitstate - time();
173
    }
174
175
    /**
176
     * @param int $waitSeconds
177
     */
178
    public function addWaitstate($waitSeconds)
179
    {
180
        if (!is_int($waitSeconds)) {
181
            throw new \RuntimeException('Waitstate can only accept integer as parameter');
182
        }
183
184
        $this->waitstate = time() + $waitSeconds;
185
    }
186
187
    /**
188
     * @return bool
189
     */
190
    public function isBusy()
191
    {
192
        return $this->waitstate > time();
193
    }
194
195
    /**
196
     * @Assert\GreaterThanOrEqual(value = 4, message = "Минимальная длина имени - 4 буквы")
197
     * @Assert\LessThanOrEqual(value = 20, message = "Максимальная длина имени - 20 букв")
198
     * @return int
199
     */
200
    public function isNameValid()
201
    {
202
        return mb_strlen($this->getLiteralUsername(), 'UTF-8');
203
    }
204
205
    /**
206
     * Очистка логина от спецсимволов для генерации имени
207
     * @return string
208
     */
209
    public function getLiteralUsername()
210
    {
211
        return preg_replace('/[^a-zA-Zа-яА-Я]/us', '', $this->getUsernameCanonical());
212
    }
213
}