| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Utils; |
| 4 |
|
| 5 |
use MultiSafepay\WooCommerce\Exceptions\MissingDependencyException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Fired on bootstrap plugin file. |
| 9 |
* This class defines all code necessary to check if there is a dependency missing to make the plugin work |
| 10 |
*/ |
| 11 |
class DependencyChecker { |
| 12 |
|
| 13 |
const REQUIRED_PLUGINS = array( |
| 14 |
'WooCommerce' => 'woocommerce/woocommerce.php', |
| 15 |
); |
| 16 |
|
| 17 |
/** |
| 18 |
* Check if there is a dependency missing to make the plugin work |
| 19 |
* |
| 20 |
* @throws MissingDependencyException |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function check(): void { |
| 24 |
$missing_plugins = $this->get_missing_plugins_list(); |
| 25 |
if ( ! empty( $missing_plugins ) ) { |
| 26 |
throw new MissingDependencyException( $missing_plugins ); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Return the keys of all missing plugins |
| 32 |
* |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
private function get_missing_plugins_list(): array { |
| 36 |
return array_keys( array_filter( self::REQUIRED_PLUGINS, array( $this, 'is_plugin_inactive' ) ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Check if a certain plugin is inactive |
| 41 |
* |
| 42 |
* @param string $plugin_path |
| 43 |
* @return boolean |
| 44 |
*/ |
| 45 |
private function is_plugin_inactive( string $plugin_path ): bool { |
| 46 |
if ( ! is_plugin_active( $plugin_path ) ) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
return false; |
| 51 |
} |
| 52 |
} |
| 53 |
|