Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connector configurator #12

Merged
merged 3 commits into from
Jun 19, 2023
Merged

Connector configurator #12

merged 3 commits into from
Jun 19, 2023

Conversation

jenky
Copy link
Collaborator

@jenky jenky commented Jun 19, 2023

Description

Better way to add options for one-off request without modifying middleware globally.

Motivation and context

The decorator approach, such as RetryableConnector and FollowRedirectsConnector, has a limitation in that it cannot use methods other than those defined in the ConnectorInterface.

use Jenky\Atlas\Contracts\ConnectorInterface;
use Jenky\Atlas\Response;

interface MyConnectorInterface extends ConnectorInterface
{
    public function register(string $username, string $password): Response;
}

class MyConnector implements MyConnectorInterface
{
    public function register(string $username, string $password): Response
    {
        //....
    }
}
$connector = new RetryableConnector(new MyConnector());

// This won't work because RetryableConnector doesn't have `register` method
$response = $connector->register('foo', 'secret'); 

The ConnectorConfigurator solves this problem by dynamically modifying a connector with the desired behavior at runtime, and also allowing the IDE to suggest all the methods defined in the MyConnectorInterface.

$connector = (new ConnectorConfigurator())
    ->retry()
    ->configure(new MyConnector());

$response = $connector->register('foo', 'secret'); 

@jenky jenky merged commit 32222d9 into main Jun 19, 2023
2 of 7 checks passed
@jenky jenky deleted the connector-configurator branch June 21, 2023 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant