選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

62 行
2.0KB

  1. <?php
  2. namespace common\helpers;
  3. use common\logic\Producer\Producer\Model\Producer;
  4. use GuzzleHttp\Client;
  5. use yii\base\ErrorException;
  6. class Alwaysdata
  7. {
  8. public static function createProducerEmailRedirection(Producer $producer)
  9. {
  10. return self::post('mailbox',[
  11. 'domain' => \Yii::$app->parameterBag->get('alwaysdataDomainOpendistribId'),
  12. 'name' => $producer->slug,
  13. 'password' => Password::generate(13),
  14. 'redirect_enabled' => true,
  15. 'redirect_to' => $producer->contact_email
  16. ]);
  17. }
  18. public static function createProducerSiteShortUrlRedirection(Producer $producer)
  19. {
  20. return self::post('site', [
  21. 'addresses' => [$producer->slug.'.opendistrib.net'],
  22. 'type' => 'redirect',
  23. 'url' => 'https://producteurs.opendistrib.net/'.$producer->slug,
  24. 'redirect_type' => 'PERMANENT',
  25. 'annotation' => 'Opendistrib / '.$producer->name
  26. ]);
  27. }
  28. private static function post(string $resource, array $data)
  29. {
  30. $client = self::getClient($resource);
  31. return $client->request('POST', '', [
  32. 'body' => json_encode($data)
  33. ]);
  34. }
  35. private static function getClient(string $resource)
  36. {
  37. self::checkConfiguration();
  38. return new Client([
  39. 'base_uri' => \Yii::$app->parameterBag->get('alwaysdataApiUrl').$resource.'/',
  40. 'auth' => [\Yii::$app->parameterBag->get('alwaysdataApiKey').' account='.\Yii::$app->parameterBag->get('alwaysdataAccount'), ''],
  41. ]);
  42. }
  43. private static function checkConfiguration()
  44. {
  45. if(!\Yii::$app->parameterBag->has('alwaysdataApiUrl')
  46. || !\Yii::$app->parameterBag->has('alwaysdataAccount')
  47. || !\Yii::$app->parameterBag->has('alwaysdataApiKey')
  48. || !\Yii::$app->parameterBag->has('alwaysdataDomainOpendistribId')) {
  49. throw new ErrorException('Configuration API Alwaysdata absente ou incomplète dans params-local.php');
  50. }
  51. }
  52. }