|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ProcessMaker\Services; |
| 6 | + |
| 7 | +use Carbon\Carbon; |
| 8 | +use ProcessMaker\Models\Process; |
| 9 | + |
| 10 | +class CaseRetentionTierService |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @deprecated Stored only for backward compatibility; use {@see self::NOTICE_AT_PROPERTY_KEY}. |
| 14 | + */ |
| 15 | + public const NOTICE_PROPERTY_KEY = 'case_retention_tier_adjustment_notice'; |
| 16 | + |
| 17 | + public const NOTICE_AT_PROPERTY_KEY = 'case_retention_tier_adjustment_notice_at'; |
| 18 | + |
| 19 | + public const NOTICE_DURATION_HOURS = 24; |
| 20 | + |
| 21 | + private const VALID_PERIODS = ['six_months', 'one_year', 'three_years', 'five_years']; |
| 22 | + |
| 23 | + /** @var array<string, int> */ |
| 24 | + private const PERIOD_MONTHS = [ |
| 25 | + 'six_months' => 6, |
| 26 | + 'one_year' => 12, |
| 27 | + 'three_years' => 36, |
| 28 | + 'five_years' => 60, |
| 29 | + ]; |
| 30 | + |
| 31 | + /** |
| 32 | + * Whether the process-listing warning for a tier-driven retention clamp should show (admins only). |
| 33 | + */ |
| 34 | + public static function adjustmentNoticeIsActive(Process $process): bool |
| 35 | + { |
| 36 | + $props = $process->properties ?? []; |
| 37 | + $at = $props[self::NOTICE_AT_PROPERTY_KEY] ?? null; |
| 38 | + if (is_string($at) && $at !== '') { |
| 39 | + return Carbon::parse($at)->greaterThan(now()->subHours(self::NOTICE_DURATION_HOURS)); |
| 40 | + } |
| 41 | + |
| 42 | + if (filter_var($props[self::NOTICE_PROPERTY_KEY] ?? false, FILTER_VALIDATE_BOOLEAN)) { |
| 43 | + $updatedAt = $process->updated_at; |
| 44 | + |
| 45 | + return $updatedAt && Carbon::parse($updatedAt)->greaterThan(now()->subHours(self::NOTICE_DURATION_HOURS)); |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Retention period options allowed for the configured CASE_RETENTION_TIER. |
| 53 | + * |
| 54 | + * @return list<string> |
| 55 | + */ |
| 56 | + public static function allowedPeriodsForCurrentTier(): array |
| 57 | + { |
| 58 | + $tier = (string) config('app.case_retention_tier', '1'); |
| 59 | + $options = config('app.case_retention_tier_options', []); |
| 60 | + |
| 61 | + return $options[$tier] ?? $options['1'] ?? ['six_months', 'one_year']; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Longest retention period in the allowed list (by duration), not by array order. |
| 66 | + * |
| 67 | + * @param list<string> $allowed |
| 68 | + */ |
| 69 | + public static function longestAllowedPeriod(array $allowed): string |
| 70 | + { |
| 71 | + $best = 'one_year'; |
| 72 | + $bestMonths = 0; |
| 73 | + foreach ($allowed as $period) { |
| 74 | + if (!is_string($period)) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + $months = self::PERIOD_MONTHS[$period] ?? null; |
| 78 | + if ($months === null) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + if ($months > $bestMonths) { |
| 82 | + $bestMonths = $months; |
| 83 | + $best = $period; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return $bestMonths > 0 ? $best : 'one_year'; |
| 88 | + } |
| 89 | + |
| 90 | + public static function normalizePeriod(mixed $period): string |
| 91 | + { |
| 92 | + if (is_string($period) && in_array($period, self::VALID_PERIODS, true)) { |
| 93 | + return $period; |
| 94 | + } |
| 95 | + |
| 96 | + return 'one_year'; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * If the process retention period is not allowed for the current tier, set it to the |
| 101 | + * longest period allowed for that tier, refresh retention_updated_at, clear retention_updated_by |
| 102 | + * (so the UI shows the default retention message), and record when to show the admin notice (24h). |
| 103 | + * |
| 104 | + * @param list<string>|null $tierAllowedPeriods When null, resolved from config; when set (e.g. from a |
| 105 | + * batch command), avoids re-reading tier options per process. |
| 106 | + * @return bool True when the process was updated. |
| 107 | + */ |
| 108 | + public static function clampProcessRetentionToCurrentTier(Process $process, ?array $tierAllowedPeriods = null): bool |
| 109 | + { |
| 110 | + $allowed = $tierAllowedPeriods ?? self::allowedPeriodsForCurrentTier(); |
| 111 | + $current = self::normalizePeriod($process->properties['retention_period'] ?? null); |
| 112 | + |
| 113 | + if (in_array($current, $allowed, true)) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + $maxPeriod = self::longestAllowedPeriod($allowed); |
| 118 | + $properties = $process->properties ?? []; |
| 119 | + $properties['retention_period'] = $maxPeriod; |
| 120 | + $properties['retention_updated_at'] = now()->toIso8601String(); |
| 121 | + unset($properties['retention_updated_by']); |
| 122 | + unset($properties[self::NOTICE_PROPERTY_KEY]); |
| 123 | + $properties[self::NOTICE_AT_PROPERTY_KEY] = now()->toIso8601String(); |
| 124 | + $process->properties = $properties; |
| 125 | + $process->saveQuietly(); |
| 126 | + |
| 127 | + return true; |
| 128 | + } |
| 129 | +} |
0 commit comments