<?php

class DModelToolFiles extends ResizeImg
{

    public function __construct($registry)
    {
        $this->registry = $registry;

        $this->setFolder('arquivo');
        $this->setDirpath($this->ftrgeralregistry->getPathRecord());
    }

    public function getThumb($filename)
    {

        if (!file_exists($this->getDirpath() . $filename) || !is_file($this->getDirpath() . $filename)) {
            return;
        }

        return Type_RecordConfig::getNickFromValue(Type_RecordConfig::Tp_FilesLogo);

    }

    public function filepath($filename)
    {

        if (!file_exists($this->getDirpath() . $filename) || !is_file($this->getDirpath() . $filename)) {
            return;
        }

        return $this->urlloja->link('', '', 'SSL', false) . $this->getFolder() . '/' . $filename;

    }

    /**
     * @throws Exception
     */
    public function createFile($name, $stringFile, $type, $sizeValidate=false)
    {

        $this->setFolder('system/cache/cachefiles');
        $this->setDirpath(DIR_CACHE.'cachefiles/');

        $fileName = $name . '.' . strtolower(Type_File::getNickFromConstValue($type));

        $fileExists = file_exists($this->getDirpath() . $fileName) || is_file($this->getDirpath() . $fileName);
        $sizeValid = $sizeValidate ? ($fileExists && (filesize($this->getDirpath() . $fileName)>0)) : true;

        if ( $fileExists && $sizeValid ) {
            return $this->getDirpath() . $fileName;
        }

        $file = fopen($this->getDirpath().$fileName, 'w');

        if ($file == false) {
            throw new Exception('Não foi possível criar o arquivo.');
        }
        fwrite($file, $stringFile);
        fclose($file);

        return $this->getDirpath() . $fileName;

    }

}
