Cache::call()   A
last analyzed

Complexity

Conditions 4
Paths 10

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 27
rs 9.7333
cc 4
nc 10
nop 0
1
<?php
2
namespace Health\Checks\Servers;
3
4
use Health\Checks\BaseCheck;
5
use Health\Checks\HealthCheckInterface;
6
use Illuminate\Support\Facades\Cache as CacheFacade;
7
8
/**
9
 * Cache Check
10
 */
11
class Cache extends BaseCheck implements HealthCheckInterface
12
{
13
14
    const DEFAULT_KEY = 'health-check';
15
16
    const DEFAULT_VALUE = 'health-check-value';
17
18
    const DEFAULT_MINUTES = 1;
19
20
    /**
21
     *
22
     * {@inheritdoc}
23
     * @see \Health\Checks\HealthCheckInterface::call()
24
     */
25
    public function call()
26
    {
27
        $builder = $this->getBuilder();
28
29
        $key = $this->getParam('key', self::DEFAULT_KEY);
30
        $value = $this->getParam('value', self::DEFAULT_VALUE);
31
        $minutes = $this->getParam('minutes', self::DEFAULT_MINUTES);
32
33
        try {
34
            $result = CacheFacade::add($key, $value, $minutes);
35
36
            if ($result) {
37
                $valueCached = CacheFacade::get($key);
38
39
                if ($valueCached !== $value) {
40
                    $builder->down()->withData("error", "Value  mismatch.");
41
                }
42
            } else {
43
                $builder->down()->withData("error", "Could not add key.");
44
            }
45
46
            CacheFacade::delete($key);
47
        } catch (\Exception $exception) {
48
            $builder->down()->withData("error", "Cache Error - " . $exception->getMessage());
49
        }
50
51
        return $builder->build();
52
    }
53
}
54