Version 1
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate_plus / data_fetcher / Http.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate_plus\data_fetcher;
4
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate_plus\DataFetcherPluginBase;
8 use GuzzleHttp\Exception\RequestException;
9
10 /**
11  * Retrieve data over an HTTP connection for migration.
12  *
13  * @DataFetcher(
14  *   id = "http",
15  *   title = @Translation("HTTP")
16  * )
17  */
18 class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterface {
19
20   /**
21    * The HTTP Client
22    *
23    * @var \GuzzleHttp\Client
24    */
25   protected $httpClient;
26
27   /**
28    * The request headers.
29    *
30    * @var array
31    */
32   protected $headers = [];
33
34   /**
35    * The data retrieval client.
36    *
37    * @var \Drupal\migrate_plus\AuthenticationPluginInterface
38    */
39   protected $authenticationPlugin;
40
41   /**
42    * {@inheritdoc}
43    */
44   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
45     parent::__construct($configuration, $plugin_id, $plugin_definition);
46     $this->httpClient = \Drupal::httpClient();
47   }
48
49   /**
50    * Returns the initialized authentication plugin.
51    *
52    * @return \Drupal\migrate_plus\AuthenticationPluginInterface
53    *   The authentication plugin.
54    */
55   public function getAuthenticationPlugin() {
56     if (!isset($this->authenticationPlugin)) {
57       $this->authenticationPlugin = \Drupal::service('plugin.manager.migrate_plus.authentication')->createInstance($this->configuration['authentication']['plugin'], $this->configuration['authentication']);
58     }
59     return $this->authenticationPlugin;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function setRequestHeaders(array $headers) {
66     $this->headers = $headers;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getRequestHeaders() {
73     return !empty($this->headers) ? $this->headers : [];
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getResponse($url) {
80     try {
81       $options = ['headers' => $this->getRequestHeaders()];
82       if (!empty($this->configuration['authentication'])) {
83         $options = array_merge($options, $this->getAuthenticationPlugin()->getAuthenticationOptions());
84       }
85       $response = $this->httpClient->get($url, $options);
86       if (empty($response)) {
87         throw new MigrateException('No response at ' . $url . '.');
88       }
89     }
90     catch (RequestException $e) {
91       throw new MigrateException('Error message: ' . $e->getMessage() . ' at ' . $url .'.');
92     }
93     return $response;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function getResponseContent($url) {
100     $response = $this->getResponse($url);
101     return $response->getBody();
102   }
103
104 }