<?php

namespace Webkul\DamBundle\Service;

use Pim\Component\Catalog\FileStorage;
use Akeneo\Component\FileStorage\File\FileStorerInterface;
use Webkul\DamBundle\Listener\ClassDefinationForCompatibility;

$versionCompatiblility = new ClassDefinationForCompatibility();
$versionCompatiblility->createClassAliases();

class ImageStorer
{
    /**
     * @var FileStorerInterface $storer
     */
    protected $storer;

    /**
     * @var uploadDir
     */
    protected $uploadDir;

    /**
     * @param FileStorerInterface $storer
     *
     * @param string uploadDir
     */
    public function __construct(FileStorerInterface $storer, $uploadDir)
    {
        $this->storer    = $storer;
        $this->uploadDir = $uploadDir;
    }

    /**
     * FUNCTION STORE THE IMAGE AND RETURN THE FILE KEY
     *
     */
    public function store($filePath)
    {
        $filePath = $this->getImagePath($filePath);
        
        $rawFile = new \SplFileInfo($filePath);
        
        $file = $this->storer->store($rawFile, 'assetStorage');
        
        return $file->getKey();
    }
}
