4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 class CRM_Utils_GeocodeProvider
{
21 * Caches the provider class name. Disables geocoding when set to FALSE.
25 private static $providerClassName;
28 * Instantiate a geocode object of the system-configured type.
30 * @return CRM_Utils_Geocode
31 * @throws CRM_Core_Exception
33 public static function getConfiguredProvider() {
34 $geoCodeClassName = self
::getUsableClassName();
35 if ($geoCodeClassName === FALSE) {
36 throw new CRM_Core_Exception('No valid geocoding provider enabled');
38 return new $geoCodeClassName();
42 * Get the name of the geocoding class if enabled.
44 * This retrieves the geocoding class, checking it can be accessed.
45 * Checks are done to mitigate the possibility it has been configured
46 * and then the file has been removed.
49 * Class name if usable, else false.
51 public static function getUsableClassName() {
52 if (self
::$providerClassName === NULL) {
53 $provider = Civi
::settings()->get('geoProvider');
54 if (!class_exists($provider)) {
55 if (class_exists('CRM_Utils_Geocode_' . $provider)) {
56 $provider = 'CRM_Utils_Geocode_' . $provider;
59 if (strlen($provider)) {
61 ->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]);
67 // Ideally geocoding providers would be required to implement an interface
68 // or extend a base class. While we identify and implement a geocoding
69 // abstraction library (rather than continue to roll our own), we settle for
71 if (!method_exists($provider, 'format') && $provider !== FALSE) {
72 Civi
::log()->error('Configured geocoder is invalid, must provide a format method', ['geocode_class' => $provider]);
76 self
::$providerClassName = $provider;
79 return self
::$providerClassName;
83 * Disable GeoProvider within a session.
85 * This disables geocoding by causing getUsableClassName() to bail out.
87 public static function disableForSession() {
88 self
::$providerClassName = FALSE;
92 * Reset geoprovider (after it has been disabled).
94 public static function reset() {
95 self
::$providerClassName = NULL;
96 self
::getUsableClassName();