state([ 'type' => 'inventory_sync', 'status' => self::STATUS_SUCCESS, 'outcome' => 'succeeded', 'context' => [ 'selection_hash' => hash('sha256', 'inventory-sync-selection-default'), 'policy_types' => ['deviceConfiguration'], 'categories' => [], 'include_foundations' => false, 'include_dependencies' => false, ], 'started_at' => now()->subMinute(), 'completed_at' => now(), ]); } protected static function booted(): void { static::addGlobalScope('inventory_sync_type', function (Builder $builder): void { $builder->where('type', 'inventory_sync'); }); static::addGlobalScope('legacy_statuses_only', function (Builder $builder): void { $builder->whereIn('status', [ self::STATUS_PENDING, self::STATUS_SUCCESS, self::STATUS_PARTIAL, self::STATUS_FAILED, self::STATUS_SKIPPED, ]); }); static::saving(function (self $run): void { if (! filled($run->type)) { $run->type = 'inventory_sync'; } $legacyStatus = (string) $run->status; $normalizedStatus = match ($legacyStatus) { self::STATUS_PENDING => 'queued', self::STATUS_RUNNING => 'running', self::STATUS_SUCCESS, self::STATUS_PARTIAL, self::STATUS_FAILED, self::STATUS_SKIPPED => 'completed', default => $legacyStatus, }; if ($normalizedStatus !== '') { $run->status = $normalizedStatus; } $context = is_array($run->context) ? $run->context : []; if (! array_key_exists('selection_hash', $context) || ! is_string($context['selection_hash']) || $context['selection_hash'] === '') { $context['selection_hash'] = hash('sha256', (string) $run->getKey().':selection'); } $run->context = $context; $run->outcome = match ($legacyStatus) { self::STATUS_SUCCESS => 'succeeded', self::STATUS_PARTIAL => 'partially_succeeded', self::STATUS_SKIPPED => 'blocked', self::STATUS_RUNNING, self::STATUS_PENDING => 'pending', default => 'failed', }; if (in_array($legacyStatus, [self::STATUS_SUCCESS, self::STATUS_PARTIAL, self::STATUS_FAILED, self::STATUS_SKIPPED], true) && $run->completed_at === null ) { $run->completed_at = now(); } }); } public function getSelectionHashAttribute(): ?string { $context = is_array($this->context) ? $this->context : []; return isset($context['selection_hash']) && is_string($context['selection_hash']) ? $context['selection_hash'] : null; } public function setSelectionHashAttribute(?string $value): void { $context = is_array($this->context) ? $this->context : []; $context['selection_hash'] = $value; $this->context = $context; } /** * @return array */ public function getSelectionPayloadAttribute(): array { $context = is_array($this->context) ? $this->context : []; return Arr::only($context, [ 'policy_types', 'categories', 'include_foundations', 'include_dependencies', ]); } /** * @param array|null $value */ public function setSelectionPayloadAttribute(?array $value): void { $context = is_array($this->context) ? $this->context : []; if (is_array($value)) { $context = array_merge($context, Arr::only($value, [ 'policy_types', 'categories', 'include_foundations', 'include_dependencies', ])); } $this->context = $context; } public function getFinishedAtAttribute(): mixed { return $this->completed_at; } public function setFinishedAtAttribute(mixed $value): void { $this->completed_at = $value; } }