Test Failed
Push — master ( 6cb9f0...7deac3 )
by Antonio Carlos
07:23 queued 02:51
created

src/Service.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 2
    public function __construct(ResourceChecker $resourceChecker, Cache $cache)
27
    {
28 2
        $this->resourceChecker = $resourceChecker;
29
30 2
        $this->cache = $cache;
31 2
    }
32
33
    /**
34
     * Check Resources.
35
     *
36
     * @param bool $force
37
     * @return \Illuminate\Support\Collection
38
     * @throws \Exception
39
     */
40
    public function checkResources($force = false)
41
    {
42
        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
    public function health()
64
    {
65
        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 one resource.
81
     *
82
     * @param $slug
83
     * @return mixed
84
     * @throws \Exception
85
     */
86
    private function getResource($slug)
0 ignored issues
show
This method is not used, and could be removed.
Loading history...
87
    {
88
        return $this->resourceChecker->getResourceBySlug($slug);
89
    }
90
91
    /**
92
     * Get a silent checker and notifier closure.
93
     *
94
     * @return \Closure
95
     */
96
    public function getSilentChecker()
97
    {
98
        return function () {
99
            return $this->checkResources();
100
        };
101
    }
102
103
    /**
104
     * Check if server is healthy.
105
     *
106
     * @return mixed
107
     * @throws \Exception
108
     */
109
    public function isHealthy()
110
    {
111
        return $this->checkResources()->reduce(function ($carry, $item) {
112
            return $carry && $item->isHealthy();
113
        }, true);
114
    }
115
116
    /**
117
     * Make a string result of all resources.
118
     *
119
     * @param $string
120
     * @param $checkSystem
121
     * @return string
122
     */
123
    private function makeString($string, $checkSystem)
124
    {
125
        return (
126
            $string .
127
            ($checkSystem
128
                ? config('health.string.ok')
129
                : config('health.string.fail'))
130
        );
131
    }
132
133
    /**
134
     * Check and get a resource.
135
     *
136
     * @param $name
137
     * @return mixed
138
     * @throws \Exception
139
     */
140
    public function resource($slug)
141
    {
142
        return $this->checkResource($slug);
143
    }
144
145
    /**
146
     * Set the action.
147
     *
148
     * @param $action
149
     */
150
    public function setAction($action)
151
    {
152
        $this->resourceChecker->setCurrentAction($action);
153
    }
154
155
    /**
156
     * @return mixed
157
     * @throws \Exception
158
     */
159
    public function string()
160
    {
161
        return collect($this->health())->reduce(function ($current, $resource) {
162
            return (
163
                $current .
164
                ($current ? config('health.string.glue') : '') .
165
                $this->makeString(
166
                    $resource->abbreviation,
167
                    $resource->isHealthy()
168
                )
169
            );
170
        });
171
    }
172
}
173