/**
  * @param ResourceDOInterface $resourceDO
  * @param string $uri
  * @return DownloadedFile
  * @throws ErrorException
  * @throws \Exception
  */
 protected function download(ResourceDOInterface $resourceDO, $uri)
 {
     // ------------
     // @todo refactoring: move downloading code from here to separate service!
     // ------------
     set_time_limit(self::CURL_TIMEOUT);
     $dir = DATA_DIR . 'download' . DIRECTORY_SEPARATOR;
     $file = $this->resourceDO->getUuid() . '_' . time() . '_' . mt_rand(100, 200) . '.tmp';
     if (!@mkdir($dir) && !is_dir($dir)) {
         throw new ErrorException('Can\'t create the directory: ' . $dir);
     }
     if (is_file($file)) {
         if (!unlink($file)) {
             throw new ErrorException('Can\'t remove old file: ' . $dir . $file);
         }
     }
     $resource = fopen($dir . $file, 'w+');
     if (!$resource) {
         throw new ErrorException('Can\'t create the file for writting: ' . $dir . $file);
     }
     $uriEnc = str_replace(' ', '%20', $uri);
     $headers = ["Accept: " . $resourceDO->getMimeType(), "Cache-Control: no-cache", "Pragma: no-cache"];
     $curlHandle = curl_init($uriEnc);
     if (!$curlHandle) {
         throw new ErrorException('Curl error for uri: ' . $uri . ': cannot create resource');
     }
     curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curlHandle, CURLOPT_TIMEOUT, static::CURL_TIMEOUT);
     // Save curl result to the file
     curl_setopt($curlHandle, CURLOPT_FILE, $resource);
     curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($curlHandle, CURLOPT_MAXREDIRS, 2);
     curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
     // get curl response
     curl_exec($curlHandle);
     if (curl_errno($curlHandle)) {
         $error = curl_error($curlHandle);
         curl_close($curlHandle);
         fclose($resource);
         throw new ErrorException('Curl error for uri: ' . $uri . '; ' . $error);
     }
     $size = (int) curl_getinfo($curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
     curl_close($curlHandle);
     fclose($resource);
     // ------------
     $downloaded = new DownloadedFile($dir . $file, $size, UPLOAD_ERR_OK, $resourceDO->getName() . '.' . $resourceDO->getType(), $resourceDO->getMimeType());
     return $downloaded;
 }
 /**
  * @param bool $byPathOnly If true, no search on disk will be executed
  * @return ResourceDOInterface
  */
 public function __invoke($byPathOnly = false)
 {
     $uuid = $this->resourceDO->getUuid();
     $type = $this->resourceDO->getType();
     $variant = $this->resourceDO->getVariant();
     $version = $this->resourceDO->getVersion();
     $baseDir = $this->resourceDO->getBaseDirectory();
     $namespace = $this->resourceDO->getNamespace();
     $filePath = $this->resourceDO->getFilePath();
     if (!$uuid || !$type || !$baseDir || !$filePath) {
         throw new CommandErrorException('Cannot destroy the empty resource');
     }
     if ($byPathOnly) {
         $this->deleteFile($filePath);
     } else {
         $command = new FindResourceOptionsCommand($this->resourceDO, $this->filesystem);
         $result = $command();
         foreach ($result as $item) {
             if ($item[ResourceDOAbstract::TOKEN_TYPE] !== $type || $item['filename'] !== $uuid || $namespace && $item[ResourceDOAbstract::TOKEN_NAMESPACE] !== $namespace) {
                 continue;
             }
             if ($version !== ResourceDOInterface::DEFAULT_VERSION) {
                 if ($variant === $item[ResourceDOAbstract::TOKEN_VARIANT] && $version === (int) $item[ResourceDOAbstract::TOKEN_VERSION]) {
                     $this->deleteFile($item['path']);
                 }
             } elseif ($variant !== ResourceDOInterface::DEFAULT_VARIANT) {
                 if ($variant === $item[ResourceDOAbstract::TOKEN_VARIANT]) {
                     $this->deleteFile($item['path']);
                 }
             } else {
                 $this->deleteFile($item['path']);
             }
         }
     }
     return $this->resourceDO;
 }
 protected function findAllResourceOptions(ResourceDOInterface $resourceDO)
 {
     /** @var \League\Flysystem\FilesystemInterface $filesystem */
     $filesystem = $this->filesystem;
     $uuid = $resourceDO->getUuid();
     $type = $resourceDO->getType();
     $name = $resourceDO->getName();
     if (!$name || !$type) {
         throw new CommandErrorException('Can not look for options: resource is empty');
     }
     $basename = $resourceDO->getBaseDirectory();
     $namespace = $resourceDO->getNamespace();
     $path = $basename . ($namespace ? $namespace . DIRECTORY_SEPARATOR : '') . $type . DIRECTORY_SEPARATOR;
     $found = $filesystem->listContents($path, true);
     $found = array_filter($found, function ($file) use($uuid, $type) {
         return array_key_exists('filename', $file) && $file['filename'] === $uuid && array_key_exists('extension', $file) && $file['extension'] === $type && array_key_exists('type', $file) && $file['type'] === 'file';
     });
     array_walk($found, [$this, 'hydrateElementFile'], ['resourceDO' => $resourceDO]);
     $found = array_values($found);
     // reset keys
     return $found;
 }