summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/media/src/OEmbed/Provider.php
blob: 415bc9677a788748920dd1cb647af9b4bae8b521 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace Drupal\media\OEmbed;

use Drupal\Component\Utility\UrlHelper;

/**
 * Value object for oEmbed providers.
 */
class Provider {

  /**
   * The provider name.
   *
   * @var string
   */
  protected $name;

  /**
   * The provider URL.
   *
   * @var string
   */
  protected $url;

  /**
   * The provider endpoints.
   *
   * @var \Drupal\media\OEmbed\Endpoint[]
   */
  protected $endpoints = [];

  /**
   * Provider constructor.
   *
   * @param string $name
   *   The provider name.
   * @param string $url
   *   The provider URL.
   * @param array[] $endpoints
   *   List of endpoints this provider exposes.
   *
   * @throws \Drupal\media\OEmbed\ProviderException
   */
  public function __construct($name, $url, array $endpoints) {
    $this->name = $name;

    if (!UrlHelper::isValid($url, TRUE) || !UrlHelper::isExternal($url)) {
      throw new ProviderException('Provider @name does not define a valid external URL.', $this);
    }
    $this->url = $url;

    try {
      foreach ($endpoints as $endpoint) {
        $endpoint += ['formats' => [], 'schemes' => [], 'discovery' => FALSE];
        $this->endpoints[] = new Endpoint($endpoint['url'], $this, $endpoint['schemes'], $endpoint['formats'], $endpoint['discovery']);
      }
    }
    catch (\InvalidArgumentException) {
      // Just skip all the invalid endpoints.
      // @todo Log the exception message to help with debugging in
      // https://www.drupal.org/project/drupal/issues/2972846.
    }

    if (empty($this->endpoints)) {
      throw new ProviderException('Provider @name does not define any valid endpoints.', $this);
    }
  }

  /**
   * Returns the provider name.
   *
   * @return string
   *   Name of the provider.
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Returns the provider URL.
   *
   * @return string
   *   URL of the provider.
   */
  public function getUrl() {
    return $this->url;
  }

  /**
   * Returns the provider endpoints.
   *
   * @return \Drupal\media\OEmbed\Endpoint[]
   *   List of endpoints this provider exposes.
   */
  public function getEndpoints() {
    return $this->endpoints;
  }

}