Passed
Branch master (2b33dd)
by FX
03:15
created

Campaign::getDuration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2017 Francois-Xavier Soubirou.
4
 *
5
 * This file is part of ci-report.
6
 *
7
 * ci-report is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * ci-report is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
declare(strict_types=1);
21
22
namespace AppBundle\Entity;
23
24
use DateTime;
25
use Doctrine\ORM\Mapping as ORM;
26
use Gedmo\Mapping\Annotation as Gedmo;
27
use JMS\Serializer\Annotation as Serializer;
28
29
/**
30
 * Campaign entity class.
31
 *
32
 * @category  ci-report app
33
 *
34
 * @author    Francois-Xavier Soubirou <[email protected]>
35
 * @copyright 2017 Francois-Xavier Soubirou
36
 * @license   http://www.gnu.org/licenses/   GPLv3
37
 *
38
 * @see      https://www.ci-report.io
39
 *
40
 * @ORM\Table(name="cir_campaign")
41
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CampaignRepository")
42
 */
43
class Campaign
44
{
45
    /**
46
     * @var int
47
     *
48
     * @ORM\Column(name="id", type="integer")
49
     * @ORM\Id
50
     * @ORM\GeneratedValue(strategy="AUTO")
51
     *
52
     * @Serializer\Exclude
53
     */
54
    private $id;
55
56
    /**
57
     * Tests warning limit.
58
     *
59
     * @var int
60
     *
61
     * @ORM\Column(name="warning", type="smallint")
62
     *
63
     * @Serializer\Groups({"public", "private"})
64
     */
65
    private $warning;
66
67
    /**
68
     * Tests success limit.
69
     *
70
     * @var int
71
     *
72
     * @ORM\Column(name="success", type="smallint")
73
     *
74
     * @Serializer\Groups({"public", "private"})
75
     */
76
    private $success;
77
78
    /**
79
     * Total number of passed tests from all testsuites.
80
     *
81
     * @var int
82
     *
83
     * @ORM\Column(name="passed", type="integer")
84
     *
85
     * @Serializer\Groups({"public", "private"})
86
     */
87
    private $passed;
88
89
    /**
90
     * Total number of failed tests from all testsuites.
91
     *
92
     * @var int
93
     *
94
     * @ORM\Column(name="failed", type="integer")
95
     *
96
     * @Serializer\Groups({"public", "private"})
97
     */
98
    private $failed;
99
100
    /**
101
     * Total number of errored tests from all testsuites.
102
     *
103
     * @var int
104
     *
105
     * @ORM\Column(name="errored", type="integer")
106
     *
107
     * @Serializer\Groups({"public", "private"})
108
     */
109
    private $errored;
110
111
    /**
112
     * Total number of skipped tests from all testsuites.
113
     *
114
     * @var int
115
     *
116
     * @ORM\Column(name="skipped", type="integer")
117
     *
118
     * @Serializer\Groups({"public", "private"})
119
     */
120
    private $skipped;
121
122
    /**
123
     * Total number of disabled tests from all testsuites.
124
     *
125
     * @var int
126
     *
127
     * @ORM\Column(name="disabled", type="integer")
128
     *
129
     * @Serializer\Groups({"public", "private"})
130
     */
131
    private $disabled;
132
133
    /**
134
     * Start Date time of the campaign in ISO 8601 format (2017-07-01T12:30:01+02:00).
135
     *
136
     * @var DateTime
137
     *
138
     * @ORM\Column(name="start", type="datetime")
139
     *
140
     * @Serializer\Groups({"public", "private"})
141
     */
142
    protected $start;
143
144
    /**
145
     * End Date time of the campaign in ISO 8601 format (2017-07-01T12:30:01+02:00).
146
     *
147
     * @var DateTime
148
     *
149
     * @ORM\Column(name="end", type="datetime", nullable=true)
150
     *
151
     * @Serializer\Groups({"public", "private"})
152
     */
153
    protected $end;
154
155
    /**
156
     * @Gedmo\SortablePosition
157
     *
158
     * @ORM\Column(name="position", type="integer")
159
     * @Serializer\Exclude
160
     */
161
    private $position;
162
163
    /**
164
     * @var Project
165
     *
166
     * @Gedmo\SortableGroup
167
     *
168
     * @ORM\ManyToOne(targetEntity="Project")
169
     * @ORM\JoinColumn(name="project_id", referencedColumnName="id", nullable=false, onDelete="cascade")
170
     *
171
     * @Serializer\Exclude
172
     */
173
    private $project;
174
175
    /**
176
     * Constructor.
177
     *
178
     * @param Project $project
179
     */
180
    public function __construct(Project $project)
181
    {
182
        $this->setWarning($project->getWarning());
183
        $this->setSuccess($project->getSuccess());
184
        $this->setProject($project);
185
    }
186
187
    /**
188
     * Get id.
189
     *
190
     * @return int
191
     */
192
    public function getId(): int
193
    {
194
        return $this->id;
195
    }
196
197
    /**
198
     * Set warning limit.
199
     *
200
     * @param int $warning Warning limit
201
     *
202
     * @return Campaign
203
     */
204
    public function setWarning(int $warning): Campaign
205
    {
206
        $this->warning = $warning;
207
208
        return $this;
209
    }
210
211
    /**
212
     * Get warning limit.
213
     *
214
     * @return int
215
     */
216
    public function getWarning(): int
217
    {
218
        return $this->warning;
219
    }
220
221
    /**
222
     * Set success limit.
223
     *
224
     * @param int $success Success limit
225
     *
226
     * @return Campaign
227
     */
228
    public function setSuccess(int $success): Campaign
229
    {
230
        $this->success = $success;
231
232
        return $this;
233
    }
234
235
    /**
236
     * Get success limit.
237
     *
238
     * @return int
239
     */
240
    public function getSuccess(): int
241
    {
242
        return $this->success;
243
    }
244
245
    /**
246
     * Set passed tests count.
247
     *
248
     * @param int $passed Passed tests
249
     *
250
     * @return Campaign
251
     */
252
    public function setpassed(int $passed): Campaign
253
    {
254
        $this->passed = $passed;
255
256
        return $this;
257
    }
258
259
    /**
260
     * Get passed tests count.
261
     *
262
     * @return int
263
     */
264
    public function getPassed(): int
265
    {
266
        return $this->passed;
267
    }
268
269
    /**
270
     * Set failed tests count.
271
     *
272
     * @param int $failed Failed tests
273
     *
274
     * @return Campaign
275
     */
276
    public function setFailed(int $failed): Campaign
277
    {
278
        $this->failed = $failed;
279
280
        return $this;
281
    }
282
283
    /**
284
     * Get failed tests count.
285
     *
286
     * @return int
287
     */
288
    public function getFailed(): int
289
    {
290
        return $this->failed;
291
    }
292
293
    /**
294
     * Set errored tests count.
295
     *
296
     * @param int $errored Errored tests
297
     *
298
     * @return Campaign
299
     */
300
    public function setErrored(int $errored): Campaign
301
    {
302
        $this->errored = $errored;
303
304
        return $this;
305
    }
306
307
    /**
308
     * Get errored tests count.
309
     *
310
     * @return int
311
     */
312
    public function getErrored(): int
313
    {
314
        return $this->errored;
315
    }
316
317
    /**
318
     * Set skipped tests count.
319
     *
320
     * @param int $skipped Skipped tests
321
     *
322
     * @return Campaign
323
     */
324
    public function setSkipped(int $skipped): Campaign
325
    {
326
        $this->skipped = $skipped;
327
328
        return $this;
329
    }
330
331
    /**
332
     * Get skipped tests count.
333
     *
334
     * @return int
335
     */
336
    public function getSkipped(): int
337
    {
338
        return $this->skipped;
339
    }
340
341
    /**
342
     * Set disabled tests count.
343
     *
344
     * @param int $disabled Disabled tests
345
     *
346
     * @return Campaign
347
     */
348
    public function setDisabled(int $disabled): Campaign
349
    {
350
        $this->disabled = $disabled;
351
352
        return $this;
353
    }
354
355
    /**
356
     * Get disabled tests count.
357
     *
358
     * @return int
359
     */
360
    public function getDisabled(): int
361
    {
362
        return $this->disabled;
363
    }
364
365
    /**
366
     * Set start datetime of campaign.
367
     *
368
     * @param DateTime $datetime start datetime of campaign.
369
     *
370
     * @return Campaign
371
     */
372
    public function setStart(DateTime $datetime): Campaign
373
    {
374
        $this->start = $datetime;
375
376
        return $this;
377
    }
378
379
    /**
380
     * Get start datetime of campaign.
381
     *
382
     * @return DateTime
383
     */
384
    public function getStart(): Datetime
385
    {
386
        return $this->start;
387
    }
388
389
    /**
390
     * Set end datetime of campaign.
391
     *
392
     * @param DateTime $datetime end datetime of campaign.
393
     *
394
     * @return Campaign
395
     */
396
    public function setEnd(DateTime $datetime): Campaign
397
    {
398
        $this->end = $datetime;
399
400
        return $this;
401
    }
402
403
    /**
404
     * Get end datetime of campaign.
405
     *
406
     * @return DateTime
407
     */
408
    public function getEnd(): ?Datetime
409
    {
410
        return $this->end;
411
    }
412
413
    /**
414
     * Set order.
415
     *
416
     * @param int $position The order.
417
     *
418
     * @return Campaign
419
     */
420
    public function setPosition(int $position): Campaign
421
    {
422
        $this->position = $position;
423
424
        return $this;
425
    }
426
427
    /**
428
     * Get position.
429
     *
430
     * @return int
431
     */
432
    public function getPosition(): int
433
    {
434
        return $this->position;
435
    }
436
437
    /**
438
     * Get reference id (Incremental integer).
439
     *
440
     * @return int
441
     *
442
     * @Serializer\VirtualProperty
443
     * @Serializer\SerializedName("refid")
444
     * @Serializer\Type("int")
445
     * @Serializer\Groups({"public", "private"})
446
     */
447
    public function getRefid(): int
448
    {
449
        return $this->position + 1;
450
    }
451
452
    /**
453
     * Set project.
454
     *
455
     * @param Project $project
456
     *
457
     * @return Campaign
458
     */
459
    public function setProject(Project $project): Campaign
460
    {
461
        $this->project = $project;
462
463
        return $this;
464
    }
465
466
    /**
467
     * Get project.
468
     *
469
     * @return Project
470
     */
471
    public function getProject(): Project
472
    {
473
        return $this->project;
474
    }
475
476
    /**
477
     * Get enabled tests.
478
     *
479
     * @return int
480
     */
481
    public function getEnabled(): int
482
    {
483
        return $this->passed
484
            + $this->failed
485
            + $this->errored
486
            + $this->skipped;
487
    }
488
489
    /**
490
     * Get percentage of successful tests.
491
     *
492
     * @return float
493
     */
494
    public function getPercentage(): float
495
    {
496
        if ($this->getEnabled() !== 0) {
497
            return round($this->passed / $this->getEnabled() * 100);
498
        }
499
500
        return 0;
501
    }
502
503
    /**
504
     * Get campaign status.
505
     *
506
     * @return int Status::FAILED|Status::WARNING|Status::SUCCESS
507
     */
508
    public function getStatus(): int
509
    {
510
        if ($this->getPercentage() < $this->getWarning()) {
511
            return Status::FAILED;
512
        }
513
        if ($this->getPercentage() < $this->getSuccess()) {
514
            return Status::WARNING;
515
        }
516
517
        return Status::SUCCESS;
518
    }
519
}
520