Leave Component Following Cakephp’s Pattern

Asked

Viewed 72 times

2

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

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

first: to use external libs (stored in folder vendor) I’m using the keyword require and include the class usingpalavra-chave use, like this:

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

and

use ImageTool;

2nd: in the method saveFileLFS I am using true and false to check whether the operation has been successful.

<?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;
        }
    }
}
  • Puts! Burzum, that black metal band the guy killed the other?

1 answer

0


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

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

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

There is no need to require now.

In saveFileLFS method I am now using Exceptions if something doesn’t 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
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.