Test Failed
Push — master ( 13dcda...44dfad )
by Antonio Carlos
04:33
created

Service   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 46.67%

Importance

Changes 0
Metric Value
dl 0
loc 140
ccs 14
cts 30
cp 0.4667
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A checkResources() 0 4 1
A health() 0 4 1
A checkResource() 0 4 1
A getResources() 0 4 1
A getSilentChecker() 0 6 1
A resource() 0 4 1
A setAction() 0 4 1
A makeString() 0 9 2
A string() 0 13 2
1
<?php
2
3
namespace PragmaRX\Health;
4
5
use PragmaRX\Health\Support\Cache;
6
use PragmaRX\Health\Support\ResourceChecker;
7
8
class Service
9
{
10
    /**
11
     * @var ResourceChecker
12
     */
13
    private $resourceChecker;
14
15
    /**
16
     * @var Cache
17
     */
18
    private $cache;
19
20
    /**
21
     * Service constructor.
22
     *
23
     * @param ResourceChecker $resourceChecker
24
     * @param Cache $cache
25
     */
26 10
    public function __construct(ResourceChecker $resourceChecker, Cache $cache)
27
    {
28 10
        $this->resourceChecker = $resourceChecker;
29
30 10
        $this->cache = $cache;
31 10
    }
32
33
    /**
34
     * Check Resources.
35
     *
36
     * @param bool $force
37
     * @return \Illuminate\Support\Collection
38
     * @throws \Exception
39
     */
40 8
    public function checkResources($force = false)
41
    {
42 8
        return $this->resourceChecker->checkResources($force);
43
    }
44
45
    /**
46
     * Check one resource.
47
     *
48
     * @param $slug
49
     * @return array
50
     * @throws \Exception
51
     */
52
    public function checkResource($slug)
53
    {
54
        return $this->resourceChecker->checkResource($slug);
55
    }
56
57
    /**
58
     * Get services health.
59
     *
60
     * @return mixed
61
     * @throws \Exception
62
     */
63 2
    public function health()
64
    {
65 2
        return $this->checkResources();
66
    }
67
68
    /**
69
     * Get resources.
70
     *
71
     * @return mixed
72
     * @throws \Exception
73
     */
74
    public function getResources()
75
    {
76
        return $this->resourceChecker->getResources();
77
    }
78
79
    /**
80
     * Get a silent checker and notifier closure.
81
     *
82
     * @return \Closure
83
     */
84 1
    public function getSilentChecker()
85
    {
86
        return function () {
87 1
            return $this->checkResources();
88 1
        };
89
    }
90
91
    /**
92
     * Make a string result of all resources.
93
     *
94
     * @param $string
95
     * @param $checkSystem
96
     * @return string
97
     */
98
    private function makeString($string, $checkSystem)
99
    {
100
        return (
101
            $string .
102
            ($checkSystem
103
                ? config('health.string.ok')
104
                : config('health.string.fail'))
105
        );
106
    }
107
108
    /**
109
     * Check and get a resource.
110
     *
111
     * @param $name
112
     * @return mixed
113
     * @throws \Exception
114
     */
115
    public function resource($slug)
116
    {
117
        return $this->checkResource($slug);
118
    }
119
120
    /**
121
     * Set the action.
122
     *
123
     * @param $action
124
     */
125 1
    public function setAction($action)
126
    {
127 1
        $this->resourceChecker->setCurrentAction($action);
128 1
    }
129
130
    /**
131
     * @return mixed
132
     * @throws \Exception
133
     */
134
    public function string()
135
    {
136
        return collect($this->health())->reduce(function ($current, $resource) {
137
            return (
138
                $current .
139
                ($current ? config('health.string.glue') : '') .
140
                $this->makeString(
141
                    $resource->abbreviation,
142
                    $resource->isHealthy()
143
                )
144
            );
145
        });
146
    }
147
}
148