|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
-
- namespace common\helpers;
-
- use common\logic\Producer\Producer\Model\Producer;
- use GuzzleHttp\Client;
- use yii\base\ErrorException;
-
- class Alwaysdata
- {
- public static function createProducerEmailRedirection(Producer $producer)
- {
- return self::post('mailbox',[
- 'domain' => \Yii::$app->parameterBag->get('alwaysdataDomainOpendistribId'),
- 'name' => $producer->slug,
- 'password' => Password::generate(13),
- 'redirect_enabled' => true,
- 'redirect_to' => $producer->contact_email
- ]);
- }
-
- public static function createProducerSiteShortUrlRedirection(Producer $producer)
- {
- return self::post('site', [
- 'addresses' => [$producer->slug.'.opendistrib.net'],
- 'type' => 'redirect',
- 'url' => 'https://producteurs.opendistrib.net/'.$producer->slug,
- 'redirect_type' => 'PERMANENT',
- 'annotation' => 'Opendistrib / '.$producer->name
- ]);
- }
-
- private static function post(string $resource, array $data)
- {
- $client = self::getClient($resource);
-
- return $client->request('POST', '', [
- 'body' => json_encode($data)
- ]);
- }
-
- private static function getClient(string $resource)
- {
- self::checkConfiguration();
-
- return new Client([
- 'base_uri' => \Yii::$app->parameterBag->get('alwaysdataApiUrl').$resource.'/',
- 'auth' => [\Yii::$app->parameterBag->get('alwaysdataApiKey').' account='.\Yii::$app->parameterBag->get('alwaysdataAccount'), ''],
- ]);
- }
-
- private static function checkConfiguration()
- {
- if(!\Yii::$app->parameterBag->has('alwaysdataApiUrl')
- || !\Yii::$app->parameterBag->has('alwaysdataAccount')
- || !\Yii::$app->parameterBag->has('alwaysdataApiKey')
- || !\Yii::$app->parameterBag->has('alwaysdataDomainOpendistribId')) {
-
- throw new ErrorException('Configuration API Alwaysdata absente ou incomplète dans params-local.php');
- }
- }
- }
|