Skip to content

Commit

Permalink
APIv4 - Cleanup factory signature, remove overridden SelectQuery object
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Feb 25, 2022
1 parent 85843f7 commit a4fb482
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 132 deletions.
3 changes: 3 additions & 0 deletions CRM/Eck/DAO/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ public static function writeRecord(array $record): CRM_Core_DAO {
* {@inheritDoc}
*/
public static function deleteRecord(array $record) {
if (empty($record['entity_type'])) {
throw new CRM_Core_Exception("Eck entity type not specified.");
}
$entityName = 'Eck_' . $record['entity_type'];
if (empty($record['id'])) {
throw new CRM_Core_Exception("Cannot delete {$entityName} with no id.");
Expand Down
31 changes: 31 additions & 0 deletions Civi/Api4/EckDAODeleteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*-------------------------------------------------------+
| CiviCRM Entity Construction Kit |
| Copyright (C) 2021 SYSTOPIA |
| Author: J. Schuppe ([email protected]) |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

namespace Civi\Api4;

use CRM_Eck_ExtensionUtil as E;

class EckDAODeleteAction extends Generic\DAODeleteAction {

protected function deleteObjects($items) {
$entityType = \CRM_Eck_BAO_Entity::getEntityType($this->getEntityName());
foreach ($items as &$item) {
$item['entity_type'] = $entityType;
}

return parent::deleteObjects($items);
}

}
49 changes: 0 additions & 49 deletions Civi/Api4/EckDAOGetAction.php

This file was deleted.

92 changes: 64 additions & 28 deletions Civi/Api4/EckEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use CRM_Eck_ExtensionUtil as E;
use Civi\Api4\Generic\BasicReplaceAction;
use Civi\Api4\Generic\CheckAccessAction;
use Civi\Api4\Generic\DAODeleteAction;
use Civi\Api4\Generic\DAOGetAction;
use Civi\Api4\Generic\DAOGetFieldsAction;
use Civi\Api4\Action\GetActions;

Expand All @@ -33,67 +33,103 @@
class EckEntity {

/**
* {@inheritDoc}
* @param string $entity_type
* @param bool $checkPermissions
* @return DAOGetFieldsAction
*/
public static function getFields($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
public static function getFields(string $entity_type, $checkPermissions = TRUE) {
return (new DAOGetFieldsAction('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function get($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
return (new EckDAOGetAction('Eck_' . $entity_type, __FUNCTION__))
/**
* @param string $entity_type
* @param bool $checkPermissions
* @return DAOGetAction
* @throws \API_Exception
*/
public static function get(string $entity_type, $checkPermissions = TRUE) {
return (new DAOGetAction('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function save($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
/**
* @param string $entity_type
* @param bool $checkPermissions
* @return EckDAOSaveAction
* @throws \API_Exception
*/
public static function save(string $entity_type, $checkPermissions = TRUE) {
return (new EckDAOSaveAction('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function create($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
/**
* @param string $entity_type
* @param bool $checkPermissions
* @return EckDAOCreateAction
* @throws \API_Exception
*/
public static function create(string $entity_type, $checkPermissions = TRUE) {
return (new EckDAOCreateAction('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function update($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
/**
* @param string $entity_type
* @param bool $checkPermissions
* @return EckDAOUpdateAction
* @throws \API_Exception
*/
public static function update(string $entity_type, $checkPermissions = TRUE) {
return (new EckDAOUpdateAction('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function delete($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
return (new DAODeleteAction('Eck_' . $entity_type, __FUNCTION__))
/**
* @param string $entity_type
* @param bool $checkPermissions
* @return EckDAODeleteAction
* @throws \API_Exception
*/
public static function delete(string $entity_type, $checkPermissions = TRUE) {
return (new EckDAODeleteAction('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function replace($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
/**
* @param string $entity_type
* @param bool $checkPermissions
* @return BasicReplaceAction
* @throws \API_Exception
*/
public static function replace(string $entity_type, $checkPermissions = TRUE) {
return (new BasicReplaceAction('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function getActions($checkPermissions = TRUE) {
[,$entity_type] = func_get_args();
/**
* @param string $entity_type
* @param bool $checkPermissions
* @return GetActions
*/
public static function getActions(string $entity_type, $checkPermissions = TRUE) {
return (new GetActions('Eck_' . $entity_type, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

public static function checkAccess() {
[,$entity_type] = func_get_args();
/**
* @param string $entity_type
* @return CheckAccessAction
* @throws \API_Exception
*/
public static function checkAccess(string $entity_type) {
return new CheckAccessAction('Eck_' . $entity_type, __FUNCTION__);
}

protected static function getEntityTitle($plural = FALSE) {
[,$entity_type] = func_get_args();
$dao = \CRM_Core_DAO_AllCoreTables::getFullName($entity_type);
return $dao ? $dao::getEntityTitle($plural) : ($plural ? \CRM_Utils_String::pluralize($entity_type) : $entity_type);
}

/**
* @return array
*/
public static function permissions() {
return []; // FIXME: Add per-entity-type permissions
}
Expand Down
54 changes: 0 additions & 54 deletions Civi/Eck/API/Api4SelectQuery.php

This file was deleted.

2 changes: 1 addition & 1 deletion Civi/Eck/API/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function onApi4CreateRequest(CreateApi4RequestEvent $event) {
in_array($entity_type, \CRM_Eck_BAO_EckEntityType::getEntityTypeNames())
) {
$event->className = 'Civi\Api4\EckEntity';
$event->args = [TRUE, $entity_type];
$event->args = [$entity_type];
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/api/v4/EckEntity/EckEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ public function testTwoEntityTypes() {
]);
$this->assertCount(1, $deleted);

$firstRecordCount = civicrm_api4($firstEntity, 'get', [
'checkPermissions' => FALSE,
'select' => ['row_count'],
]);
$this->assertCount(1, $firstRecordCount);

$secondRecords = civicrm_api4($secondEntity, 'get', [
'checkPermissions' => FALSE,
'orderBy' => ['subtype' => 'ASC'],
Expand Down

0 comments on commit a4fb482

Please sign in to comment.