services:
    oro_message_queue.client.config:
        class: 'Oro\Component\MessageQueue\Client\Config'
        public: false

    oro_message_queue.client.driver_factory:
        class: 'Oro\Component\MessageQueue\Client\DriverFactory'
        public: false
        arguments:
            - []

    oro_message_queue.client.security_token_provider:
        class: Oro\Bundle\MessageQueueBundle\Security\SecurityTokenProvider
        arguments:
            - '@security.token_storage'

    oro_message_queue.client.security_aware_driver_factory:
        class: Oro\Bundle\MessageQueueBundle\Security\SecurityAwareDriverFactory
        decorates: oro_message_queue.client.driver_factory
        arguments:
            - '@.inner'
            - [] # security agnostic topics
            - '@oro_message_queue.client.security_token_provider'
            - '@oro_security.token_serializer'

    oro_message_queue.platform.optional_listener_driver_factory:
        class: Oro\Bundle\MessageQueueBundle\Platform\OptionalListenerDriverFactory
        public: false
        decorates: oro_message_queue.client.driver_factory
        decoration_priority: -255
        arguments:
            - '@.inner'
            - '@oro_platform.optional_listeners.manager'

    oro_message_queue.job.job_middleware:
        class: 'Oro\Component\MessageQueue\Job\JobMiddleware'
        arguments:
            - '@oro_message_queue.job.runner'
            - '@oro_message_queue.topic.registry'
            - '@oro_message_queue.job.processor'
        tags:
            - { name: oro_message_queue.middleware }

    oro_message_queue.client.driver:
        class: 'Oro\Component\MessageQueue\Client\DriverInterface'
        public: true
        factory: ['@oro_message_queue.client.driver_factory', 'create']
        arguments:
            - '@oro_message_queue.transport.connection'
            - '@oro_message_queue.client.config'

    oro_message_queue.client.message_producer:
        class: 'Oro\Component\MessageQueue\Client\MessageProducer'
        lazy: true
        arguments:
            - '@oro_message_queue.client.driver'
            - '@oro_message_queue.router.message_router'
            - '%kernel.debug%'
        calls:
            - [setLogger, ['@logger']]
            - [setMiddlewares, [!tagged_iterator oro_message_queue.middleware]]

    Oro\Component\MessageQueue\Client\MessageProducerInterface:
        alias: 'oro_message_queue.client.message_producer'

    oro_message_queue.message_producer:
        alias: 'oro_message_queue.client.message_producer'
        public: true

    oro_message_queue.router.message_router:
        class: 'Oro\Component\MessageQueue\Client\Router\MessageRouter'
        arguments:
            - '@oro_message_queue.client.driver'
            - '@oro_message_queue.topic.registry'
            - '@oro_message_queue.client.meta.topic_meta_registry'
            - '@oro_message_queue.client.meta.destination_meta_registry'

    oro_message_queue.client.message_processor_registry:
        class: 'Oro\Component\MessageQueue\Client\MessageProcessorRegistry'
        arguments:
            - !abstract message processors defined in BuildMessageProcessorRegistryPass
        tags: ['container.service_locator']

    oro_message_queue.client.meta.topic_meta_registry:
        class: 'Oro\Component\MessageQueue\Client\Meta\TopicMetaRegistry'
        arguments:
            - $queuesByTopic: defined in BuildTopicMetaRegistryPass
            - $messageProcessorsByTopicAndQueue: defined in BuildTopicMetaRegistryPass

    oro_message_queue.client.meta.destination_meta_registry:
        class: 'Oro\Component\MessageQueue\Client\Meta\DestinationMetaRegistry'
        public: true
        arguments:
            - '@oro_message_queue.client.config'
            - $messageProcessorsByQueue: !abstract defined in BuildDestinationMetaRegistryPass

    oro_message_queue.client.meta.topic_description_provider:
        class: 'Oro\Component\MessageQueue\Client\Meta\TopicDescriptionProvider'
        arguments:
            - '@oro_message_queue.topic.registry'

    oro_message_queue.client.noop_message_processor:
        class: 'Oro\Component\MessageQueue\Client\NoopMessageProcessor'
        arguments:
            - '%oro_message_queue.client.noop_status%'

    oro_message_queue.client.extension.create_queue:
        class: 'Oro\Component\MessageQueue\Client\ConsumptionExtension\CreateQueueExtension'
        arguments:
            - '@oro_message_queue.client.driver'
            - '@oro_message_queue.client.created_queues'
        tags:
            - { name: 'oro_message_queue.consumption.extension', priority: -10 }

    oro_message_queue.client.extension.message_body_resolver:
        class: 'Oro\Component\MessageQueue\Client\ConsumptionExtension\MessageBodyResolverExtension'
        arguments:
            - '@oro_message_queue.topic.registry'
            - '@oro_message_queue.client.message_body_resolver'
        tags:
            # Priority should be high enough to ensure that extensions which need message body are called later.
            - { name: 'oro_message_queue.consumption.extension', persistent: true, priority: 250 }

    oro_message_queue.client.extension.message_processor_router:
        class: 'Oro\Component\MessageQueue\Client\ConsumptionExtension\MessageProcessorRouterExtension'
        arguments:
            - '@oro_message_queue.client.meta.topic_meta_registry'
            - '@oro_message_queue.client.meta.destination_meta_registry'
            - 'oro_message_queue.client.noop_message_processor'
        tags:
            # Priority should be high enough to ensure that extensions which need message processor are called later.
            - { name: 'oro_message_queue.consumption.extension', persistent: true, priority: 128 }

    oro_message_queue.client.queue_consumer:
        class: 'Oro\Component\MessageQueue\Consumption\QueueConsumer'
        arguments:
            - '@oro_message_queue.transport.connection'
            - '@oro_message_queue.consumption.extensions'
            - '@oro_message_queue.log.consumer_state'
            - '@oro_message_queue.client.message_processor_registry'

    oro_message_queue.client.created_queues:
        class: 'Oro\Component\MessageQueue\Transport\QueueCollection'

    oro_message_queue.profiler.message_queue_collector:
        class: Oro\Bundle\MessageQueueBundle\Profiler\MessageQueueCollector
        arguments:
            - '@oro_message_queue.message_producer'
        tags:
            - { name: data_collector, template: '@OroMessageQueue/Collector/message_queue.html.twig', id: message_queue }

    oro_message_queue.client.buffered_message_producer:
        class: Oro\Bundle\MessageQueueBundle\Client\BufferedMessageProducer
        decorates: oro_message_queue.client.message_producer
        decoration_priority: -255 # the buffered producer should be at the top of the decoration chain
        arguments:
            - '@.inner'
            - '@logger'
            - '@oro_message_queue.client.message_filter'

    oro_message_queue.client.message_buffer_manager:
        class: Oro\Bundle\MessageQueueBundle\Client\MessageBufferManager
        arguments:
            - '@oro_message_queue.client.buffered_message_producer'
            - '@doctrine'
            - 'default'

    oro_message_queue.client.message_filter:
        class: Oro\Bundle\MessageQueueBundle\Client\ChainMessageFilter
        arguments:
            - [] # filters
            - ~ # container

    oro_message_queue.client.dbal_transaction_watcher:
        class: Oro\Bundle\MessageQueueBundle\Client\DbalTransactionWatcher
        arguments:
            - '@oro_message_queue.client.buffered_message_producer'
        tags:
            - { name: oro.doctrine.connection.transaction_watcher }

    oro_message_queue.client.request_watcher:
        class: Oro\Bundle\MessageQueueBundle\Client\RequestWatcher
        arguments:
            - '@oro_message_queue.client.buffered_message_producer'
        tags:
            - { name: kernel.event_subscriber }

    oro_message_queue.client.command_watcher:
        class: Oro\Bundle\MessageQueueBundle\Client\CommandWatcher
        arguments:
            - '@oro_message_queue.client.buffered_message_producer'
        tags:
            - { name: kernel.event_subscriber }

    oro_message_queue.client.processor_watcher:
        class: Oro\Bundle\MessageQueueBundle\Client\ProcessorWatcher
        arguments:
            - '@oro_message_queue.client.buffered_message_producer'
        tags:
            - { name: oro_message_queue.consumption.extension, priority: 255 }

    oro_message_queue.client.message_body_resolver:
        class: Oro\Component\MessageQueue\Client\MessageBodyResolver
        arguments:
            - '@oro_message_queue.topic.registry'

    oro_message_queue.client.extension.child_job_failing:
        class: 'Oro\Component\MessageQueue\Client\ConsumptionExtension\ChildJobFailingExtension'
        arguments:
            - '@oro_message_queue.job.processor'
        tags:
            - { name: 'oro_message_queue.consumption.extension' }

    oro_message_queue.job.job_extension:
        class: 'Oro\Component\MessageQueue\Job\JobExtension'
        arguments:
            - '@oro_message_queue.job.runner'
        tags:
            - { name: 'oro_message_queue.consumption.extension'}
