|
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->params['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->params['alwaysdataApiUrl'].$resource.'/',
- 'auth' => [\Yii::$app->params['alwaysdataApiKey'].' account='.\Yii::$app->params['alwaysdataAccount'], ''],
- ]);
- }
-
- private static function checkConfiguration()
- {
- if(!isset(\Yii::$app->params['alwaysdataApiUrl'])
- || !isset(\Yii::$app->params['alwaysdataAccount'])
- || !isset(\Yii::$app->params['alwaysdataApiKey'])
- || !isset(\Yii::$app->params['alwaysdataDomainOpendistribId'])) {
-
- throw new ErrorException('Configuration API Alwaysdata absente ou incomplète dans params-local.php');
- }
- }
- }
|