1 | <?php |
||
9 | |||
10 | class Certificate extends Base |
||
11 | { |
||
12 | /** |
||
13 | * HTTP Checker. |
||
14 | * |
||
15 | * @return Result |
||
16 | */ |
||
17 | public function check() |
||
18 | { |
||
19 | $resources = $this->getResourceUrlArray(); |
||
20 | |||
21 | $first = collect($resources)->first(); |
||
22 | |||
23 | if (filled($first)) { |
||
24 | $this->target->setDisplay("{$first}"); |
||
25 | } |
||
26 | |||
27 | try { |
||
28 | foreach ($resources as $url) { |
||
29 | [$healthy, $message] = $this->checkCertificate($url); |
||
30 | |||
31 | if (!$healthy) { |
||
32 | return $this->makeResult(false, $message); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | return $this->makeHealthyResult(); |
||
37 | } catch (\Exception $exception) { |
||
38 | report($exception); |
||
39 | |||
40 | return $this->makeResultFromException($exception); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * HTTP Checker. |
||
46 | * |
||
47 | * @return Result |
||
48 | */ |
||
49 | public function checkCertificate($url) |
||
50 | { |
||
51 | return $this->checkHostCertificate($this->getHost($url)); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get the error message. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | protected function getErrorMessage($host) |
||
60 | { |
||
61 | return sprintf($this->target->resource->errorMessage, $host); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get the error message. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | protected function getHost($url) |
||
70 | { |
||
71 | $parsed = parse_url($url); |
||
72 | |||
73 | if (isset($parsed['host'])) { |
||
74 | return $parsed['host']; |
||
75 | } |
||
76 | |||
77 | return $url; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get array of resource urls. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | private function getResourceUrlArray() |
||
86 | { |
||
87 | if (is_a($this->target->urls, Collection::class)) { |
||
88 | return $this->target->urls->toArray(); |
||
89 | } |
||
90 | |||
91 | return (array) $this->target->urls; |
||
92 | } |
||
93 | |||
178 |