Fix php comments
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessor.php
index a24cde25ea7403ca6e5babfc52aa1cf04dc73deb..f455c5e7fcdffe745c8d72efd241a046b17f94be 100644 (file)
@@ -18,7 +18,7 @@
 /**
  * This class contains payment processor related functions.
  */
-class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor {
+class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor implements \Civi\Test\HookInterface {
   /**
    * Static holder for the default payment processor
    * @var object
@@ -32,9 +32,11 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    *   Parameters for Processor entity.
    *
    * @return CRM_Financial_DAO_PaymentProcessor
-   * @throws Exception
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public static function create($params) {
+  public static function create(array $params): CRM_Financial_DAO_PaymentProcessor {
     // If we are creating a new PaymentProcessor and have not specified the payment instrument to use, get the default from the Payment Processor Type.
     if (empty($params['id']) && empty($params['payment_instrument_id'])) {
       $params['payment_instrument_id'] = civicrm_api3('PaymentProcessorType', 'getvalue', [
@@ -88,16 +90,9 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
     return $processor;
   }
 
-  /**
-   * Class constructor.
-   */
-  public function __construct() {
-    parent::__construct();
-  }
-
   /**
    * Retrieve array of allowed credit cards for this payment processor.
-   * @param interger|null $paymentProcessorID id of processor.
+   * @param int|null $paymentProcessorID
    * @return array
    */
   public static function getCreditCards($paymentProcessorID = NULL) {
@@ -189,27 +184,29 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    * Delete payment processor.
    *
    * @param int $paymentProcessorID
-   *
-   * @return null
+   * @deprecated
    */
   public static function del($paymentProcessorID) {
     if (!$paymentProcessorID) {
       throw new CRM_Core_Exception(ts('Invalid value passed to delete function.'));
     }
+    static::deleteRecord(['id' => $paymentProcessorID]);
+  }
 
-    $dao = new CRM_Financial_DAO_PaymentProcessor();
-    $dao->id = $paymentProcessorID;
-    if (!$dao->find(TRUE)) {
-      return NULL;
-    }
-
-    $testDAO = new CRM_Financial_DAO_PaymentProcessor();
-    $testDAO->name = $dao->name;
-    $testDAO->is_test = 1;
-    $testDAO->delete();
+  /**
+   * Callback for hook_civicrm_post().
+   * @param \Civi\Core\Event\PostEvent $event
+   */
+  public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) {
+    if ($event->action === 'delete') {
+      // When a paymentProcessor is deleted, delete the associated test processor
+      $testDAO = new CRM_Financial_DAO_PaymentProcessor();
+      $testDAO->name = $event->object->name;
+      $testDAO->is_test = 1;
+      $testDAO->delete();
 
-    $dao->delete();
-    Civi\Payment\System::singleton()->flushProcessors();
+      Civi\Payment\System::singleton()->flushProcessors();
+    }
   }
 
   /**
@@ -227,7 +224,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    *   associated array with payment processor related fields
    */
   public static function getPayment($paymentProcessorID, $mode = 'based_on_id') {
-    $capabilities = ($mode == 'test') ? ['TestMode'] : [];
+    $capabilities = ($mode === 'test') ? ['TestMode'] : [];
     $processors = self::getPaymentProcessors($capabilities, [$paymentProcessorID]);
     return $processors[$paymentProcessorID];
   }
@@ -273,12 +270,12 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
   /**
    * Get all payment processors as an array of objects.
    *
-   * @param string|NULL $mode
+   * @param string|null $mode
    * only return this mode - test|live or NULL for all
    * @param bool $reset
    * @param bool $isCurrentDomainOnly
    *   Do we only want to load payment processors associated with the current domain.
-   * @param bool|NULL $isActive
+   * @param bool|null $isActive
    *   Do we only want active processors, only inactive (FALSE) or all processors (NULL)
    *
    * @throws CiviCRM_API3_Exception
@@ -305,10 +302,10 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
     if ($isCurrentDomainOnly) {
       $retrievalParameters['domain_id'] = CRM_Core_Config::domainID();
     }
-    if ($mode == 'test') {
+    if ($mode === 'test') {
       $retrievalParameters['is_test'] = 1;
     }
-    elseif ($mode == 'live') {
+    elseif ($mode === 'live') {
       $retrievalParameters['is_test'] = 0;
     }
 
@@ -417,7 +414,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
       }
       // Invalid processors will store a null value in 'object' (e.g. if not all required config fields are present).
       // This is determined by calling when loading the processor via the $processorObject->checkConfig() function.
-      if (!$processor['object'] instanceof \CRM_Core_Payment) {
+      if (!$processor['object'] instanceof CRM_Core_Payment) {
         unset($processors[$index]);
         continue;
       }
@@ -504,7 +501,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
       return $result;
     }
 
-    if ($component == 'membership') {
+    if ($component === 'membership') {
       $sql = "
     SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
       FROM civicrm_membership mem
@@ -514,7 +511,7 @@ INNER JOIN civicrm_contribution       con ON ( mp.contribution_id = con.id )
  LEFT JOIN civicrm_contribution_page  cp  ON ( con.contribution_page_id  = cp.id )
      WHERE mp.membership_id = %1";
     }
-    elseif ($component == 'contribute') {
+    elseif ($component === 'contribute') {
       $sql = "
     SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
       FROM civicrm_contribution       con
@@ -522,7 +519,7 @@ INNER JOIN civicrm_contribution       con ON ( mp.contribution_id = con.id )
  LEFT JOIN civicrm_contribution_page  cp  ON ( con.contribution_page_id  = cp.id )
      WHERE con.id = %1";
     }
-    elseif ($component == 'recur') {
+    elseif ($component === 'recur') {
       // @deprecated - use getPaymentProcessorForRecurringContribution.
       $sql = "
     SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
@@ -544,13 +541,13 @@ INNER JOIN civicrm_contribution       con ON ( mp.contribution_id = con.id )
 
     $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : ($dao->ppID2 ?? NULL);
     $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live';
-    if (!$ppID || $type == 'id') {
+    if (!$ppID || $type === 'id') {
       $result = $ppID;
     }
-    elseif ($type == 'info') {
+    elseif ($type === 'info') {
       $result = self::getPayment($ppID, $mode);
     }
-    elseif ($type == 'obj' && is_numeric($ppID)) {
+    elseif ($type === 'obj' && is_numeric($ppID)) {
       try {
         $paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $ppID]);
       }