NonStrictFilter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 46
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 26 4
A setCache() 0 3 1
A init() 0 3 1
1
<?php
2
3
namespace Leankoala\HealthFoundation\Filter\Basic;
4
5
use Leankoala\HealthFoundation\Check\CacheAwareCheck;
6
use Leankoala\HealthFoundation\Check\MetricAwareResult;
7
use Leankoala\HealthFoundation\Check\Result;
8
use Leankoala\HealthFoundation\Filter\BasicFilter;
9
use Leankoala\HealthFoundation\Extenstion\Cache\Cache;
10
11
class NonStrictFilter extends BasicFilter implements CacheAwareCheck
12
{
13
    private $maxErrorsInARow = 2;
14
15
    /**
16
     * @var Cache
17
     */
18
    private $cache;
19
20
    public function init($maxErrorsInARow = 2)
21
    {
22
        $this->maxErrorsInARow = $maxErrorsInARow;
23
    }
24
25
    public function run()
26
    {
27
        $result = $this->getCheck()->run();
28
29
        // @todo handle metric aware checks
30
31
        if ($result->getStatus() == Result::STATUS_FAIL) {
32
            $currentErrorsInARow = (int)$this->cache->get('errorsInARow');
33
            $currentErrorsInARow++;
34
35
            $this->cache->set('errorsInARow', $currentErrorsInARow);
36
37
            if ($currentErrorsInARow >= $this->maxErrorsInARow) {
38
                return $result;
39
            } else {
40
                if ($result instanceof MetricAwareResult) {
41
                    $metricResult = new MetricAwareResult(Result::STATUS_PASS, 'Passed because non-strict mode is activated. Failed with message: ' . $result->getMessage());
42
                    $metricResult->setMetric($result->getMetricValue(), $result->getMetricUnit());
43
                    return $metricResult;
44
                } else {
45
                    return new Result(Result::STATUS_PASS, 'Passed because non-strict mode is activated. Failed with message: ' . $result->getMessage());
46
                }
47
            }
48
        } else {
49
            $this->cache->set('errorsInARow', 0);
50
            return $result;
51
        }
52
    }
53
54
    public function setCache(Cache $cache)
55
    {
56
        $this->cache = $cache;
57
    }
58
}
59