Skip to content

Commit

Permalink
Refactored GIRUtils to allow testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
agger-magenta committed Nov 2, 2021
1 parent 25ee2f8 commit 3f30062
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 70 deletions.
59 changes: 33 additions & 26 deletions os2forms_egir.module
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ function os2forms_egir_gir_approver_is_initiator($processID, $queueID) {
function os2forms_egir_gir_change_org_unit($processID, $queueID) {
// Get ID for webform which spawned this process.
$sid = MaestroEngine::getEntityIdentiferByUniqueID($processID, 'submission');

$utils = new GIRUtils();

if ($sid) {

$webform_submission = WebformSubmission::load($sid);
Expand All @@ -139,7 +142,7 @@ function os2forms_egir_gir_change_org_unit($processID, $queueID) {
$validity = ['from' => $start_date, 'to' => $end_date];

// Org Unit edit details.
$org_unit_uuid = GIRUtils::getTermData($org_unit_id, 'field_uuid');
$org_unit_uuid = $utils->getTermData($org_unit_id, 'field_uuid');

$ou_edit = [
'type' => 'org_unit',
Expand All @@ -153,11 +156,11 @@ function os2forms_egir_gir_change_org_unit($processID, $queueID) {
// Owner edit details.
// We need the UUID of the owner *object*.
$owner_path = '/service/ou/' . $org_unit_uuid . '/details/owner';
$owner_json = GIRUtils::getJsonFromApi($owner_path);
$owner_json = $utils->getJsonFromApi($owner_path);
$owner_obj_uuid = reset($owner_json)["uuid"];

// And then the UUID of the owner person.
$owner_person_uuid = GIRUtils::getUserData($owner_id, 'field_uuid');
$owner_person_uuid = $utils->getUserData($owner_id, 'field_uuid');

$owner_edit = [
'type' => 'owner',
Expand All @@ -171,9 +174,9 @@ function os2forms_egir_gir_change_org_unit($processID, $queueID) {

$edit_data = [$ou_edit, $owner_edit];
$json_data = json_encode($edit_data);
GIRUtils::formsLog()->notice('Edit data sent: <' . $json_data . '>');
$utils->formsLog()->notice('Edit data sent: <' . $json_data . '>');

$resp = GIRUtils::postJsonToApi('/service/details/edit', $json_data);
$resp = $utils->postJsonToApi('/service/details/edit', $json_data);

if ($resp->getStatusCode() === 200) {
$url_status = 'Success';
Expand All @@ -197,6 +200,7 @@ function os2forms_egir_gir_change_org_unit($processID, $queueID) {
function os2forms_egir_gir_change_external($processID, $queueID) {
// Get ID for webform which spawned this process.
$sid = MaestroEngine::getEntityIdentiferByUniqueID($processID, 'submission');
$utils = new GIRUtils();
if ($sid) {

$webform_submission = WebformSubmission::load($sid);
Expand Down Expand Up @@ -226,16 +230,16 @@ function os2forms_egir_gir_change_external($processID, $queueID) {
else {
return FALSE;
}
GIRUtils::formsLog()->notice(var_export($values, TRUE));
GIRUtils::formsLog()->notice(var_export($extra_uuids, TRUE));
$utils->formsLog()->notice(var_export($values, TRUE));
$utils->formsLog()->notice(var_export($extra_uuids, TRUE));
// Config.
$config = new EGIRConfig();

// Validity.
$validity = ['from' => $start_date, 'to' => $end_date];

// Employee edit details.
$external_uuid = GIRUtils::getUserData($external_id, 'field_uuid');
$external_uuid = $utils->getUserData($external_id, 'field_uuid');
$empl_edit = [
'type' => 'employee',
'uuid' => $external_uuid,
Expand Down Expand Up @@ -267,8 +271,8 @@ function os2forms_egir_gir_change_external($processID, $queueID) {
];

// Employment edit details.
$consultancy_uuid = GIRUtils::getTermData($consultancy_id, 'field_uuid');
$consultant_type_uuid = GIRUtils::getTermData($consultant_type_id, 'field_uuid');
$consultancy_uuid = $utils->getTermData($consultancy_id, 'field_uuid');
$consultant_type_uuid = $utils->getTermData($consultant_type_id, 'field_uuid');
$engagement_edit = [
'type' => 'engagement',
'uuid' => $extra_uuids['engagement_uuid'],
Expand All @@ -286,9 +290,9 @@ function os2forms_egir_gir_change_external($processID, $queueID) {
$engagement_edit,
];
$json_data = json_encode($edit_data);
GIRUtils::formsLog()->notice('Edit data sent:' . var_export($json_data, TRUE));
$utils->formsLog()->notice('Edit data sent:' . var_export($json_data, TRUE));

$resp = GIRUtils::postJsonToApi('/service/details/edit', $json_data);
$resp = $utils->postJsonToApi('/service/details/edit', $json_data);

if ($resp->getStatusCode() === 200) {
$url_status = 'Success';
Expand All @@ -312,6 +316,7 @@ function os2forms_egir_gir_change_external($processID, $queueID) {
function os2forms_egir_gir_disable_org_unit($processID, $queueID) {
// Get ID for webform which spawned this process.
$sid = MaestroEngine::getEntityIdentiferByUniqueID($processID, 'submission');
$utils = new GIRUtils();
if ($sid) {

$webform_submission = WebformSubmission::load($sid);
Expand All @@ -331,12 +336,12 @@ function os2forms_egir_gir_disable_org_unit($processID, $queueID) {
// We terminate *from* end_date *to* infinity.
// NB: This kind of validity payload is only possible as of MO ^2.0!
$validity = ['validity' => ['from' => $end_date, 'to' => NULL]];
$org_unit_uuid = GIRUtils::getTermData($org_unit_id, 'field_uuid');
$org_unit_uuid = $utils->getTermData($org_unit_id, 'field_uuid');

$json_data = json_encode($validity);
GIRUtils::formsLog()->notice('Termination data sent: <' . $json_data . '>');
$utils->formsLog()->notice('Termination data sent: <' . $json_data . '>');

$resp = GIRUtils::postJsonToApi("/service/ou/{$org_unit_uuid}/terminate", $json_data);
$resp = $utils->postJsonToApi("/service/ou/{$org_unit_uuid}/terminate", $json_data);

if ($resp->getStatusCode() === 200) {
$url_status = 'Success';
Expand Down Expand Up @@ -377,12 +382,12 @@ function os2forms_egir_gir_disable_external($processID, $queueID) {
else {
return FALSE;
}
$external_uuid = GIRUtils::getUserData($external_id, 'field_uuid');
$external_uuid = $utils->getUserData($external_id, 'field_uuid');
// Unfortunately, terminating *from* end date *to* infinity is currently
// not possible from this endpoint. We use the old way.
$validity = ['validity' => ['to' => $end_date]];
$json_data = json_encode($validity);
$resp = GIRUtils::postJsonToApi("/service/e/{$external_uuid}/terminate", $json_data);
$resp = $utils->postJsonToApi("/service/e/{$external_uuid}/terminate", $json_data);

if ($resp->getStatusCode() === 200) {
$url_status = 'Success';
Expand All @@ -406,6 +411,7 @@ function os2forms_egir_gir_disable_external($processID, $queueID) {
function os2forms_egir_gir_create_org_unit($processID, $queueID) {
// Get ID for webform which spawned this process.
$sid = MaestroEngine::getEntityIdentiferByUniqueID($processID, 'submission');
$utils = new GIRUtils();
if ($sid) {

$webform_submission = WebformSubmission::load($sid);
Expand Down Expand Up @@ -436,7 +442,7 @@ function os2forms_egir_gir_create_org_unit($processID, $queueID) {
$validity = ['from' => $start_date, 'to' => $end_date];

// Owner data.
$owner_uuid = GIRUtils::getUserData($owner_id, 'field_uuid');
$owner_uuid = $utils->getUserData($owner_id, 'field_uuid');
$owner_details = [
'type' => 'owner',
'owner' => ['uuid' => $owner_uuid],
Expand All @@ -455,9 +461,9 @@ function os2forms_egir_gir_create_org_unit($processID, $queueID) {
];

$json_data = json_encode($ou_data);
GIRUtils::formsLog()->notice('Org unit sent: <' . $json_data . '>');
$utils->formsLog()->notice('Org unit sent: <' . $json_data . '>');

$resp = GIRUtils::postJsonToApi('/service/ou/create', $json_data);
$resp = $utils->postJsonToApi('/service/ou/create', $json_data);

if ($resp->getStatusCode() === 201) {
$url_status = 'Success';
Expand All @@ -481,6 +487,7 @@ function os2forms_egir_gir_create_org_unit($processID, $queueID) {
function os2forms_egir_gir_create_external($processID, $queueID) {
// Get ID for webform which spawned this process.
$sid = MaestroEngine::getEntityIdentiferByUniqueID($processID, 'submission');
$utils = new GIRUtils();
if ($sid) {

$webform_submission = WebformSubmission::load($sid);
Expand Down Expand Up @@ -516,9 +523,9 @@ function os2forms_egir_gir_create_external($processID, $queueID) {
$validity = ['from' => $start_date, 'to' => $end_date];

// Get consultancy data MO UUIDs.
$consultancy_uuid = GIRUtils::getTermData($consultancy_id, 'field_uuid');
$consultant_type_uuid = GIRUtils::getTermData($consultant_type_id, 'field_uuid');
$cost_center_uuid = GIRUtils::getTermData($cost_center_id, 'field_uuid');
$consultancy_uuid = $utils->getTermData($consultancy_id, 'field_uuid');
$consultant_type_uuid = $utils->getTermData($consultant_type_id, 'field_uuid');
$cost_center_uuid = $utils->getTermData($cost_center_id, 'field_uuid');

// Generate employment UUID.
$employment_uuid = Uuid::uuid4()->toString();
Expand Down Expand Up @@ -567,7 +574,7 @@ function os2forms_egir_gir_create_external($processID, $queueID) {

// Remaining engagement associations.
foreach ($org_units as $ou) {
$ou_uuid = GIRUtils::getTermData($ou, 'field_uuid');
$ou_uuid = $utils->getTermData($ou, 'field_uuid');
array_push($details,
[
'type' => 'engagement_association',
Expand All @@ -587,11 +594,11 @@ function os2forms_egir_gir_create_external($processID, $queueID) {
];

$json_data = json_encode($external_data);
GIRUtils::formsLog()->notice('External sent: <' . $json_data . '>');
$utils->formsLog()->notice('External sent: <' . $json_data . '>');

// Param ?force=true is required because we create engagements and
// engagement associations, which are interdependent, at the same time.
$resp = GIRUtils::postJsonToApi('/service/e/create?force=true', $json_data);
$resp = $utils->postJsonToApi('/service/e/create?force=true', $json_data);

if ($resp->getStatusCode() === 201) {
$url_status = 'Success';
Expand Down
4 changes: 3 additions & 1 deletion src/Feeds/Fetcher/KeycloakAuthFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ public function fetch(FeedInterface $feed, StateInterface $state) {
$sink = $this->fileSystem->tempnam('temporary://', 'feeds_http_fetcher');
$sink = $this->fileSystem->realpath($sink);

$utils = new GIRUtils();

$response = $this->get(
$feed->getSource(), $sink,
$this->getCacheKey($feed), GIRUtils::getOpenIdToken()
$this->getCacheKey($feed), $utils->getOpenIdToken()
);
// @todo Handle redirects.
// @codingStandardsIgnoreStart
Expand Down
30 changes: 18 additions & 12 deletions src/GIRUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ class GIRUtils implements ContainerFactoryPluginInterface {
*/
protected $httpClient;

/**
* Constructor.
*/
final public function __construct(array $configuration, $plugin_id, $plugin_definition, Client $httpClient) {
$this->httpClient = $httpClient;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* Static create function.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
Expand All @@ -37,30 +43,30 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* Get logger.
*/
public static function formsLog() {
public function formsLog() {
return \Drupal::logger('os2forms_egir');
}

/**
* Get user data by Drupal ID and field name.
*/
public static function getUserData($user_id, $field_name) {
public function getUserData($user_id, $field_name) {
$user = \Drupal::entityTypeManager()->getStorage('user')->load($user_id);
return $user->getTypedData()->get($field_name)->value;
}

/**
* Get taxonomy term data by Drupal ID and field name.
*/
public static function getTermData($term_id, $field_name) {
public function getTermData($term_id, $field_name) {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term_id);
return $term->getTypedData()->get($field_name)->value;
}

/**
* Get term ID by name.
*/
public static function getTermIdByName($name) {
public function getTermIdByName($name) {

$properties = [];
$properties['name'] = $name;
Expand All @@ -74,7 +80,7 @@ public static function getTermIdByName($name) {
/**
* Get Drupal user ID by MO UUID.
*/
public static function getUserByGirUuid($mo_uuid) {
public function getUserByGirUuid($mo_uuid) {
$user_store = \Drupal::entityTypeManager()->getStorage('user');
$user_array = $user_store->loadByProperties(['field_uuid' => $mo_uuid]);
if ($user_array) {
Expand All @@ -88,7 +94,7 @@ public static function getUserByGirUuid($mo_uuid) {
/**
* Get JSON from specified GIR API path.
*/
public static function getJsonFromApi($path) {
public function getJsonFromApi($path) {
$config = new EGIRConfig();
$mo_url = $config->girUrl;
$url = $mo_url . $path;
Expand Down Expand Up @@ -123,7 +129,7 @@ public static function getJsonFromApi($path) {
/**
* Post data to the relevant path.
*/
public static function postJsonToApi($path, $data) {
public function postJsonToApi($path, $data) {
// Full API path.
$config = new EGIRConfig();
$url = $config->girUrl . $path;
Expand Down Expand Up @@ -151,7 +157,7 @@ public static function postJsonToApi($path, $data) {
/**
* Get OpenID authentication token from Keycloak.
*/
public static function getOpenIdToken() {
public function getOpenIdToken() {
$keycloak_configuration = \Drupal::config('openid_connect.settings.keycloak');

$keycloak_settings = $keycloak_configuration->get('settings');
Expand Down Expand Up @@ -190,7 +196,7 @@ public static function getOpenIdToken() {
*
* NOTE: Do not recurse into children.
*/
public static function getEmployees($org_unit_uuid) {
public function getEmployees($org_unit_uuid) {
$engagement_path = "/service/ou/{$org_unit_uuid}/details/engagement?validity=present";

$engagements = self::getJsonFromApi($engagement_path);
Expand All @@ -206,7 +212,7 @@ public static function getEmployees($org_unit_uuid) {
/**
* Get all employments with engagements in the specified organisation unit.
*/
public static function getExternals($org_unit_uuid) {
public function getExternals($org_unit_uuid) {
$ea_path = "/api/v1/engagement_association?validity=present&org_unit={$org_unit_uuid}";
$engagement_associations = self::getJsonFromApi($ea_path);

Expand All @@ -227,7 +233,7 @@ public static function getExternals($org_unit_uuid) {
/**
* Get the engagement (singular) for the given employee.
*/
public static function getEngagement($employee_uuid) {
public function getEngagement($employee_uuid) {
$employee_path = "/service/e/{$employee_uuid}/";
$details_path = $employee_path . 'details/';
$details_json = GIRUtils::getJsonFromApi($details_path);
Expand All @@ -247,7 +253,7 @@ public static function getEngagement($employee_uuid) {
/**
* Get the engagement associations for the given engagement.
*/
public static function getEngagementAssociations($engagement_uuid) {
public function getEngagementAssociations($engagement_uuid) {
$today = date('Y-m-d');
$ea_path = (
"/api/v1/engagement_association?engagement={$engagement_uuid}&at={$today}"
Expand Down
Loading

0 comments on commit 3f30062

Please sign in to comment.