<?php

namespace App\Http\Traits;

use Illuminate\Support\Facades\File;

/**
 * Trait FileSystemTrait
 *
 * @package App\Http\Traits
 */
trait FileSystemTrait
{

    /**
     * Make directory with a check if that directory already exists
     *
     * @param string $path
     * @param int    $mode
     * @param bool   $recursive
     * @param bool   $force
     * @return bool
     */
    public static function makeDirectory($path, $mode = 0777, $recursive = true, $force = false)
    {
        if ( ! File::isDirectory($path)) {
            return File::makeDirectory($path, $mode, $recursive, $force);
        }

        return true;
    }

    /**
     * Upload file to storage
     *
     * @param string                        $storageDir
     * @param int                           $prefix
     * @param \Illuminate\Http\UploadedFile $file
     * @return \Illuminate\Http\File|\Symfony\Component\HttpFoundation\File\File
     */
    public static function uploadFileToStorage($storageDir, $prefix, $file)
    {
        return $file->move(
            storage_path($storageDir),
            $prefix . '_' . time() . '.' . $file->getClientOriginalExtension()
        );
    }
}