Skip to content

A free open source IT asset/license management system

Update addigy sync adapter with new sync adapter format

snipe14cc5efa714e
diff --git a/app/SyncAdapters/Addigy/AddigyAdapter.php b/app/SyncAdapters/Addigy/AddigyAdapter.php
index 6fd2998170..710dceb5e4 100644
--- a/app/SyncAdapters/Addigy/AddigyAdapter.php
+++ b/app/SyncAdapters/Addigy/AddigyAdapter.php
@@ -7,8 +7,7 @@ use App\Models\AssetExternalSource;
use App\Models\SyncAdapterConfig;
use App\Models\User;
use App\SyncAdapters\HostInventoryRecord;
-use App\SyncAdapters\PushableAdapter;
-use App\SyncAdapters\SyncAdapter;
+use App\SyncAdapters\PushableSyncAdapter;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
@@ -24,7 +23,7 @@ use Illuminate\Support\Facades\Log;
* user -> Addigy end-user tied to the device) is opt-in per instance
* because it requires an Addigy end-user ID lookup per push.
*/
-class AddigyAdapter extends SyncAdapter implements PushableAdapter
+class AddigyAdapter extends PushableSyncAdapter
{
/**
* Device-fact identifiers to request on every `POST /devices` call.
diff --git a/app/SyncAdapters/SyncAdapter.php b/app/SyncAdapters/SyncAdapter.php
index 2e1dd93f96..0f84cf9ead 100644
--- a/app/SyncAdapters/SyncAdapter.php
+++ b/app/SyncAdapters/SyncAdapter.php
@@ -3,7 +3,6 @@
namespace App\SyncAdapters;
use App\Models\Asset;
-use App\Models\CustomField;
use App\Models\SyncAdapterConfig;
use App\Models\SyncAdapterInstance;
use App\Rules\ExternalUrl;
@@ -159,9 +158,9 @@ abstract class SyncAdapter
* the docs page (vendor's own for named adapters, Snipe-IT's own
* for Custom HTTP). docs_url is null for adapters whose vendor
* has no stable public reference URL or gates its docs behind a
- * login. direction is derived from whether the class implements
- * PushableAdapter so the catalog UI reflects reality without a
- * separate list for admins or docs to drift from.
+ * login. direction is derived from whether the class extends
+ * PushableSyncAdapter so the catalog UI reflects reality without
+ * a separate list for admins or docs to drift from.
*
* @return array<string, array{label: string, docs_url: ?string, direction: 'pull'|'both'}>
*/
@@ -172,7 +171,7 @@ abstract class SyncAdapter
$out[$slug] = [
'label' => $class::typeLabel(),
'docs_url' => $class::docsUrl(),
- 'direction' => is_a($class, PushableAdapter::class, true) ? 'both' : 'pull',
+ 'direction' => is_subclass_of($class, PushableSyncAdapter::class) ? 'both' : 'pull',
];
}
@@ -1029,24 +1028,24 @@ abstract class SyncAdapter
\Log::debug("This should never be called - there's no such thing as 'direction' for pull-only SyncAdapters");
// Though, maybe, there is? 'skip' is a direction, and you might use that?
// TODO - this is copypasta from PushableSyncAdapter, but without it, tests won't pass
- $posted = (array) $request->input($slug . '_direction', []);
+ $posted = (array) $request->input($slug.'_direction', []);
// Widgets' picker rows submit their unfinished pair under
// <slug>_<map_key>_pending[key|value] so admins can save
// without clicking + to commit the row first.
- $pairedMapKeys = [$slug . '_mapping_pending'];
+ $pairedMapKeys = [$slug.'_mapping_pending'];
foreach ($this->settingsSchema() as $field) {
if (($field['type'] ?? '') === 'field_map') {
- $pairedMapKeys[] = $slug . '_' . $field['key'] . '_pending';
+ $pairedMapKeys[] = $slug.'_'.$field['key'].'_pending';
}
}
- $directionPending = (array) $request->input($slug . '_direction_pending', []);
+ $directionPending = (array) $request->input($slug.'_direction_pending', []);
$pendingDir = is_string($directionPending['value'] ?? null) ? trim($directionPending['value']) : '';
foreach ($pairedMapKeys as $pendingField) {
$pairedPending = (array) $request->input($pendingField, []);
$pendingKey = is_string($pairedPending['key'] ?? null) ? trim($pairedPending['key']) : '';
$pendingValue = is_string($pairedPending['value'] ?? null) ? trim($pairedPending['value']) : '';
- if ($pendingKey !== '' && $pendingValue !== '' && $pendingDir !== '' && !array_key_exists($pendingKey, $posted)) {
+ if ($pendingKey !== '' && $pendingValue !== '' && $pendingDir !== '' && ! array_key_exists($pendingKey, $posted)) {
$posted[$pendingKey] = $pendingDir;
}
}
@@ -1059,20 +1058,20 @@ abstract class SyncAdapter
$pushable = MappingTargets::FIELDS;
foreach ($posted as $field => $direction) {
- if (!in_array($field, $validFields, true)) {
+ if (! in_array($field, $validFields, true)) {
continue;
}
- if (!in_array($direction, ['pull', 'push', 'skip', 'both'], true)) {
+ if (! in_array($direction, ['pull', 'push', 'skip', 'both'], true)) {
continue;
}
if (
in_array($field, MappingTargets::FIELDS, true)
- && !in_array($field, $pushable, true)
+ && ! in_array($field, $pushable, true)
&& in_array($direction, ['push', 'both'], true)
) {
$direction = 'pull';
}
- SyncAdapterConfig::put($this->instance->id, 'direction.' . $field, (string) $direction);
+ SyncAdapterConfig::put($this->instance->id, 'direction.'.$field, (string) $direction);
}
// Same forget-on-omit rule as persistFieldMappings for extras:
@@ -1080,8 +1079,8 @@ abstract class SyncAdapter
// POST, and their direction needs clearing so it doesn't
// linger on the next sync if the extra gets re-added.
foreach (array_keys($this->extraFields()) as $extraKey) {
- if (!array_key_exists($extraKey, $posted)) {
- SyncAdapterConfig::forget($this->instance->id, 'direction.' . $extraKey);
+ if (! array_key_exists($extraKey, $posted)) {
+ SyncAdapterConfig::forget($this->instance->id, 'direction.'.$extraKey);
}
}
}
diff --git a/tests/Feature/SyncAdapters/TypeCatalogDirectionTest.php b/tests/Feature/SyncAdapters/TypeCatalogDirectionTest.php
index c136605743..a088417dd0 100644
--- a/tests/Feature/SyncAdapters/TypeCatalogDirectionTest.php
+++ b/tests/Feature/SyncAdapters/TypeCatalogDirectionTest.php
@@ -5,10 +5,16 @@ namespace Tests\Feature\SyncAdapters;
use App\SyncAdapters\Addigy\AddigyAdapter;
use App\SyncAdapters\Jamf\JamfAdapter;
use App\SyncAdapters\Osctrl\OsctrlAdapter;
-use App\SyncAdapters\PushableAdapter;
+use App\SyncAdapters\PushableSyncAdapter;
use App\SyncAdapters\SyncAdapter;
use Tests\TestCase;
+/**
+ * Pins the direction key SyncAdapter::typeCatalog() derives from each
+ * adapter class so the catalog UI in the help pane reflects reality
+ * without a hand-maintained list that would drift away from the actual
+ * code every time an adapter gained push support.
+ */
class TypeCatalogDirectionTest extends TestCase
{
public function test_catalog_entry_shape_includes_direction_key(): void
@@ -24,18 +30,18 @@ class TypeCatalogDirectionTest extends TestCase
}
}
- public function test_direction_matches_pushable_adapter_interface_implementation(): void
+ public function test_direction_matches_pushable_sync_adapter_subclassing(): void
{
$catalog = SyncAdapter::typeCatalog();
$types = SyncAdapter::allTypes();
foreach ($catalog as $slug => $entry) {
$class = $types[$slug];
- $expected = is_a($class, PushableAdapter::class, true) ? 'both' : 'pull';
+ $expected = is_subclass_of($class, PushableSyncAdapter::class) ? 'both' : 'pull';
$this->assertSame(
$expected,
$entry['direction'],
- "{$slug} direction derivation drifted from PushableAdapter implementation.",
+ "{$slug} direction derivation drifted from PushableSyncAdapter subclassing.",
);
}
}
@@ -44,25 +50,25 @@ class TypeCatalogDirectionTest extends TestCase
{
// osctrl is pull-only. Guards against a regression where every
// adapter would start reporting 'both' due to a derivation bug.
- $this->assertFalse(is_a(OsctrlAdapter::class, PushableAdapter::class, true));
+ $this->assertFalse(is_subclass_of(OsctrlAdapter::class, PushableSyncAdapter::class));
$this->assertSame('pull', SyncAdapter::typeCatalog()['osctrl']['direction']);
}
public function test_push_capable_adapter_reports_both(): void
{
- // Jamf implements PushableAdapter. Guards against a regression
+ // Jamf extends PushableSyncAdapter. Guards against a regression
// where push-capable adapters would silently be downgraded to
// pull-only in the catalog UI.
- $this->assertTrue(is_a(JamfAdapter::class, PushableAdapter::class, true));
+ $this->assertTrue(is_subclass_of(JamfAdapter::class, PushableSyncAdapter::class));
$this->assertSame('both', SyncAdapter::typeCatalog()['jamf']['direction']);
}
public function test_addigy_direction_reflects_current_capability(): void
{
- // Addigy's direction tracks whatever PushableAdapter state the
- // class is in. This test proves the catalog follows the class
- // without an explicit list in the catalog method.
- $expected = is_a(AddigyAdapter::class, PushableAdapter::class, true) ? 'both' : 'pull';
+ // Addigy's direction tracks whatever PushableSyncAdapter state
+ // the class is in. This test proves the catalog follows the
+ // class without an explicit list in the catalog method.
+ $expected = is_subclass_of(AddigyAdapter::class, PushableSyncAdapter::class) ? 'both' : 'pull';
$this->assertSame($expected, SyncAdapter::typeCatalog()['addigy']['direction']);
}
}