Skip to content

Commit

Permalink
Merge branch 'connectorEvent' into 1.x
Browse files Browse the repository at this point in the history
[#7] Dispatch an event for modifying the connector ID
  • Loading branch information
jensschuppe committed Sep 14, 2023
2 parents 0806404 + d9e92ae commit 39fb01c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 24 deletions.
24 changes: 8 additions & 16 deletions modules/civiremote_event/src/CiviMRF.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function getEvent($event_id, $remote_token = NULL) {

$reply = &drupal_static(__FUNCTION__ . '_' . implode('_', $params));
if (!isset($reply)) {
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteEvent',
'getsingle',
$params,
Expand Down Expand Up @@ -106,8 +105,7 @@ public function getForm($event_id, $profile, $remote_token = NULL, $context = 'c

$reply = &drupal_static(__FUNCTION__ . '_' . implode('_', $params));
if (!isset($reply)) {
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteParticipant',
'get_form',
$params,
Expand Down Expand Up @@ -155,8 +153,7 @@ public function validateEventRegistration($event_id, $profile, $remote_token = N
'context' => $context,
]);
self::addRemoteContactId($params);
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteParticipant',
'validate',
$params,
Expand Down Expand Up @@ -197,8 +194,7 @@ public function createEventRegistration($event_id, $profile, $remote_token = NUL
'token' => $remote_token
]);
self::addRemoteContactId($params);
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteParticipant',
'create',
$params,
Expand Down Expand Up @@ -242,8 +238,7 @@ public function updateEventRegistration($event_id, $profile, $remote_token = NUL
'token' => $remote_token
]);
self::addRemoteContactId($params);
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteParticipant',
'update',
$params,
Expand Down Expand Up @@ -282,8 +277,7 @@ public function cancelEventRegistration($event_id, $remote_token = NULL, $show_m
'token' => $remote_token
];
self::addRemoteContactId($params);
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteParticipant',
'cancel',
$params,
Expand Down Expand Up @@ -323,8 +317,7 @@ public function getCheckinInfo($remote_token, $show_messages = FALSE) {
'token' => $remote_token
];
self::addRemoteContactId($params);
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'EventCheckin',
'verify',
$params,
Expand Down Expand Up @@ -370,8 +363,7 @@ public function checkinParticipant($remote_token, $status_id, $show_messages = F
'status_id' => $status_id,
];
self::addRemoteContactId($params);
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'EventCheckin',
'confirm',
$params,
Expand Down
53 changes: 45 additions & 8 deletions src/CiviMRF.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

namespace Drupal\civiremote;


use Drupal;
use Drupal\cmrf_core\Core;
use Drupal\civiremote\Event\ConnectorEvent;
use \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;

/**
* Class CiviMRF
Expand All @@ -26,15 +27,53 @@
*/
class CiviMRF {

/** @var Core */
/**
* @var Core
*/
public $core;

public function __construct(Core $core) {
$this->core = $core;
}

protected function connector() {
return Drupal::config('civiremote.settings')->get('cmrf_connector');
protected function connector(array $context = []) {
$event = new ConnectorEvent(
Drupal::config('civiremote.settings')->get('cmrf_connector'),
$context
);
/* @var ContainerAwareEventDispatcher $event_dispatcher */
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, ConnectorEvent::EVENT_NAME);
return $event->getConnectorId();
}

/**
* @param $entity
* @param $action
* @param $params
* @param $options
* @param $callback
* @param $api_version
*
* @return \CMRF\Core\Call
*/
protected function createCall($entity, $action, $params = [], $options = [], $callback = NULL, $api_version = '3') {
return $this->core->createCall(
$this->connector([
'entity' => $entity,
'action' => $action,
'params' => $params,
'options' => $options,
'callback' => $callback,
'api_version' => $api_version,
]),
$entity,
$action,
$params,
$options,
$callback,
$api_version
);
}

/**
Expand All @@ -44,8 +83,7 @@ protected function connector() {
* The CiviRemote ID, or FALSE when no contact could be matched.
*/
public function matchContact($params) {
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteContact',
'match',
$params,
Expand All @@ -63,8 +101,7 @@ public function matchContact($params) {
* The CiviRemote roles, or FALSE when synchronising roles failed.
*/
public function getRoles($params) {
$call = $this->core->createCall(
$this->connector(),
$call = $this->createCall(
'RemoteContact',
'get_roles',
$params,
Expand Down
46 changes: 46 additions & 0 deletions src/Event/ConnectorEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\civiremote\Event;

use Drupal\Component\EventDispatcher\Event;

class ConnectorEvent extends Event {

const EVENT_NAME = 'civiremote_connector';

/**
* The CiviMRF Connector ID.
*
* @var string $connector_id
*/
protected string $connector_id;

/**
* The context of the event.
*
* @var array $context
*/
protected array $context;

public function __construct(string $connector_id, array $context = []) {
$this->connector_id = $connector_id;
$this->context = $context;
}

/**
* @return string
*/
public function getConnectorId(): string {
return $this->connector_id;
}

/**
* @param string $connector_id
*
* @return void
*/
public function setConnectorId(string $connector_id): void {
$this->connector_id = $connector_id;
}

}

0 comments on commit 39fb01c

Please sign in to comment.