<commit_before><?php
namespace Model\FileStorage\Drivers;

use Dframe\FileStorage\Drivers\DatabaseDriverInterface;

/**
 * Class DatabaseDriverModel
 *
 * @package Model\FileStorage\Drivers
 */
class DatabaseDriverModel extends \Model\Model implements DatabaseDriverInterface
{
    /**
     * @param      $adapter
     * @param      $path
     * @param bool $cache
     *
     * @return mixed
     */
    public function get($adapter, $path, $cache = false)
    {
        if (!isset($path) or empty($path) or empty($adapter)) {
            return $this->methodResult(false);
        }

        $row = $this->baseClass->db->select('files', '*', ['file_path' => $path, 'file_adapter' => $adapter])->result();
        if (empty($row['file_id'])) {
            return $this->methodResult(false);
        }

        if ($cache != false) {
            if ($cache === true) {
                $row['cache'] = $this->baseClass->db->select('files_cache', '*', ['file_id' => $row['file_id']])->results();
            } else {
                $row['cache'] = $this->baseClass->db->select('files_cache', '*', ['file_cache_path' => $cache])->result();
            }
        }

        return $this->methodResult(true, $row);
    }

    /**
     * @param      $adapter
     * @param      $path
     * @param      $mime
     * @param bool $stream
     *
     * @return mixed
     */
    public function put($adapter, $path, $mime, $stream = false)
    {
        $get = $this->get($adapter, $path);
        if ($get['return'] == true) {
            return $this->methodResult(false, ['response' => 'Taki obraz już istnieje']);
        }

        $getLastInsertId = $this->baseClass->db->pdoQuery('INSERT INTO `files` (`file_adapter`, `file_path`, `file_mime`) VALUES (?,?,?)', [$adapter, $path, $mime])->getLastInsertId();
        return $this->methodResult(true, ['lastInsertId' => $getLastInsertId]);
    }

    /**
     * @param      $adapter
     * @param      $originalPath
     * @param      $cachePath
     * @param      $mime
     * @param bool $stream
     *
     * @return mixed
     */
    public function cache($adapter, $originalPath, $cachePath, $mime, $stream = false)
    {
        $row = $this->baseClass->db->select('files', '*', ['file_path' => $originalPath])->result();
        if (empty($row['file_id'])) {
            $put = $this->put($adapter, $originalPath, $mime);
            $row['file_id'] = $put['lastInsertId'];
        }

        $cache = $this->baseClass->db->select('files_cache', '*', ['file_cache_path' => $cachePath])->result();
        if (empty($cache['id']) and !empty($row['file_id'])) {
            $data = [
                'file_id' => $row['file_id'],
                'file_cache_path' => $cachePath,
                'file_cache_mime' => $mime
            ];

            // if($stream != false){
            //     $metadata = new \Libs\Plugins\FileStorage\MetadataFile($mime, $stream);
            //     $data['file_cache_metadata'] = json_encode($metadata->get());
            // }

            $getLastInsertId = $this->baseClass->db->insert('files_cache', $data);
            return $this->methodResult(true, ['lastInsertId' => $getLastInsertId]);
        }

        return $this->methodResult(false);
    }

    /**
     * @param $adapter
     * @param $path
     *<commit_msg>Resolved #11, Update Examples<commit_after><?php
namespace Model\FileStorage\Drivers;

use Dframe\FileStorage\Drivers\DatabaseDriverInterface;

/**
 * Class DatabaseDriverModel
 *
 * @package Model\FileStorage\Drivers
 */
class DatabaseDriverModel extends \Model\Model implements DatabaseDriverInterface
{
    /**
     * @param      $adapter
     * @param      $path
     * @param bool $cache
     *
     * @return mixed
     */
    public function get($adapter, $path, $cache = false)
    {
        if (!isset($path) or empty($path) or empty($adapter)) {
            return $this->methodResult(false);
        }

        $row = $this->baseClass->db->select('files', '*', ['file_path' => $path, 'file_adapter' => $adapter])->result();
        if (empty($row['file_id'])) {
            return $this->methodResult(false);
        }

        if ($cache != false) {
            if ($cache === true) {
                $row['cache'] = $this->baseClass->db->select('files_cache', '*', ['file_id' => $row['file_id']])->results();
            } else {
                $row['cache'] = $this->baseClass->db->select('files_cache', '*', ['file_cache_path' => $cache])->result();
            }
        }

        return $this->methodResult(true, $row);
    }

    /**
     * @param      $adapter
     * @param      $path
     * @param      $mime
     * @param bool $stream
     *
     * @return mixed
     */
    public function put($adapter, $path, $mime, $stream = false)
    {
        $get = $this->get($adapter, $path);
        if ($get['return'] == true) {
            return $this->methodResult(false, ['response' => 'Taki obraz już istnieje']);
        }

        $getLastInsertId = $this->baseClass->db->pdoQuery('INSERT INTO `files` (`file_adapter`, `file_path`, `file_mime`) VALUES (?,?,?)', [$adapter, $path, $mime])->getLastInsertId();
        return $this->methodResult(true, ['lastInsertId' => $getLastInsertId]);
    }

    /**
     * @param      $adapter
     * @param      $originalPath
     * @param      $cachePath
     * @param      $mime
     * @param bool $stream
     *
     * @return mixed
     */
    public function cache($adapter, $originalPath, $cachePath, $mime, $stream = false)
    {
        $row = $this->baseClass->db->select('files', '*', ['file_path' => $originalPath])->result();
        if (empty($row['file_id'])) {
            $put = $this->put($adapter, $originalPath, $mime, $stream);
            $row['file_id'] = $put['lastInsertId'];
        }

        $cache = $this->baseClass->db->select('files_cache', '*', ['file_cache_path' => $cachePath])->result();
        if (empty($cache['id']) and !empty($row['file_id'])) {
            $data = [
                'file_id' => $row['file_id'],
                'file_cache_path' => $cachePath,
                'file_cache_mime' => $mime
            ];

            // if($stream != false){
            //     $metadata = new \Libs\Plugins\FileStorage\MetadataFile($mime, $stream);
            //     $data['file_cache_metadata'] = json_encode($metadata->get());
            // }

            $getLastInsertId = $this->baseClass->db->insert('files_cache', $data);
            return $this->methodResult(true, ['lastInsertId' => $getLastInsertId]);
        }

        return $this->methodResult(false);
    }

    /**
     * @param $adapter
     * @param $path
     *
{'fraction_non_alphanumeric': 0.18583671628326567, 'fraction_numerical': 0.00030070666065253347, 'mean_word_length': 2.2464616886285995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1115788', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1993, 'n_words': 366}