RefreshService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A refreshSuite() 0 23 2
A refreshCampaign() 0 36 4
A __construct() 0 3 1
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 App\Service;
24
25
use App\Entity\Campaign;
26
use App\Entity\Status;
27
use App\Entity\Suite;
28
use App\Entity\Test;
29
use Doctrine\Common\Persistence\ManagerRegistry;
30
31
/**
32
 * Refresh service 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
class RefreshService
43
{
44
    /**
45
     * @var ManagerRegistry
46
     */
47
    private $doctrine;
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param ManagerRegistry $doctrine Doctrine registry manager
53
     */
54
    public function __construct(ManagerRegistry $doctrine)
55
    {
56
        $this->doctrine = $doctrine;
57
    }
58
59
    /**
60
     * Refresh campaign status and tests results by scanning all suites.
61
     *
62
     * @param Campaign $campaign Campaign
63
     * @param bool     $all      Refresh Campaign and suites
64
     */
65
    public function refreshCampaign(Campaign $campaign, bool $all = false): void
66
    {
67
        $repository = $this->doctrine->getRepository(Suite::class);
68
        $suites = $repository->findSuitesByCampaign($campaign);
69
70
        $passed = 0;
71
        $failed = 0;
72
        $errored = 0;
73
        $skipped = 0;
74
        $disabled = 0;
75
        $status = 0;
76
77
        foreach ($suites as $suite) {
78
            if ($all) {
79
                $this->refreshSuite($suite);
80
            }
81
            $passed += $suite->getPassed();
82
            $failed += $suite->getFailed();
83
            $errored += $suite->getErrored();
84
            $skipped += $suite->getSkipped();
85
            $disabled += $suite->getDisabled();
86
            $status = max($status, $suite->getStatus());
87
        }
88
        $campaign->setPassed($passed)
89
            ->setFailed($failed)
90
            ->setErrored($errored)
91
            ->setSkipped($skipped)
92
            ->setDisabled($disabled);
93
94
        // If no suite Status = Unknown
95
        if (0 === $status) {
96
            $campaign->setStatus(Status::UNKNOWN);
97
        } else {
98
            $campaign->setStatus($status);
99
        }
100
        $this->doctrine->getManager()->flush();
101
    }
102
103
    /**
104
     * Refresh suite status and tests results by scanning all tests.
105
     *
106
     * @param Suite $suite Suite
107
     */
108
    public function refreshSuite(Suite $suite): void
109
    {
110
        $repository = $this->doctrine->getRepository(Test::class);
111
        $tests = $repository->findTestsBySuite($suite);
112
113
        $passed = 0;
114
        $failed = 0;
115
        $errored = 0;
116
        $skipped = 0;
117
118
        foreach ($tests as $test) {
119
            $passed += $test->getPassed();
120
            $failed += $test->getFailed();
121
            $errored += $test->getErrored();
122
            $skipped += $test->getSkipped();
123
        }
124
        $suite->setPassed($passed)
125
            ->setFailed($failed)
126
            ->setErrored($errored)
127
            ->setSkipped($skipped);
128
        // Duration is not re-evaluate.
129
130
        $this->doctrine->getManager()->flush();
131
    }
132
}
133