Last active
October 9, 2020 11:29
-
-
Save mohit-rocks/afad063a1dae9d0de2e001577a1d64be to your computer and use it in GitHub Desktop.
Creating Integrations Plugin in Mautic - Managing third-party Authentications in Mautic Plugin for Integrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApiConsumer | |
{ | |
// Other variables, methods declaration. | |
// Full code: https://github.com/mohit-rocks/HelloWorldBundle/blob/master/Connection/ApiConsumer.php | |
/** | |
* Fetch the data from API endpoint. | |
*/ | |
public function getSubscribers() | |
{ | |
$users = $this->client->get('v2/api/fetch-dummy-users'); | |
// Polish and manipulate the data. | |
return $users; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Few methods from client.php | |
// Full code: https://github.com/mohit-rocks/HelloWorldBundle/blob/master/Connection/Client.php | |
/** | |
* Get the client Client object from HttpFactory. | |
*/ | |
private function getClient(): ClientInterface | |
{ | |
$this->getApiHost(); | |
$credentials = $this->getCredentials(); | |
return $this->httpFactory->getClient($credentials); | |
} | |
/** | |
* Get the credentials object. | |
*/ | |
private function getCredentials(): Credentials | |
{ | |
if (!$this->config->isConfigured()) { | |
throw new PluginNotConfiguredException(); | |
} | |
$apiKeys = $this->config->getApiKeys(); | |
return new Credentials($apiKeys['key'], $apiKeys['secret']); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let plugin system know about the class, add following snippet in Config.php | |
// Full code: https://github.com/mohit-rocks/HelloWorldBundle/blob/master/Config/config.php | |
'services' => [ | |
'integrations' => [ | |
// Removed other services for better understanding. | |
// Provides the form types to use for the configuration UI | |
'mautic.integration.helloworld.configuration' => [ | |
'class' => \MauticPlugin\HelloWorldBundle\Integration\Support\ConfigSupport::class, | |
'tags' => [ | |
'mautic.config_integration', | |
], | |
], | |
], | |
], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Full code: https://github.com/mohit-rocks/HelloWorldBundle/blob/master/Config/config.php | |
'helloworld.connection.client' => [ | |
'class' => \MauticPlugin\HelloWorldBundle\Connection\Client::class, | |
'arguments' => [ | |
'mautic.integrations.auth_provider.basic_auth', | |
'mautic.helper.cache_storage', | |
'router', | |
'monolog.logger.mautic', | |
'helloworld.integration.config', | |
], | |
], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Full code: https://github.com/mohit-rocks/HelloWorldBundle/blob/master/Form/Type/ConfigAuthType.php | |
class ConfigAuthType extends AbstractType | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options): void | |
{ | |
$builder->add( | |
'key', | |
TextType::class, | |
[ | |
'label' => 'hello_world.form.key', | |
'required' => true, | |
'attr' => [ | |
'class' => 'form-control', | |
], | |
'constraints' => [ | |
new NotBlank(['message' => 'hello_world.form.key.required']), | |
], | |
] | |
); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConfigSupport extends HelloWorldIntegration implements ConfigFormInterface, ConfigFormAuthInterface | |
{ | |
use DefaultConfigFormTrait; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getAuthConfigFormName(): string | |
{ | |
return ConfigAuthType::class; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Full code: https://github.com/mohit-rocks/HelloWorldBundle/blob/master/Connection/Credentials.php | |
class Credentials implements CredentialsInterface | |
{ | |
private $userName; | |
private $userPassword; | |
public function __construct(string $userName, string $userPassword) | |
{ | |
$this->userName = $userName; | |
$this->userPassword = $userPassword; | |
} | |
public function getUsername(): ?string | |
{ | |
return $this->userName; | |
} | |
public function getPassword(): ?string | |
{ | |
return $this->userPassword; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @var $factory HttpFactory */ | |
$client = $factory->getClient($credentials); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment