Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\media\OEmbed;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\Component\Utility\UrlHelper;
|
Chris@17
|
6
|
Chris@17
|
7 /**
|
Chris@17
|
8 * Value object for oEmbed providers.
|
Chris@17
|
9 */
|
Chris@17
|
10 class Provider {
|
Chris@17
|
11
|
Chris@17
|
12 /**
|
Chris@17
|
13 * The provider name.
|
Chris@17
|
14 *
|
Chris@17
|
15 * @var string
|
Chris@17
|
16 */
|
Chris@17
|
17 protected $name;
|
Chris@17
|
18
|
Chris@17
|
19 /**
|
Chris@17
|
20 * The provider URL.
|
Chris@17
|
21 *
|
Chris@17
|
22 * @var string
|
Chris@17
|
23 */
|
Chris@17
|
24 protected $url;
|
Chris@17
|
25
|
Chris@17
|
26 /**
|
Chris@17
|
27 * The provider endpoints.
|
Chris@17
|
28 *
|
Chris@17
|
29 * @var \Drupal\media\OEmbed\Endpoint[]
|
Chris@17
|
30 */
|
Chris@17
|
31 protected $endpoints = [];
|
Chris@17
|
32
|
Chris@17
|
33 /**
|
Chris@17
|
34 * Provider constructor.
|
Chris@17
|
35 *
|
Chris@17
|
36 * @param string $name
|
Chris@17
|
37 * The provider name.
|
Chris@17
|
38 * @param string $url
|
Chris@17
|
39 * The provider URL.
|
Chris@17
|
40 * @param array[] $endpoints
|
Chris@17
|
41 * List of endpoints this provider exposes.
|
Chris@17
|
42 *
|
Chris@17
|
43 * @throws \Drupal\media\OEmbed\ProviderException
|
Chris@17
|
44 */
|
Chris@17
|
45 public function __construct($name, $url, array $endpoints) {
|
Chris@17
|
46 if (!UrlHelper::isValid($url, TRUE) || !UrlHelper::isExternal($url)) {
|
Chris@17
|
47 throw new ProviderException('Provider @name does not define a valid external URL.', $this);
|
Chris@17
|
48 }
|
Chris@17
|
49
|
Chris@17
|
50 $this->name = $name;
|
Chris@17
|
51 $this->url = $url;
|
Chris@17
|
52
|
Chris@17
|
53 try {
|
Chris@17
|
54 foreach ($endpoints as $endpoint) {
|
Chris@17
|
55 $endpoint += ['formats' => [], 'schemes' => [], 'discovery' => FALSE];
|
Chris@17
|
56 $this->endpoints[] = new Endpoint($endpoint['url'], $this, $endpoint['schemes'], $endpoint['formats'], $endpoint['discovery']);
|
Chris@17
|
57 }
|
Chris@17
|
58 }
|
Chris@17
|
59 catch (\InvalidArgumentException $e) {
|
Chris@17
|
60 // Just skip all the invalid endpoints.
|
Chris@17
|
61 // @todo Log the exception message to help with debugging in
|
Chris@17
|
62 // https://www.drupal.org/project/drupal/issues/2972846.
|
Chris@17
|
63 }
|
Chris@17
|
64
|
Chris@17
|
65 if (empty($this->endpoints)) {
|
Chris@17
|
66 throw new ProviderException('Provider @name does not define any valid endpoints.', $this);
|
Chris@17
|
67 }
|
Chris@17
|
68 }
|
Chris@17
|
69
|
Chris@17
|
70 /**
|
Chris@17
|
71 * Returns the provider name.
|
Chris@17
|
72 *
|
Chris@17
|
73 * @return string
|
Chris@17
|
74 * Name of the provider.
|
Chris@17
|
75 */
|
Chris@17
|
76 public function getName() {
|
Chris@17
|
77 return $this->name;
|
Chris@17
|
78 }
|
Chris@17
|
79
|
Chris@17
|
80 /**
|
Chris@17
|
81 * Returns the provider URL.
|
Chris@17
|
82 *
|
Chris@17
|
83 * @return string
|
Chris@17
|
84 * URL of the provider.
|
Chris@17
|
85 */
|
Chris@17
|
86 public function getUrl() {
|
Chris@17
|
87 return $this->url;
|
Chris@17
|
88 }
|
Chris@17
|
89
|
Chris@17
|
90 /**
|
Chris@17
|
91 * Returns the provider endpoints.
|
Chris@17
|
92 *
|
Chris@17
|
93 * @return \Drupal\media\OEmbed\Endpoint[]
|
Chris@17
|
94 * List of endpoints this provider exposes.
|
Chris@17
|
95 */
|
Chris@17
|
96 public function getEndpoints() {
|
Chris@17
|
97 return $this->endpoints;
|
Chris@17
|
98 }
|
Chris@17
|
99
|
Chris@17
|
100 }
|