1
\$\begingroup\$

I would like to know how I can improve this CakePHP 3.0 Component (inside folder controller)

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

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

and

use ImageTool;

2nd: in method saveFileLFS I'm using true or false to flag OK.

<?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;
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review. Good job on your first question. \$\endgroup\$
    – SirPython
    Dec 1, 2015 at 22:06
  • \$\begingroup\$ @SirPython thank you, I'm originally user from pt.stackoverflow \$\endgroup\$
    – Ricardo
    Dec 1, 2015 at 22:14
  • \$\begingroup\$ You may find this a useful read Uploading files and images with CakePHP 3. \$\endgroup\$
    – AD7six
    Dec 6, 2015 at 18:48

1 Answer 1

0
\$\begingroup\$

If you have another point to improve, please comment or answer, I will change the accepted answer

To change the require I use the composer.json classmap like this (follow this answer: Import Class without autoload and repository):

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

I don't use require now.

In saveFileLFS method now I'm using Exception if something don'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
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.