ResultReporterManager::postPersist()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Overwatch\ResultBundle\Reporter;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use Overwatch\ResultBundle\Entity\TestResult;
7
use Overwatch\ResultBundle\Reporter\ResultReporterInterface;
8
9
/**
10
 * ResultReporterManager
11
 * The ResultReporterManager keeps a list of all known ResultReporters and also
12
 * acts as an event listener to the doctrine postPersist event. When a postPersist
13
 * event is fired for the TestResult class, this Manager will notify all ResultReporters
14
 * that have been added to it.
15
 */
16
class ResultReporterManager
17
{
18
    private $resultReporters = [];
19
20
    private $logger;
21
22 134
    public function __construct($logger)
23
    {
24 134
        $this->logger = $logger;
25 134
    }
26
27 134
    public function add(ResultReporterInterface $reporter)
28
    {
29 134
        $this->resultReporters[] = $reporter;
30 134
    }
31
32 129
    public function notifyAll(TestResult $result)
33
    {
34 129
        foreach ($this->resultReporters as $reporter) {
35
            try {
36 129
                $reporter->notify($result);
37 129
            } catch (\Exception $ex) {
38 1
                $this->logger->error('An error occurred whilst calling ResultReporter ' . \get_class($reporter) . ':' . $ex);
39
            }
40 129
        }
41 129
    }
42
43 127
    public function postPersist(LifecycleEventArgs $args)
44
    {
45 127
        $entity = $args->getEntity();
46
47 127
        if ($entity instanceof TestResult) {
48 127
            $this->notifyAll($entity);
49 127
        }
50 127
    }
51
}
52