Leave component following the CakePHP standard

2

How can I improve this CakePHP 3.0 Component (inside the controller folder)

  

Question discussed in: Post functional code in stackoverflow for refactoring?

1st: to use external libs (stored in the vendor folder) I'm using the require keyword and include the class using palavra-chave use , like this :

require_once(ROOT . DS . 'vendor' . DS . 'CakePHP-ImageTool-Component' . DS . 'ImageTool.php');

and

use ImageTool;

2nd: in method saveFileLFS I am using true and false to check if the operation succeeded.

<?php
namespace App\Controller\Component;

require_once(ROOT . DS . 'vendor' . DS . 'CakePHP-ImageTool-Component' . DS . 'ImageTool.php');

use Burzum\FileStorage\Lib\StorageManager;
use Cake\Controller\Component;
use ImageTool;

class UploadFileComponent extends Component
{
    function resizeImage($settings)
    {
        $status = ImageTool::resize([
            'input' => $settings['input'],
            'output' => $settings['output'],
            'width' => $settings['width'],
            'height' => $settings['height'],
            'mode' => $settings['mode']
        ]);
        return $status;
    }

    public function saveFileLFS($stringSeparator, $storeName, $productName)
    {
        $key = $storeName . $stringSeparator . $productName . $stringSeparator .
            $this->request->data['Media']['file']['name'];
        if(StorageManager::adapter('Local')->write($key,
            file_get_contents($this->request->data['Media']['file']['tmp_name']))){
            return true;
        }else
       {
            return false;
        }
    }
}
    
asked by anonymous 02.12.2015 / 14:14

1 answer

0
  

If you have another point where you can improve, comment or answer this question

To change require I used composer.json classmap this way (see this answer: Import Class without autoload and repository ):

"autoload": {
    "classmap": [
        "./vendor/CakePHP-ImageTool-Component"
    ]
}

You do not need require now.

In saveFileLFS method I am now using Exceptions if something does not work.

public function saveFileLFS($stringSeparator, $storeName, $productName)
{
    $key = $storeName . $stringSeparator . $productName . $stringSeparator .
        $this->request->data['Media']['file']['name'];
    if(!StorageManager::adapter('Local')->write($key,
        file_get_contents($this->request->data['Media']['file']['tmp_name']))){
        throw new MissingHelperException();
        //Or other Exception
    }
}
    
10.12.2015 / 13:32