User   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 3
cbo 1
dl 0
loc 197
rs 10

17 Methods

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