0

I have Auth class which I am calling at one place in the code:

$this->auth = new Auth($this->options);

Now, the Options is the class containing things like host, port, username, pass... and is used within Auth constructor, but the only thing I need it for there is as a failsafe to initialize a new auth type if none is provided:

public function getAuthType()
{
    if (!$this->authType) {
        $this->authType = new Plain($this->options);
    }

    return $this->authType;
}

Auth types (Plain and DigestMD5) extend the abstract class Authentication which implements Authenticable interface.

Now the issue I have is that I have thought about dependency injection here, but failed to do the logic where I could enable the user to set a different auth type, so I thought of modifying Auth constructor like this:

public function __construct(Options $options, Authenticable $authType)
{
    $this->options = $options;
    $this->authType = $authType;
}

And while this would work, I have an issue here that upon initializing the class, the user would have to do something like:

new Auth($this->options, new DigestMD5($this->options)));

I don't like this at all, since passing the $this->options twice within the same constructor seems redundant, not to mention that it opens the possibility to feed Auth one set of options and DigestMD5 another one.

What is the best way to organize this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.