Remove unnecessary try/catch per #17729
[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 * @var array
23 */
24 public $checksConfig = [];
25
26 /**
27 * Get the configured status checks.
28 *
29 * @return array
30 *
31 * @throws \API_Exception
32 * @throws \Civi\API\Exception\UnauthorizedException
33 */
34 public function getChecksConfig() {
35 if (empty($this->checksConfig)) {
36 $this->checksConfig = Civi::cache('checks')->get('checksConfig', []);
37 if (empty($this->checksConfig)) {
38 $this->checksConfig = StatusPreference::get()->setCheckPermissions(FALSE)->execute()->indexBy('name');
39 }
40 }
41 return $this->checksConfig;
42 }
43
44 /**
45 * @param array $checksConfig
46 */
47 public function setChecksConfig(array $checksConfig) {
48 $this->checksConfig = $checksConfig;
49 }
50
3a0d0bbd
CW
51 /**
52 * Should these checks be run?
53 *
54 * @return bool
55 */
56 public function isEnabled() {
57 return TRUE;
58 }
59
60 /**
61 * Run all checks in this class.
62 *
63 * @return array
64 * [CRM_Utils_Check_Message]
b4628ea6 65 *
66 * @throws \API_Exception
67 * @throws \Civi\API\Exception\UnauthorizedException
3a0d0bbd
CW
68 */
69 public function checkAll() {
be2fb01f 70 $messages = [];
3a0d0bbd 71 foreach (get_class_methods($this) as $method) {
b4628ea6 72 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
73 if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) {
3a0d0bbd
CW
74 $messages = array_merge($messages, $this->$method());
75 }
76 }
77 return $messages;
78 }
d631cdc8 79
b4628ea6 80 /**
81 * Is the specified check disabled.
82 *
83 * @param string $method
84 *
85 * @return bool
86 *
87 * @throws \API_Exception
88 * @throws \Civi\API\Exception\UnauthorizedException
89 */
90 public function isDisabled($method) {
185a0768
CW
91 $checks = $this->getChecksConfig();
92 if (isset($checks[$method]['is_active'])) {
93 return !$checks[$method]['is_active'];
b4628ea6 94 }
95 return FALSE;
96 }
97
4094935a
AP
98 /**
99 * Check if file exists on given URL.
100 *
6714d8d2 101 * @param string $url
9679fb15 102 * @param float|bool $timeoutOverride
32e1f6cc 103 *
4094935a 104 * @return bool
b4628ea6 105 * @throws \GuzzleHttp\Exception\GuzzleException
4094935a 106 */
9679fb15
SL
107 public function fileExists($url, $timeoutOverride = FALSE) {
108 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
109 if (!$timeoutOverride && $timeoutOverride !== 0) {
110 $timeoutOverride = (float) Civi::settings()->get('http_timeout');
111 }
4094935a
AP
112 $fileExists = FALSE;
113 try {
114 $guzzleClient = new GuzzleHttp\Client();
115 $guzzleResponse = $guzzleClient->request('GET', $url, array(
9679fb15 116 'timeout' => $timeoutOverride,
4094935a
AP
117 ));
118 $fileExists = ($guzzleResponse->getStatusCode() == 200);
119 }
120 catch (Exception $e) {
32e1f6cc 121 // At this stage we are not checking for variants of not being able to receive it.
122 // However, we might later enhance this to distinguish forbidden from a 500 error.
4094935a
AP
123 }
124 return $fileExists;
125 }
126
137d9cf0 127}