Passed
Pull Request — develop (#8)
by ANTHONIUS
04:35
created

Report::setReportProcessor()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-coverage-extension project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Behat\Coverage\Bridge;
15
16
use Doyo\Behat\Coverage\Event\ReportEvent;
17
use Doyo\Behat\Coverage\Exception\ReportProcessException;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
class Report implements EventSubscriberInterface
21
{
22
    /**
23
     * @var object
24
     */
25
    private $reportProcessor;
26
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * @var string
34
     */
35
    private $target;
36
37 6
    public static function getSubscribedEvents()
38
    {
39
        return [
40 6
            ReportEvent::PROCESS => 'onReportProcess',
41
        ];
42
    }
43
44
    /**
45
     * @return object
46
     */
47 1
    public function getReportProcessor()
48
    {
49 1
        return $this->reportProcessor;
50
    }
51
52
    /**
53
     * @param object $reportProcessor
54
     *
55
     * @return Report
56
     */
57 8
    public function setReportProcessor($reportProcessor)
58
    {
59 8
        $this->reportProcessor = $reportProcessor;
60
61 8
        return $this;
62
    }
63
64
    /**
65
     * @return string
66
     */
67 1
    public function getName()
68
    {
69 1
        return $this->name;
70
    }
71
72
    /**
73
     * @param string $name
74
     *
75
     * @return Report
76
     */
77 7
    public function setName($name)
78
    {
79 7
        $this->name = $name;
80
81 7
        return $this;
82
    }
83
84
    /**
85
     * @return string
86
     */
87 1
    public function getTarget()
88
    {
89 1
        return $this->target;
90
    }
91
92
    /**
93
     * @param string $target
94
     *
95
     * @return Report
96
     */
97 7
    public function setTarget($target)
98
    {
99 7
        $this->target = $target;
100
101 7
        return $this;
102
    }
103
104 6
    public function onReportProcess(ReportEvent $event)
105
    {
106 6
        $coverage = $event->getProcessor()->getCodeCoverage();
107 6
        $io       = $event->getIO();
108
        /* @todo process this error message */
109
        try {
110 6
            $this->reportProcessor->process($coverage, $this->target, $this->name);
111 5
            $io->text(
112 5
                sprintf(
113 5
                    '<info><comment>%s</comment> processed to: <comment>%s</comment></info>',
114 5
                    $this->name,
115 5
                    $this->target
116
                ));
117 1
        } catch (\Exception $e) {
118 1
            $message = sprintf(
119 1
                "failed to generate %s report. with Processor message:\n%s",
120 1
                $this->name,
121 1
                $e->getMessage()
122
            );
123 1
            $event->addException(new ReportProcessException($message));
124
        }
125
    }
126
}
127