Skip to content

Instantly share code, notes, and snippets.

@mohit-rocks
Last active October 9, 2020 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohit-rocks/afad063a1dae9d0de2e001577a1d64be to your computer and use it in GitHub Desktop.
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
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;
}
}
// 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']);
}
// 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',
],
],
],
],
// 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',
],
],
// 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']),
],
]
);
}
}
class ConfigSupport extends HelloWorldIntegration implements ConfigFormInterface, ConfigFormAuthInterface
{
use DefaultConfigFormTrait;
/**
* {@inheritdoc}
*/
public function getAuthConfigFormName(): string
{
return ConfigAuthType::class;
}
}
// 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;
}
}
/** @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