You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 line
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->params['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->params['alwaysdataApiUrl'].$resource.'/',
  40. 'auth' => [\Yii::$app->params['alwaysdataApiKey'].' account='.\Yii::$app->params['alwaysdataAccount'], ''],
  41. ]);
  42. }
  43. private static function checkConfiguration()
  44. {
  45. if(!isset(\Yii::$app->params['alwaysdataApiUrl'])
  46. || !isset(\Yii::$app->params['alwaysdataAccount'])
  47. || !isset(\Yii::$app->params['alwaysdataApiKey'])
  48. || !isset(\Yii::$app->params['alwaysdataDomainOpendistribId'])) {
  49. throw new ErrorException('Configuration API Alwaysdata absente ou incomplète dans params-local.php');
  50. }
  51. }
  52. }