ModuleUser::setRating()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: device
5
 * Date: 23.02.16
6
 * Time: 10:00
7
 */
8
9
namespace AppBundle\Entity;
10
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Criteria;
13
use Doctrine\ORM\Mapping as ORM;
14
15
/**
16
 * ModuleUser
17
 *
18
 * @ORM\Table(name="module_user")
19
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ModuleUserRepository")
20
 */
21
class ModuleUser implements \JsonSerializable
22
{
23
    const STATUS_ACTIVE = 'active';
24
    const STATUS_SUCCESS = 'success';
25
    const STATUS_FAILED = 'failed';
26
27
    /**
28
     * @ORM\Id
29
     * @ORM\GeneratedValue
30
     * @ORM\Column(type="integer")
31
     */
32
    private $id;
33
34
35
    /**
36
     * @ORM\Column(type="string")
37
     */
38
    private $status;
39
40
    /**
41
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\PassModule", mappedBy="moduleUser", cascade={"remove"})
42
     */
43
    private $passModules;
44
45
    /**
46
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="modulesUser")
47
     */
48
    private $user;
49
50
    /**
51
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Module", inversedBy="modulesUser")
52
     */
53
    private $module;
54
55
    /**
56
     * @ORM\Column(type="float")
57
     */
58
    private $rating;
59
60 1
    public function jsonSerialize()
61
    {
62
        return [
63
            'user' => $this->getUser(),
64
            'module' => $this->getModule(),
65
            'passModule' => $this->getPassModules()->last()
66 1
        ];
67
    }
68
69
70 27
    public function __construct()
71
    {
72 27
        $this->passModules = new ArrayCollection();
73 27
        $this->status = self::STATUS_ACTIVE;
74 27
        $this->rating = 0;
75 27
    }
76
77
    /**
78
     * @return mixed
79
     */
80 1
    public function getPassModules()
81
    {
82 1
        return $this->passModules;
83
    }
84
85
    /**
86
     * @param PassModule $passModule
87
     * @return $this
88
     */
89
    public function addPassModule(PassModule $passModule)
90
    {
91
        $this->passModules->add($passModule);
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param PassModule $passModule
98
     */
99
    public function removePassModule(PassModule $passModule)
100
    {
101
        $this->passModules->removeElement($passModule);
102
    }
103
104
    /**
105
     * @return mixed
106
     */
107 1
    public function getId()
108
    {
109 1
        return $this->id;
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115 1
    public function getAttempts()
116
    {
117 1
        return count($this->getInactivePassModules());
118
    }
119
120
    /**
121
     * @return mixed
122
     */
123
    public function getIsActive()
124
    {
125
        return ($this->status == self::STATUS_ACTIVE);
126
    }
127
128
129
    /**
130
     * @return mixed
131
     */
132
    public function getUser()
133
    {
134
        return $this->user;
135
    }
136
137
    /**
138
     * @param mixed $user
139
     * @return $this
140
     */
141 27
    public function setUser(User $user = null)
142
    {
143 27
        $this->user = $user;
144
145 27
        return $this;
146
    }
147
148
    /**
149
     * @return mixed
150
     */
151 4
    public function getModule()
152
    {
153 4
        return $this->module;
154
    }
155
156
    /**
157
     * @param mixed $module
158
     * @return $this
159
     */
160 27
    public function setModule(Module $module = null)
161
    {
162 27
        $this->module = $module;
163
164 27
        return $this;
165
    }
166
167
    /**
168
     * @return mixed
169
     */
170
    public function getRating()
171
    {
172
        return $this->rating;
173
    }
174
175
    /**
176
     * @param $rating
177
     * @return $this
178
     */
179 27
    public function setRating($rating)
180
    {
181 27
        $this->rating = $rating;
182 27
        return $this;
183
    }
184
185
186 2
    public function getCountPassModules()
187
    {
188 2
        return count($this->passModules);
189
    }
190
191 1
    public function getInactivePassModules()
192
    {
193 1
        $criteria = Criteria::create();
194 1
        $criteria->where(Criteria::expr()->eq('isActive', false));
195
196 1
        return $this->passModules->matching($criteria);
197
    }
198
199
    /**
200
     * @return mixed
201
     */
202 1
    public function getStatus()
203
    {
204 1
        return $this->status;
205
    }
206
207
    /**
208
     * @param mixed $status
209
     */
210 27
    public function setStatus($status)
211
    {
212 27
        $this->status = $status;
213
214 27
        return $this;
215
    }
216
217 1
    public function getLastModule()
218
    {
219 1
        return $this->passModules->last();
220
    }
221
222
}
223