You should sometimes try something new with your code, you know. I tried wrapping the callable inside a try-finally block, so that the original tenant is always restored, even if an exception is thrown. You see that 'makeCurrent()' method, I made sure it calls on the original tenant ONLY if it exists, using the null coalescing operator ($originalCurrentTenant?->makeCurrent()). This will prevent an error in case the '$originalCurrentTenant' variable is null.
You should sometimes try something new with your code, you know. I tried wrapping the callable inside a try-finally block, so that the original tenant is always restored, even if an exception is thrown. You see that 'makeCurrent()' method, I made sure it calls on the original tenant ONLY if it exists, using the null coalescing operator ($originalCurrentTenant?->makeCurrent()). This will prevent an error in case the '$originalCurrentTenant' variable is null.