Your IP : 216.73.217.13


Current Path : /var/www/magento.test.indacotrentino.com/www/app/code/Webkul/BuyButton/Setup/
Upload File :
Current File : /var/www/magento.test.indacotrentino.com/www/app/code/Webkul/BuyButton/Setup/InstallSchema.php

<?php
/**
 * BuyButton Schema Setup.
 * @category  Webkul
 * @package   Webkul_BuyButton
 * @author    Webkul
 * @copyright Copyright (c)  Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

namespace Webkul\BuyButton\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\App\Filesystem\DirectoryList;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * Undocumented function
     *
     * @param \Magento\Framework\Filesystem\Driver\File $driverInterface
     * @param \Magento\Framework\Module\Dir\Reader $reader
     * @param \Magento\Framework\Filesystem $fileSystem
     * @param \Magento\Framework\Filesystem\Io\File $filesystemFile
     */
    public function __construct(
        \Magento\Framework\Filesystem\Driver\File $driverInterface,
        \Magento\Framework\Module\Dir\Reader $reader,
        \Magento\Framework\Filesystem $fileSystem,
        \Magento\Framework\Filesystem\Io\File $filesystemFile
    ) {
        $this->driverInterface = $driverInterface;
        $this->reader = $reader;
        $this->fileSystem = $fileSystem;
        $this->filesystemFile = $filesystemFile;
    }
    /**
     * {@inheritdoc}
     *
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $this->moveDirToLibDir();
    }

    /**
     * move directory
     *
     * @return void
     */
    private function moveDirToLibDir()
    {
        try {
            $objManager = \Magento\Framework\App\ObjectManager::getInstance();
            /** @var \Magento\Framework\Module\Dir\Reader $reader */
            $reader = $this->reader;

            /** @var \Magento\Framework\Filesystem $filesystem */
            $filesystem = $this->fileSystem;

            $libEbayFullPath = $filesystem
                                ->getDirectoryRead(DirectoryList::MEDIA)
                                ->getAbsolutePath('buybutton');
            if (!$this->driverInterface->isExists($libEbayFullPath)) {
                $this->driverInterface->createDirectory($libEbayFullPath, 0777);
                $jscssFile = $reader->getModuleDir('', 'Webkul_BuyButton').'/buybutton/';
                $this->copyFiles($jscssFile, $libEbayFullPath);
                if (!$this->driverInterface->isExists($libEbayFullPath.'/fonts')) {
                    $this->driverInterface->createDirectory($libEbayFullPath.'/fonts', 0777);
                }
                $fontFile = $reader->getModuleDir('', 'Webkul_BuyButton').'/buybutton/fonts/';
                $this->copyFiles($fontFile, $libEbayFullPath.'/fonts');
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * copy files
     *
     * @param string $source
     * @param string $destination
     * @return void
     */
    private function copyFiles($source, $destination)
    {
        $buyButtonFiles = $this->driverInterface->readDirectoryRecursively($source, 1);
        foreach ($buyButtonFiles as $buyButtonFile) {
            if ($this->filesystemFile->fileExists($buyButtonFile)) {
                $partsOfPath = explode('/', str_replace('\\', '/', $buyButtonFile));
                $fileName = $partsOfPath[count($partsOfPath) - 1];
                $this->filesystemFile->cp($buyButtonFile, $destination.'/'.$fileName);
            }
        }
    }
}