Merge pull request #17817 from colemanw/getChecksConfig
[civicrm-core.git] / CRM / Utils / Check / Component.php
CommitLineData
3a0d0bbd
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
3a0d0bbd 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
3a0d0bbd
CW
9 +--------------------------------------------------------------------+
10 */
11
b4628ea6 12use Civi\Api4\StatusPreference;
4094935a 13
3a0d0bbd
CW
14/**
15 *
16 * @package CRM
ca5cec67 17 * @copyright CiviCRM LLC https://civicrm.org/licensing
3a0d0bbd
CW
18 */
19abstract class CRM_Utils_Check_Component {
20
b4628ea6 21 /**
22 * Get the configured status checks.
23 *
24 * @return array
25 *
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
28 */
29 public function getChecksConfig() {
1416a3c4
CW
30 if (empty(Civi::$statics[__FUNCTION__])) {
31 Civi::$statics[__FUNCTION__] = (array) StatusPreference::get(FALSE)
32 ->addWhere('domain_id', '=', 'current_domain')
33 ->execute()->indexBy('name');
b4628ea6 34 }
1416a3c4 35 return Civi::$statics[__FUNCTION__];
b4628ea6 36 }
37
3a0d0bbd
CW
38 /**
39 * Should these checks be run?
40 *
41 * @return bool
42 */
43 public function isEnabled() {
44 return TRUE;
45 }
46
47 /**
48 * Run all checks in this class.
49 *
50 * @return array
51 * [CRM_Utils_Check_Message]
b4628ea6 52 *
53 * @throws \API_Exception
54 * @throws \Civi\API\Exception\UnauthorizedException
3a0d0bbd
CW
55 */
56 public function checkAll() {
be2fb01f 57 $messages = [];
3a0d0bbd 58 foreach (get_class_methods($this) as $method) {
b4628ea6 59 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
60 if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) {
3a0d0bbd
CW
61 $messages = array_merge($messages, $this->$method());
62 }
63 }
64 return $messages;
65 }
d631cdc8 66
b4628ea6 67 /**
68 * Is the specified check disabled.
69 *
70 * @param string $method
71 *
72 * @return bool
73 *
74 * @throws \API_Exception
75 * @throws \Civi\API\Exception\UnauthorizedException
76 */
77 public function isDisabled($method) {
185a0768
CW
78 $checks = $this->getChecksConfig();
79 if (isset($checks[$method]['is_active'])) {
80 return !$checks[$method]['is_active'];
b4628ea6 81 }
82 return FALSE;
83 }
84
4094935a
AP
85 /**
86 * Check if file exists on given URL.
87 *
6714d8d2 88 * @param string $url
9679fb15 89 * @param float|bool $timeoutOverride
32e1f6cc 90 *
4094935a 91 * @return bool
b4628ea6 92 * @throws \GuzzleHttp\Exception\GuzzleException
4094935a 93 */
9679fb15
SL
94 public function fileExists($url, $timeoutOverride = FALSE) {
95 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
96 if (!$timeoutOverride && $timeoutOverride !== 0) {
97 $timeoutOverride = (float) Civi::settings()->get('http_timeout');
98 }
4094935a
AP
99 $fileExists = FALSE;
100 try {
101 $guzzleClient = new GuzzleHttp\Client();
102 $guzzleResponse = $guzzleClient->request('GET', $url, array(
9679fb15 103 'timeout' => $timeoutOverride,
4094935a
AP
104 ));
105 $fileExists = ($guzzleResponse->getStatusCode() == 200);
106 }
107 catch (Exception $e) {
32e1f6cc 108 // At this stage we are not checking for variants of not being able to receive it.
109 // However, we might later enhance this to distinguish forbidden from a 500 error.
4094935a
AP
110 }
111 return $fileExists;
112 }
113
137d9cf0 114}