Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Commit 5325f9b

Browse files
MatTheCatMathieu Lechat
authored and
Mathieu Lechat
committed
Initial commit
0 parents  commit 5325f9b

17 files changed

+667
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
.php_cs.cache
3+
composer.lock
4+
vendor

.php_cs.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return \PhpCsFixer\Config::create()
4+
->setFinder(
5+
\Symfony\Component\Finder\Finder::create()
6+
->files()
7+
->name('*.php')
8+
->in('.')
9+
->exclude('vendor')
10+
)
11+
->setRules([
12+
'@Symfony' => true,
13+
'array_syntax' => ['syntax' => 'short'],
14+
]);

.travis.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
3+
php:
4+
- 7
5+
6+
env:
7+
- SYMFONY_VERSION=2.*
8+
- SYMFONY_VERSION=3.*
9+
10+
cache:
11+
directories:
12+
- $HOME/.composer/cache
13+
14+
install:
15+
- composer require --dev --no-update symfony/framework-bundle:${SYMFONY_VERSION}
16+
- composer install
17+
18+
script:
19+
- vendor/bin/php-cs-fixer fix --dry-run --diff
20+
- phpunit --coverage-text
21+
22+
sudo: false

DependencyInjection/Configuration.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace SendinBlue\Bundle\ApiBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7+
use Symfony\Component\Config\Definition\ConfigurationInterface;
8+
9+
class Configuration implements ConfigurationInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function getConfigTreeBuilder()
15+
{
16+
$treeBuilder = new TreeBuilder();
17+
$rootNode = $treeBuilder->root('sendinblue_api');
18+
19+
$rootNode
20+
->beforeNormalization()
21+
->ifTrue(function ($configuration) {
22+
return is_array($configuration)
23+
&& !array_key_exists('clients', $configuration)
24+
&& !array_key_exists('client', $configuration)
25+
;
26+
})
27+
->then(function ($configuration) {
28+
$defaultClient = 'default';
29+
if (isset($configuration['default_client'])) {
30+
$defaultClient = $configuration['default_client'];
31+
unset($configuration['default_client']);
32+
}
33+
34+
$clientConfiguration = [];
35+
foreach (['key', 'endpoints'] as $clientKey) {
36+
if (array_key_exists($clientKey, $configuration)) {
37+
$clientConfiguration[$clientKey] = $configuration[$clientKey];
38+
unset($configuration[$clientKey]);
39+
}
40+
}
41+
42+
$configuration['clients'] = [$defaultClient => $clientConfiguration];
43+
44+
return $configuration;
45+
})
46+
->end()
47+
->children()
48+
->scalarNode('default_client')->end()
49+
->end()
50+
->fixXmlConfig('client')
51+
->append($this->getClientsNode())
52+
;
53+
54+
return $treeBuilder;
55+
}
56+
57+
/**
58+
* @return ArrayNodeDefinition
59+
*/
60+
private function getClientsNode()
61+
{
62+
$treeBuilder = new TreeBuilder();
63+
$node = $treeBuilder->root('clients');
64+
65+
$node
66+
->requiresAtLeastOneElement()
67+
->useAttributeAsKey('name')
68+
->prototype('array')
69+
->fixXmlConfig('endpoint')
70+
->children()
71+
->arrayNode('endpoints')
72+
->prototype('enum')
73+
->values(SendinBlueApiExtension::getEndpoints())
74+
->end()
75+
->end()
76+
->scalarNode('key')->isRequired()->cannotBeEmpty()->end()
77+
->end()
78+
->end()
79+
;
80+
81+
return $node;
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 SendinBlue
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# sendinblue/api-bundle
2+
3+
This bundle integrates [`sendinblue/api-v3-sdk`](https://github.com/sendinblue/APIv3-php-library) into Symfony 2 and 3.
4+
5+
## Installation
6+
7+
### Step 1: Download the Bundle
8+
9+
Open a command console, enter your project directory and execute the
10+
following command to download the latest stable version of this bundle:
11+
12+
```console
13+
$ composer require sendinblue/api-bundle "~1"
14+
```
15+
16+
This command requires you to have Composer installed globally, as explained
17+
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
18+
of the Composer documentation.
19+
20+
### Step 2: Enable the Bundle
21+
22+
Then, enable the bundle by adding it to the list of registered bundles
23+
in the `app/AppKernel.php` file of your project:
24+
25+
```php
26+
<?php
27+
// app/AppKernel.php
28+
29+
// ...
30+
class AppKernel extends Kernel
31+
{
32+
public function registerBundles()
33+
{
34+
$bundles = array(
35+
// ...
36+
37+
new SendinBlue\Bundle\ApiBundle\SendinBlueApiBundle(),
38+
);
39+
40+
// ...
41+
}
42+
43+
// ...
44+
}
45+
```
46+
47+
## Configuration
48+
49+
```yaml
50+
sendinblue_api:
51+
endpoints: []
52+
key: ~
53+
```
54+
55+
You’ll define the API client key under `sendinblue_api.key`.
56+
57+
Then you’ll need to otp-in for the endpoints you want to access. One service will be created per endpoint:
58+
59+
- account
60+
- attributes
61+
- contacts
62+
- email_campaigns
63+
- folders
64+
- lists
65+
- process
66+
- reseller
67+
- senders
68+
- sms_campaigns
69+
- smtp
70+
- transactional_sms
71+
- webhooks
72+
73+
The service names will be `sendinblue_api.%s_endpoint` where `%s` is a value from the list above. The corresponding classes can be found under the `SendinBlue\Client\Api` namespace.
74+
75+
```php
76+
/** @var SendinBlue\Client\Api\AccountApi $accountEndpoint */
77+
$accountEndpoint = $this->get('sendinblue_api.account_endpoint');
78+
```
79+
80+
### Multiple clients
81+
82+
To use more than one client you must move `key` and `endpoints` parameter under a `clients` associative array. Each client will be named by its key.
83+
84+
```yaml
85+
sendinblue_api:
86+
clients:
87+
first:
88+
endpoints:
89+
- account
90+
key: ~
91+
second:
92+
endpoints:
93+
- account
94+
key: ~
95+
```
96+
97+
To access the first client account endpoint you’ll get the `sendinblue_api.first_client.account_endpoint` service.
98+
99+
#### Default client
100+
101+
You can define a default client through the `default_client` parameter; by default it is the first defined client. You can access the default client endpoints without adding its name in the service name. In the above example the `sendinblue_api.account_endpoint` service would be an alias of `sendinblue_api.first_client.account_endpoint`.
102+
103+
If you write
104+
105+
```yaml
106+
sendinblue_api:
107+
default_client: second
108+
clients:
109+
first:
110+
endpoints:
111+
- account
112+
key: ~
113+
second:
114+
endpoints:
115+
- account
116+
key: ~
117+
```
118+
119+
then `sendinblue_api.account_endpoint` will be an alias of `sendinblue_api.second_client.account_endpoint`.

0 commit comments

Comments
 (0)