Skip to content

Do we need a dependency injection tag? #39

Closed
@Nyholm

Description

@Nyholm

When I use this bundle I starting to convert services that were previously dependent on Guzzle or that used the auto discovery. With this bundle I can inject the httplug.client and httplug.message_factory. I tend to write services that looks like this:

<?php

namespace Acme\CoreBundle\Service;

use Http\Client\HttpClient;
use Http\Message\MessageFactory;

/** 
 * A service that send data to the statistics server
 */
class StatisticsClient
{
    /**
     * @var string
     */
    private $apiUrl;

    /**
     * @var HttpClient
     */
    private $client;

    /**
     * @var MessageFactory
     */
    private $messageFactory;

    /**
     *
     * @param string $apiUrl
     * @param HttpClient $client
     * @param MessageFactory $messageFactory
     */
    public function __construct(HttpClient $client, MessageFactory $messageFactory, $apiUrl)
    {
        $this->apiUrl = $apiUrl;
        $this->client = $client;
        $this->messageFactory = $messageFactory;
    }

    /**
     * @param array  $data
     */
    public function post(array $data)
    {
        // do some logic on $data

        $request = $this->messageFactory->createRequest('POST', $this->apiUrl, [], json_encode($data));
        $this->client->sendRequest($request);
    }
}

All of them depends on httplug.client and httplug.message_factory. Im will add a dependency injection tag and a trait in my application to easily do a setter injection on services like this.
Question: Is that something that should live in the bundle?

Suggestion on tag name: httplug
Traits: ClientAccessorTrait, MessageFactoryAccessorTrait, StreamFactoryAccessorTrait, UrlFactoryAccessorTrait. All of them have a getter and setter for a private property.

EDIT:

Or maybe just one trait and one tag that will inject the httplug.client and httplug.message_factory only. Please correct me if I'm wrong but those will be the most used services.
Btw, this suggestion is all about developer experience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions