Completed
Branch v1.x-dev (48843d)
by Benjamin
06:26
created

Rule::setReadOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Obblm\Core\Entity;
4
5
use Symfony\Component\Serializer\Annotation\Ignore;
6
use Obblm\Core\Repository\RuleRepository;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
11
/**
12
 * @ORM\Entity(repositoryClass=RuleRepository::class)
13
 * @ORM\Table(name="obblm_rule")
14
 */
15
class Rule
16
{
17
    /**
18
     * @ORM\Id()
19
     * @ORM\GeneratedValue()
20
     * @ORM\Column(type="integer")
21
     */
22
    private $id;
23
24
    /**
25
     * @ORM\Column(type="string", unique=true, length=255)
26
     */
27
    private $ruleKey;
28
29
    /**
30
     * @ORM\Column(type="string", length=255, nullable=true)
31
     */
32
    private $ruleDirectory;
33
34
    /**
35
     * @ORM\Column(type="string", length=255)
36
     */
37
    private $template;
38
39
    /**
40
     * @ORM\Column(type="string", length=255)
41
     */
42
    private $name;
43
44
    /**
45
     * @ORM\Column(type="string", length=255, nullable=true)
46
     */
47
    private $description;
48
49
    /**
50
     * @ORM\Column(type="array", nullable=true)
51
     */
52
    private $rule = [];
53
54
    /**
55
     * @ORM\Column(type="boolean")
56
     */
57
    private $postBb2020;
58
59
    /**
60
     * @ORM\Column(type="boolean")
61
     */
62
    private $readOnly;
63
64
    /**
65
     * @ORM\OneToMany(targetEntity=Team::class, mappedBy="rule")
66
     * @Ignore()
67
     */
68
    private $teams;
69
70
    protected $injuryTable = [];
71
72 9
    public function __construct()
73
    {
74 9
        $this->postBb2020 = false;
75 9
        $this->readOnly = false;
76 9
        $this->teams = new ArrayCollection();
77 9
    }
78
79 6
    public function getId(): ?int
80
    {
81 6
        return $this->id;
82
    }
83
84 7
    public function getRuleKey(): ?string
85
    {
86 7
        return $this->ruleKey;
87
    }
88
89 9
    public function setRuleKey(string $ruleKey): self
90
    {
91 9
        $this->ruleKey = $ruleKey;
92
93 9
        return $this;
94
    }
95
96
    public function getRuleDirectory(): ?string
97
    {
98
        return $this->ruleDirectory;
99
    }
100
101 7
    public function setRuleDirectory(string $ruleDirectory): self
102
    {
103 7
        $this->ruleDirectory = $ruleDirectory;
104
105 7
        return $this;
106
    }
107
108 1
    public function getTemplate(): ?string
109
    {
110 1
        return $this->template;
111
    }
112
113 8
    public function setTemplate(string $template): self
114
    {
115 8
        $this->template = $template;
116
117 8
        return $this;
118
    }
119
120
    public function getName(): ?string
121
    {
122
        return $this->name;
123
    }
124
125 7
    public function setName(string $name): self
126
    {
127 7
        $this->name = $name;
128
129 7
        return $this;
130
    }
131
132
    public function getDescription(): ?string
133
    {
134
        return $this->description;
135
    }
136
137
    public function setDescription(?string $description): self
138
    {
139
        $this->description = $description;
140
141
        return $this;
142
    }
143
144 4
    public function getRule(): ?array
145
    {
146 4
        return $this->rule;
147
    }
148
149 7
    public function setRule(?array $rule): self
150
    {
151 7
        $this->rule = $rule;
152 7
        $this->constructInjuryTable($rule);
153
154 7
        return $this;
155
    }
156
157 1
    public function getPostBb2020(): ?bool
158
    {
159 1
        return $this->postBb2020;
160
    }
161
162 1
    public function isPostBb2020(): ?bool
163
    {
164 1
        return $this->getPostBb2020();
165
    }
166
167 7
    public function setPostBb2020(bool $postBb2020): self
168
    {
169 7
        $this->postBb2020 = $postBb2020;
170
171 7
        return $this;
172
    }
173
174
    public function getReadOnly(): ?bool
175
    {
176
        return $this->readOnly;
177
    }
178
179
    public function isReadOnly(): ?bool
180
    {
181
        return $this->getReadOnly();
182
    }
183
184 7
    public function setReadOnly(bool $readOnly): self
185
    {
186 7
        $this->readOnly = $readOnly;
187
188 7
        return $this;
189
    }
190
191
    /**
192
     * @return Collection|Team[]
193
     * @Ignore()
194
     */
195
    public function getTeams(): Collection
196
    {
197
        return $this->teams;
198
    }
199
200
    public function addTeam(Team $team): self
201
    {
202
        if (!$this->teams->contains($team)) {
203
            $this->teams[] = $team;
204
            $team->setRule($this);
205
        }
206
207
        return $this;
208
    }
209
210
    public function removeTeam(Team $team): self
211
    {
212
        if ($this->teams->contains($team)) {
213
            $this->teams->removeElement($team);
214
            // set the owning side to null (unless already changed)
215
            if ($team->getRule() === $this) {
216
                $team->setRule(null);
217
            }
218
        }
219
220
        return $this;
221
    }
222
223
    public function __toString(): ?string
224
    {
225
        return $this->ruleKey;
226
    }
227
228
    /**
229
     * Methods
230
     */
231
232
    /**
233
     * Construct Injury Table
234
     *
235
     * @param array $rule
236
     *
237
     */
238 7
    protected function constructInjuryTable($rule)
239
    {
240 7
        foreach ($rule['injuries'] as $injuryKey => $injury) {
241 7
            if (isset($injury['from']) && isset($injury['to'])) {
242 7
                for ($key = $injury['from']; $key <= $injury['to']; $key++) {
243 7
                    $this->injuryTable[$key] = $injuryKey;
244
                }
245 7
            } elseif (isset($injury['from'])) {
246 7
                $this->injuryTable[$injury['from']] = $injuryKey;
247
            }
248
        }
249 7
    }
250
251
    /**
252
     * Get Experience Level For Experience Value
253
     *
254
     * @param integer $experience
255
     *
256
     * @return string|boolean
257
     */
258
    public function getExperienceLevelForValue($experience)
259
    {
260
        $datas = $this->getRule();
261
        ksort($datas['experience']);
262
        $last = false;
263
        foreach ($datas['experience'] as $key => $level) {
264
            if ($experience >= $key) {
265
                $last = $level;
266
            }
267
        }
268
        return $last;
269
    }
270
271
    /**
272
     * Get Injury For Value
273
     *
274
     * @param integer $value
275
     *
276
     * @return array|boolean
277
     */
278
    public function getInjury($value)
279
    {
280
        return (isset($this->injuryTable[$value])) ? array(
281
            'key_name' => $this->injuryTable[$value],
282
            'effect' => $this->getInjuryEffect($this->injuryTable[$value])
283
        ) : false ;
284
    }
285
286
    /**
287
     * Get Injury Effect For Injury Key Name
288
     *
289
     * @param string $keyName
290
     *
291
     * @return array|boolean
292
     */
293
    public function getInjuryEffect($keyName)
294
    {
295
        $datas = $this->getRule();
296
        return ($datas['injuries'][$keyName]) ? $datas['injuries'][$keyName]['effects'] : false;
297
    }
298
}
299