Passed
Push — master ( e05dd3...01a0a7 )
by Nils
02:57
created

HealthFoundation::getHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Leankoala\HealthFoundation;
4
5
use GuzzleHttp\Client;
6
use Leankoala\HealthFoundation\Check\Check;
7
use Leankoala\HealthFoundation\Check\HttpClientAwareCheck;
8
use Leankoala\HealthFoundation\Check\CacheAwareCheck;
9
use Leankoala\HealthFoundation\Extenstion\Cache\Cache;
10
11
class HealthFoundation
12
{
13
    /**
14
     * @var Check[]
15
     */
16
    private $registeredChecks = [];
17
18
    /**
19
     * @var Client
20
     */
21
    private $httpClient;
22
23
    public function setHttpClient(Client $httpClient)
24
    {
25
        $this->httpClient = $httpClient;
26
    }
27
28
    /**
29
     * Return the current httpClient if set otherwise create one.
30
     *
31
     * @return Client
32
     */
33
    private function getHttpClient()
34
    {
35
        if (!$this->httpClient) {
36
            $this->httpClient = new Client();
37
        }
38
39
        return $this->httpClient;
40
    }
41
42
43
    /**
44
     * Return the current memory abstraction if set otherwise create one.
45
     *
46
     * @return Cache
47
     */
48
    private function getCache(CacheAwareCheck $check)
49
    {
50
        return new Cache($check->getIdentifier());
51
    }
52
53
    public function registerCheck(Check $check, $identifier = false, $description = "")
54
    {
55
        if ($check instanceof HttpClientAwareCheck) {
56
            $check->setHttpClient($this->getHttpClient());
57
        }
58
59
        if ($check instanceof CacheAwareCheck) {
60
            $check->setCache($this->getCache($check));
61
        }
62
63
        if ($identifier) {
64
            $this->registeredChecks[$identifier] = ['check' => $check, 'description' => $description];
65
        } else {
66
            $this->registeredChecks[] = ['check' => $check, 'description' => $description];
67
        }
68
    }
69
70
    public function runHealthCheck()
71
    {
72
        $runResult = new RunResult();
73
74
        foreach ($this->registeredChecks as $identifier => $checkInfos) {
75
            /** @var Check $check */
76
            $check = $checkInfos['check'];
77
78
            $checkResult = $check->run();
79
            $runResult->addResult($check, $checkResult, $identifier, $checkInfos['description']);
80
        }
81
82
        return $runResult;
83
    }
84
}
85