Passed
Push — master ( 2992b4...d315c2 )
by FX
03:02
created

RefreshService::refreshCampaign()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 25
nc 4
nop 1
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) 2017 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of ci-report.
7
 *
8
 * ci-report is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * ci-report is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace AppBundle\Service;
24
25
use AppBundle\Entity\Campaign;
26
use AppBundle\Entity\Status;
27
use AppBundle\Entity\Suite;
28
use Doctrine\Common\Persistence\ManagerRegistry;
29
30
/**
31
 * Refresh service class.
32
 *
33
 * @category  ci-report app
34
 *
35
 * @author    Francois-Xavier Soubirou <[email protected]>
36
 * @copyright 2017 Francois-Xavier Soubirou
37
 * @license   http://www.gnu.org/licenses/   GPLv3
38
 *
39
 * @see      https://www.ci-report.io
40
 */
41
class RefreshService
42
{
43
    /**
44
     * @var ManagerRegistry
45
     */
46
    private $doctrine;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param ManagerRegistry $doctrine Doctrine registry manager
52
     */
53
    public function __construct(ManagerRegistry $doctrine)
54
    {
55
        $this->doctrine = $doctrine;
56
    }
57
58
    /**
59
     * Refresh campaign status and tests results by scanning all suites.
60
     *
61
     * @param Campaign $campaign Campaign
62
     */
63
    public function refreshCampaign(Campaign $campaign): void
64
    {
65
        $repository = $this->doctrine->getRepository(Suite::class);
66
        $suites = $repository->findSuitesByCampaign($campaign);
67
68
        $passed = 0;
69
        $failed = 0;
70
        $errored = 0;
71
        $skipped = 0;
72
        $disabled = 0;
73
        $status = 0;
74
75
        foreach ($suites as $suite) {
76
            $passed += $suite->getPassed();
77
            $failed += $suite->getFailed();
78
            $errored += $suite->getErrored();
79
            $skipped += $suite->getSkipped();
80
            $disabled += $suite->getDisabled();
81
            $status = max($status, $suite->getStatus());
82
        }
83
        $campaign->setPassed($passed)
84
            ->setFailed($failed)
85
            ->setErrored($errored)
86
            ->setSkipped($skipped)
87
            ->setDisabled($disabled);
88
89
        // If no suite Status = Warning
90
        if (0 === $status) {
91
            $campaign->setStatus(Status::WARNING);
92
        } else {
93
            $campaign->setStatus($status);
94
        }
95
        $this->doctrine->getManager()->flush();
96
    }
97
}
98