Passed
Pull Request — develop (#5)
by ANTHONIUS
10:30
created

CachedCoverage::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
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
    public function __construct($namespace, array $codeCoverageOptions, Filter $filter)
29
    {
30
        $cache = new Cache($namespace);
31
32
        $cache->setCodeCoverageOptions($codeCoverageOptions);
33
        $cache->setFilter($filter);
34
        $cache->save();
35
36
        $this->cache = $cache;
37
    }
38
39
    public static function getSubscribedEvents()
40
    {
41
        return [
42
            CoverageEvent::REFRESH => 'onCoverageRefresh',
43
            CoverageEvent::START   => 'onCoverageStarted',
44
            CoverageEvent::STOP    => ['onCoverageStopped', 10],
45
        ];
46
    }
47
48
    /**
49
     * @return Cache
50
     */
51
    public function getCache()
52
    {
53
        return $this->cache;
54
    }
55
56
    /**
57
     * @param Cache $cache
58
     *
59
     * @return CachedCoverage
60
     */
61
    public function setCache(Cache $cache)
62
    {
63
        $this->cache = $cache;
64
65
        return $this;
66
    }
67
68
    public function onCoverageRefresh()
69
    {
70
        $this->cache->reset();
71
    }
72
73
    public function onCoverageStarted(CoverageEvent $event)
74
    {
75
        $cache = $this->cache;
76
77
        $cache->setCoverageId($event->getCoverageId());
78
        $cache->save();
79
    }
80
81
    public function onCoverageStopped(CoverageEvent $event)
82
    {
83
        $cache = $this->cache;
84
85
        $cache->readCache();
86
        $event->updateCoverage($cache->getCoverage());
87
    }
88
}
89