Completed
Push — master ( 546355...e7f15d )
by FX
06:52
created

Campaign::setFromDTO()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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