[DependencyInjection] refactored code to avoid logic duplication