|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | +import 'package:flutter_policy_engine/src/core/interfaces/i_policy_evaluator.dart'; |
| 3 | +import 'package:flutter_policy_engine/src/core/interfaces/i_policy_storage.dart'; |
| 4 | +import 'package:flutter_policy_engine/src/core/memory_policy_storage.dart'; |
| 5 | +import 'package:flutter_policy_engine/src/core/role_evaluator.dart'; |
| 6 | +import 'package:flutter_policy_engine/src/models/policy.dart'; |
| 7 | +import 'package:flutter_policy_engine/src/utils/json_handler.dart'; |
| 8 | +import 'package:flutter_policy_engine/src/utils/log_handler.dart'; |
| 9 | + |
| 10 | +/// Manages policy lifecycle and provides centralized access to policy operations. |
| 11 | +/// |
| 12 | +/// The [PolicyManager] serves as the main entry point for policy-related operations, |
| 13 | +/// coordinating between storage, evaluation, and policy state management. It extends |
| 14 | +/// [ChangeNotifier] to notify listeners of policy state changes. |
| 15 | +/// |
| 16 | +/// Example usage: |
| 17 | +/// ```dart |
| 18 | +/// final policyManager = PolicyManager( |
| 19 | +/// storage: MyPolicyStorage(), |
| 20 | +/// evaluator: MyPolicyEvaluator(), |
| 21 | +/// ); |
| 22 | +/// |
| 23 | +/// await policyManager.initialize(policyJsonData); |
| 24 | +/// ``` |
| 25 | +class PolicyManager extends ChangeNotifier { |
| 26 | + /// Creates a new [PolicyManager] instance. |
| 27 | + /// |
| 28 | + /// [storage] is responsible for persisting and retrieving policy data. |
| 29 | + /// [evaluator] handles policy evaluation logic and decision making. |
| 30 | + PolicyManager({ |
| 31 | + IPolicyStorage? storage, |
| 32 | + IPolicyEvaluator? evaluator, |
| 33 | + }) : _storage = storage ?? MemoryPolicyStorage(), |
| 34 | + _evaluator = evaluator; |
| 35 | + |
| 36 | + /// The storage implementation for policy persistence. |
| 37 | + final IPolicyStorage _storage; |
| 38 | + |
| 39 | + /// The evaluator implementation for policy decision making. |
| 40 | + IPolicyEvaluator? _evaluator; |
| 41 | + |
| 42 | + /// Internal cache of loaded policies, keyed by policy identifier. |
| 43 | + Map<String, Policy> _policies = {}; |
| 44 | + |
| 45 | + /// Indicates whether the policy manager has been initialized with policy data. |
| 46 | + bool _isInitialized = false; |
| 47 | + |
| 48 | + /// Returns whether the policy manager has been initialized. |
| 49 | + /// |
| 50 | + /// Returns `true` if [initialize] has been called successfully, `false` otherwise. |
| 51 | + bool get isInitialized => _isInitialized; |
| 52 | + |
| 53 | + /// Returns an unmodifiable view of all loaded policies. |
| 54 | + /// |
| 55 | + /// The returned map cannot be modified directly. Use [initialize] to update |
| 56 | + /// the policy collection. |
| 57 | + Map<String, Policy> get policies => Map.unmodifiable(_policies); |
| 58 | + |
| 59 | + /// Initializes the policy manager with policy data from JSON. |
| 60 | + /// |
| 61 | + /// Parses the provided [jsonPolicies] and loads them into the internal cache. |
| 62 | + /// This method should be called before using any policy-related functionality. |
| 63 | + /// |
| 64 | + /// [jsonPolicies] should be a map where keys are policy identifiers and values |
| 65 | + /// are JSON representations of [Policy] objects. |
| 66 | + /// |
| 67 | + /// Throws: |
| 68 | + /// - [JsonParseException] if policy parsing fails completely |
| 69 | + /// - [FormatException] if the JSON data is malformed |
| 70 | + /// - [ArgumentError] if policy parsing fails |
| 71 | + Future<void> initialize(Map<String, dynamic> jsonPolicies) async { |
| 72 | + try { |
| 73 | + LogHandler.info( |
| 74 | + 'Initializing policy manager', |
| 75 | + context: { |
| 76 | + 'policy_count': jsonPolicies.length, |
| 77 | + 'policy_keys': jsonPolicies.keys.take(5).toList(), |
| 78 | + }, |
| 79 | + operation: 'policy_manager_initialize', |
| 80 | + ); |
| 81 | + |
| 82 | + _policies = JsonHandler.parseMap( |
| 83 | + jsonPolicies, |
| 84 | + (json) => Policy.fromJson(json), |
| 85 | + context: 'policy_manager', |
| 86 | + allowPartialSuccess: |
| 87 | + true, // Allow partial success for graceful degradation |
| 88 | + ); |
| 89 | + |
| 90 | + // Only create evaluator if we have at least some policies |
| 91 | + if (_policies.isNotEmpty) { |
| 92 | + _evaluator = RoleEvaluator(_policies); |
| 93 | + await _storage.savePolicies(_policies); |
| 94 | + _isInitialized = true; |
| 95 | + |
| 96 | + LogHandler.info( |
| 97 | + 'Policy manager initialized successfully', |
| 98 | + context: { |
| 99 | + 'loaded_policies': _policies.length, |
| 100 | + 'total_policies': jsonPolicies.length, |
| 101 | + }, |
| 102 | + operation: 'policy_manager_initialized', |
| 103 | + ); |
| 104 | + } else { |
| 105 | + LogHandler.warning( |
| 106 | + 'Policy manager initialized with no valid policies', |
| 107 | + context: { |
| 108 | + 'total_policies': jsonPolicies.length, |
| 109 | + }, |
| 110 | + operation: 'policy_manager_empty', |
| 111 | + ); |
| 112 | + // Still mark as initialized but with empty policies |
| 113 | + _isInitialized = true; |
| 114 | + } |
| 115 | + |
| 116 | + notifyListeners(); |
| 117 | + } catch (e, stackTrace) { |
| 118 | + LogHandler.error( |
| 119 | + 'Failed to initialize policy manager', |
| 120 | + error: e, |
| 121 | + stackTrace: stackTrace, |
| 122 | + context: { |
| 123 | + 'policy_count': jsonPolicies.length, |
| 124 | + }, |
| 125 | + operation: 'policy_manager_initialize_error', |
| 126 | + ); |
| 127 | + |
| 128 | + // Re-throw to allow caller to handle the error |
| 129 | + rethrow; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments