Passed
Pull Request — develop (#5)
by ANTHONIUS
04:29
created

CachedCoverage::setCache()   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
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Bridge\CodeCoverage\Cache;
17
use Doyo\Behat\Coverage\Event\CoverageEvent;
18
use SebastianBergmann\CodeCoverage\Filter;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
class CachedCoverage implements EventSubscriberInterface
22
{
23
    /**
24
     * @var Cache
25
     */
26
    private $cache;
27
28 8
    public function __construct($namespace, array $codeCoverageOptions, Filter $filter)
29
    {
30 8
        $cache = new Cache($namespace);
31
32 8
        $cache->setCodeCoverageOptions($codeCoverageOptions);
33 8
        $cache->setFilter($filter);
34 8
        $cache->save();
35
36 8
        $this->cache = $cache;
37
    }
38
39 3
    public static function getSubscribedEvents()
40
    {
41
        return [
42 3
            CoverageEvent::REFRESH => 'onCoverageRefresh',
43 1
            CoverageEvent::START   => 'onCoverageStarted',
44 1
            CoverageEvent::STOP    => ['onCoverageStopped', 10],
45
        ];
46
    }
47
48
    /**
49
     * @return Cache
50
     */
51 1
    public function getCache()
52
    {
53 1
        return $this->cache;
54
    }
55
56
    /**
57
     * @param Cache $cache
58
     *
59
     * @return CachedCoverage
60
     */
61 6
    public function setCache(Cache $cache)
62
    {
63 6
        $this->cache = $cache;
64
65 6
        return $this;
66
    }
67
68 2
    public function onCoverageRefresh()
69
    {
70 2
        $this->cache->reset();
71
    }
72
73 2
    public function onCoverageStarted(CoverageEvent $event)
74
    {
75 2
        $cache = $this->cache;
76
77 2
        $cache->setCoverageId($event->getCoverageId());
78 2
        $cache->save();
79
    }
80
81 2
    public function onCoverageStopped(CoverageEvent $event)
82
    {
83 2
        $cache = $this->cache;
84
85 2
        $cache->readCache();
86 2
        $event->updateCoverage($cache->getCoverage());
87
    }
88
}
89