|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SendinBlue\Bundle\ApiBundle\DependencyInjection; |
| 4 | + |
| 5 | +use Symfony\Component\Config\FileLocator; |
| 6 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 7 | +use Symfony\Component\DependencyInjection\Definition; |
| 8 | +use Symfony\Component\DependencyInjection\DefinitionDecorator; |
| 9 | +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 10 | +use Symfony\Component\DependencyInjection\Reference; |
| 11 | +use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
| 12 | + |
| 13 | +class SendinBlueApiExtension extends Extension |
| 14 | +{ |
| 15 | + private static $MAPPING = [ |
| 16 | + 'account' => 'SendinBlue\Client\Api\AccountApi', |
| 17 | + 'attributes' => 'SendinBlue\Client\Api\AttributesApi', |
| 18 | + 'contacts' => 'SendinBlue\Client\Api\ContactsApi', |
| 19 | + 'email_campaigns' => 'SendinBlue\Client\Api\EmailCampaignsApi', |
| 20 | + 'folders' => 'SendinBlue\Client\Api\FoldersApi', |
| 21 | + 'lists' => 'SendinBlue\Client\Api\ListsApi', |
| 22 | + 'process' => 'SendinBlue\Client\Api\ProcessApi', |
| 23 | + 'reseller' => 'SendinBlue\Client\Api\ResellerApi', |
| 24 | + 'senders' => 'SendinBlue\Client\Api\SendersApi', |
| 25 | + 'sms_campaigns' => 'SendinBlue\Client\Api\SMSCampaignsApi', |
| 26 | + 'smtp' => 'SendinBlue\Client\Api\SMTPApi', |
| 27 | + 'transactional_sms' => 'SendinBlue\Client\Api\TransactionalSMSApi', |
| 28 | + 'webhooks' => 'SendinBlue\Client\Api\WebhooksApi', |
| 29 | + ]; |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function load(array $config, ContainerBuilder $container) |
| 35 | + { |
| 36 | + $loader = new XmlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources/config')); |
| 37 | + $loader->load('services.xml'); |
| 38 | + |
| 39 | + $config = $this->processConfiguration(new Configuration(), $config); |
| 40 | + |
| 41 | + if (empty($config['default_client'])) { |
| 42 | + $keys = array_keys($config['clients']); |
| 43 | + $config['default_client'] = reset($keys); |
| 44 | + } |
| 45 | + |
| 46 | + foreach ($config['clients'] as $name => $client) { |
| 47 | + $this->loadClient($name, $config['default_client'] === $name, $client, $container); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + */ |
| 54 | + public function getNamespace() |
| 55 | + { |
| 56 | + return 'http://sendinblue.com/schema/dic/api'; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function getXsdValidationBasePath() |
| 63 | + { |
| 64 | + return dirname(__DIR__).'/Resources/config/schema'; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function getAlias() |
| 71 | + { |
| 72 | + return 'sendinblue_api'; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array |
| 77 | + */ |
| 78 | + public static function getEndpoints() |
| 79 | + { |
| 80 | + return array_keys(self::$MAPPING); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param string $name |
| 85 | + * @param bool $default |
| 86 | + * @param array $client |
| 87 | + * @param ContainerBuilder $container |
| 88 | + */ |
| 89 | + private function loadClient($name, $default, array $client, ContainerBuilder $container) |
| 90 | + { |
| 91 | + $configurationService = sprintf('sendinblue_api.%s_client.configuration', $name); |
| 92 | + $configuration = $container->setDefinition( |
| 93 | + $configurationService, |
| 94 | + new DefinitionDecorator('sendinblue_api.client.configuration') |
| 95 | + ); |
| 96 | + |
| 97 | + $configuration->addMethodCall('setApiKey', ['api-key', $client['key']]); |
| 98 | + |
| 99 | + $clientService = sprintf('sendinblue_api.%s_client', $name); |
| 100 | + $container |
| 101 | + ->setDefinition($clientService, new DefinitionDecorator('sendinblue_api.client')) |
| 102 | + ->setArguments([new Reference($configurationService)]) |
| 103 | + ; |
| 104 | + |
| 105 | + foreach ($client['endpoints'] as $endpoint) { |
| 106 | + $endpointService = sprintf('sendinblue_api.%s_client.%s_endpoint', $name, $endpoint); |
| 107 | + |
| 108 | + $container->setDefinition($endpointService, new Definition( |
| 109 | + self::$MAPPING[$endpoint], |
| 110 | + [new Reference($clientService)] |
| 111 | + )); |
| 112 | + |
| 113 | + if ($default) { |
| 114 | + $container->setAlias( |
| 115 | + sprintf('sendinblue_api.%s_endpoint', $endpoint), |
| 116 | + $endpointService |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments