Skip to content
Snippets Groups Projects
Select Git revision
  • eea9fe8eca4c5c2b69fbec8805f3cd2306ff8471
  • master default protected
2 results

registration.php

Blame
  • ApplyMediaFiles.php 2.77 KiB
    <?php
    declare(strict_types=1);
    
    namespace Firegento\ContentProvisioning\Model\Command;
    
    use Firegento\ContentProvisioning\Api\Data\EntryInterface;
    use Firegento\ContentProvisioning\Api\TargetMediaDirectoryPathProviderInterface;
    use Magento\Framework\Exception\FileSystemException;
    use Magento\Framework\Filesystem\DriverInterface;
    
    class ApplyMediaFiles
    {
        /**
         * @var TargetMediaDirectoryPathProviderInterface
         */
        private $targetMediaDirectoryPathProvider;
    
        /**
         * @var DriverInterface
         */
        private $fileSystemDriver;
    
        /**
         * @param TargetMediaDirectoryPathProviderInterface $targetMediaDirectoryPathProvider
         * @param DriverInterface $fileSystemDriver
         */
        public function __construct(
            TargetMediaDirectoryPathProviderInterface $targetMediaDirectoryPathProvider,
            DriverInterface $fileSystemDriver
        ) {
            $this->targetMediaDirectoryPathProvider = $targetMediaDirectoryPathProvider;
            $this->fileSystemDriver = $fileSystemDriver;
        }
    
        /**
         * @param EntryInterface $entry
         * @throws FileSystemException
         */
        public function execute(EntryInterface $entry): void
        {
            if ($entry->getMediaDirectory() === null) {
                return;
            }
    
            $sourceDirPath = $entry->getMediaDirectory();
            foreach ($entry->getMediaFiles() as $fileName) {
                $this->copyFile($sourceDirPath, $fileName);
            }
        }
    
        /**
         * @param string $sourceDirPath
         * @param string $fileName
         * @throws FileSystemException
         */
        private function copyFile(string $sourceDirPath, string $fileName): void
        {
            $targetDirPath = $this->targetMediaDirectoryPathProvider->get();
            $sourcePathname = $sourceDirPath . DIRECTORY_SEPARATOR . $fileName;
            $targetPathname = $targetDirPath . DIRECTORY_SEPARATOR . $fileName;
    
            if ($this->fileSystemDriver->isFile($sourcePathname)
                && $this->fileSystemDriver->isReadable($sourcePathname)
            ) {
                $this->createDirectory($targetDirPath, $sourceDirPath, $sourcePathname);
                $this->fileSystemDriver->copy($sourcePathname, $targetPathname);
            }
        }
    
        /**