Skip to content
Snippets Groups Projects
Select Git revision
  • d977ce7978164cdbf538f2e6a858a33b18fedc94
  • develop default protected
  • 1.3 protected
  • 1.1
  • 1.2 protected
  • 1.0
  • 13-export-cli-command
  • 1.7.0
  • 1.6.0
  • 1.5.0
  • 1.4.3
  • 1.4.2
  • 1.4.1
  • 1.4.0
  • 1.3.7
  • 1.3.6
  • 1.3.5
  • 1.3.4
  • 1.3.3
  • 1.3.2
  • 1.3.1
  • 1.3.0
  • 1.1.4
  • 1.2.5
  • 1.2.4
  • 1.2.3
  • 1.0.6
27 results

ApplyMediaFiles.php

Blame
  • ApplyMediaFiles.php 2.24 KiB
    <?php
    declare(strict_types=1);
    
    namespace Firegento\ContentProvisioning\Model\Command;
    
    use Firegento\ContentProvisioning\Api\Data\EntryInterface;
    use Firegento\ContentProvisioning\Api\TargetMediaDirectoryPathProviderInterface;
    use SplFileInfo;
    
    class ApplyMediaFiles
    {
        /**
         * @var TargetMediaDirectoryPathProviderInterface
         */
        private $targetMediaDirectoryPathProvider;
    
        /**
         * @param TargetMediaDirectoryPathProviderInterface $targetMediaDirectoryPathProvider
         */
        public function __construct(
            TargetMediaDirectoryPathProviderInterface $targetMediaDirectoryPathProvider
        ) {
            $this->targetMediaDirectoryPathProvider = $targetMediaDirectoryPathProvider;
        }
    
        /**
         * @param EntryInterface $entry
         */
        public function execute(EntryInterface $entry): void
        {
            if ($entry[EntryInterface::MEDIA_DIRECTORY] === null) {
                return;
            }
    
            $sourceDirPath = $entry[EntryInterface::MEDIA_DIRECTORY];
            foreach ($entry[EntryInterface::MEDIA_FILES] as $fileName) {
                $this->copyFile($sourceDirPath, $fileName);
            }
        }
    
        /**
         * @param string $sourceDirPath
         * @param string $fileName
         */
        private function copyFile(string $sourceDirPath, string $fileName): void
        {
            $targetDirPath = $this->targetMediaDirectoryPathProvider->get();
            $sourcePathname = $sourceDirPath . DIRECTORY_SEPARATOR . $fileName;
            $targetPathname = $targetDirPath . DIRECTORY_SEPARATOR . $fileName;
    
            $sourceInfo = new SplFileInfo($sourcePathname);
            if ($sourceInfo->isFile() && $sourceInfo->isReadable()) {
                $this->createDirectory($targetDirPath, $sourceDirPath, $sourceInfo);
                copy($sourceInfo->getPathname(), $targetPathname);
            }
        }
    
        /**
         * @param string $targetDirPath
         * @param string $sourceDirPath
         * @param SplFileInfo $sourceInfo
         */
        private function createDirectory(string $targetDirPath, string $sourceDirPath, SplFileInfo $sourceInfo): void
        {
            $subPath = str_replace($sourceDirPath, '', $sourceInfo->getPath());
            $pathname = $targetDirPath . DIRECTORY_SEPARATOR . $subPath;
            if (!is_dir($pathname)) {
                mkdir($pathname, 0775, true);
            }
        }