Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / file_mdm / src / FileMetadata.php
1 <?php
2
3 namespace Drupal\file_mdm;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\File\FileSystemInterface;
7 use Drupal\file_mdm\Plugin\FileMetadataPluginManager;
8 use Psr\Log\LoggerInterface;
9
10 /**
11  * A file metadata object.
12  */
13 class FileMetadata implements FileMetadataInterface {
14
15   /**
16    * The FileMetadata plugin manager.
17    *
18    * @var \Drupal\file_mdm\Plugin\FileMetadataPluginManager
19    */
20   protected $pluginManager;
21
22   /**
23    * The file_mdm logger.
24    *
25    * @var \Psr\Log\LoggerInterface
26    */
27   protected $logger;
28
29   /**
30    * The file system service.
31    *
32    * @var \Drupal\Core\File\FileSystemInterface
33    */
34   protected $fileSystem;
35
36   /**
37    * The URI of the file.
38    *
39    * @var string
40    */
41   protected $uri = '';
42
43   /**
44    * The hash used to reference the URI.
45    *
46    * @var string
47    */
48   protected $hash;
49
50   /**
51    * The local filesystem path to the file.
52    *
53    * This is used to allow accessing local copies of files stored remotely, to
54    * minimise remote calls and allow functions that cannot access remote stream
55    * wrappers to operate locally.
56    *
57    * @var string
58    */
59   protected $localTempPath;
60
61   /**
62    * The array of FileMetadata plugins for this URI.
63    *
64    * @var \Drupal\file_mdm\Plugin\FileMetadataPluginInterface[]
65    */
66   protected $plugins = [];
67
68   /**
69    * Constructs a FileMetadata object.
70    *
71    * @param \Drupal\file_mdm\Plugin\FileMetadataPluginManager $plugin_manager
72    *   The file metadata plugin manager.
73    * @param \Psr\Log\LoggerInterface $logger
74    *   The logger service.
75    * @param \Drupal\Core\File\FileSystemInterface $file_system
76    *   The file system service.
77    * @param string $uri
78    *   The URI of the file.
79    * @param string $hash
80    *   The hash used to reference the URI by file_mdm.
81    */
82   public function __construct(FileMetadataPluginManager $plugin_manager, LoggerInterface $logger, FileSystemInterface $file_system, $uri, $hash) {
83     $this->pluginManager = $plugin_manager;
84     $this->logger = $logger;
85     $this->fileSystem = $file_system;
86     $this->uri = $uri;
87     $this->hash = $hash;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function getUri() {
94     return $this->uri;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function getLocalTempPath() {
101     return $this->localTempPath;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function setLocalTempPath($temp_uri) {
108     $this->localTempPath = $temp_uri;
109     foreach ($this->plugins as $plugin) {
110       $plugin->setLocalTempPath($this->localTempPath);
111     }
112     return $this;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function copyUriToTemp($temp_uri = NULL) {
119     if ($temp_uri === NULL) {
120       $temp_uri = $this->fileSystem->tempnam('temporary://', 'file_mdm_');
121       $this->fileSystem->unlink($temp_uri);
122       $temp_uri .= '.' . pathinfo($this->getUri(), PATHINFO_EXTENSION);
123     }
124     if ($temp_path = file_unmanaged_copy($this->getUri(), $this->fileSystem->realpath($temp_uri), FILE_EXISTS_REPLACE)) {
125       $this->setLocalTempPath($temp_path);
126     }
127     return (bool) $temp_path;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function copyTempToUri() {
134     if (($temp_path = $this->getLocalTempPath()) === NULL) {
135       return FALSE;
136     }
137     return (bool) file_unmanaged_copy($temp_path, $this->getUri(), FILE_EXISTS_REPLACE);
138   }
139
140   /**
141    * {@inheritdoc}
142    */
143   public function getFileMetadataPlugin($metadata_id) {
144     if (!isset($this->plugins[$metadata_id])) {
145       try {
146         $this->plugins[$metadata_id] = $this->pluginManager->createInstance($metadata_id);
147         $this->plugins[$metadata_id]->setUri($this->uri);
148         $this->plugins[$metadata_id]->setLocalTempPath($this->localTempPath ?: $this->uri);
149         $this->plugins[$metadata_id]->setHash($this->hash);
150       }
151       catch (PluginNotFoundException $e) {
152         return NULL;
153       }
154     }
155     return $this->plugins[$metadata_id];
156   }
157
158   /**
159    * {@inheritdoc}
160    */
161   public function getSupportedKeys($metadata_id, $options = NULL) {
162     try {
163       if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
164         $keys = $plugin->getSupportedKeys($options);
165       }
166       else {
167         $keys = NULL;
168       }
169     }
170     catch (\Exception $e) {
171       $this->logger->error($e->getMessage());
172       $keys = NULL;
173     }
174     return $keys;
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function getMetadata($metadata_id, $key = NULL) {
181     try {
182       if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
183         $metadata = $plugin->getMetadata($key);
184       }
185       else {
186         $metadata = NULL;
187       }
188     }
189     catch (\Exception $e) {
190       $this->logger->error($e->getMessage());
191       $metadata = NULL;
192     }
193     return $metadata;
194   }
195
196   /**
197    * {@inheritdoc}
198    */
199   public function removeMetadata($metadata_id, $key) {
200     try {
201       if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
202         return $plugin->removeMetadata($key);
203       }
204     }
205     catch (\Exception $e) {
206       $this->logger->error($e->getMessage());
207     }
208     return FALSE;
209   }
210
211   /**
212    * {@inheritdoc}
213    */
214   public function setMetadata($metadata_id, $key, $value) {
215     try {
216       if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
217         return $plugin->setMetadata($key, $value);
218       }
219     }
220     catch (\Exception $e) {
221       $this->logger->error($e->getMessage());
222     }
223     return FALSE;
224   }
225
226   /**
227    * {@inheritdoc}
228    */
229   public function isMetadataLoaded($metadata_id) {
230     if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
231       return $plugin->isMetadataLoaded();
232     }
233     return FALSE;
234   }
235
236   /**
237    * {@inheritdoc}
238    */
239   public function loadMetadata($metadata_id, $metadata) {
240     if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
241       return $plugin->loadMetadata($metadata);
242     }
243     return FALSE;
244   }
245
246   /**
247    * {@inheritdoc}
248    */
249   public function loadMetadataFromCache($metadata_id) {
250     if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
251       return $plugin->loadMetadataFromCache();
252     }
253     return FALSE;
254   }
255
256   /**
257    * {@inheritdoc}
258    */
259   public function saveMetadataToCache($metadata_id, array $tags = []) {
260     if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
261       return $plugin->saveMetadataToCache($tags);
262     }
263     return FALSE;
264   }
265
266   /**
267    * {@inheritdoc}
268    */
269   public function saveMetadataToFile($metadata_id) {
270     if ($plugin = $this->getFileMetadataPlugin($metadata_id)) {
271       return $plugin->saveMetadataToFile();
272     }
273     return FALSE;
274   }
275
276 }