Laravel photo uploader class I'm trying to separate logic from the controller, so I created this Upload class. It is not a big class, just a class so that the controller doesn't know about the logic and stuff like that.

&lt;?php 
namespace Dnianas\Uploads;

class Photo 
{    

    /**
     * The image filename.
     * @var string
     */
    protected $filename;

    /**
     * The image extension.
     * @var string
     */
    protected $extension;

    /**
     * The image hashed value that we hashed using SHA1.
     * @var string
     */
    protected $hashedName;

    /**
     * The filename with the hashed name and the extension.
     * @var string
     */
    public $path;

    public function makeProfilePicture($input)
    {
        $this-&gt;fileName     = $input-&gt;getClientOriginalName();
        $this-&gt;extension    = $input-&gt;getClientOriginalExtension();
        $this-&gt;hashedName   = sha1(time() . $this-&gt;fileName);
        $this-&gt;path         = $this-&gt;hashedName . '.' .$this-&gt;extension;

        // Resize the image
        $image = \Image::make($input);
        $image-&gt;fit(300, 300);

        // Set the destination
        $destination = photos_path() . '/' . $this-&gt;path;

        // Save the image
        $image-&gt;save($destination);

        // Return the object
        return $this;
    }

    public function makeCoverPhoto($input)
    {
        $this-&gt;fileName     = $input-&gt;getClientOriginalName();
        $this-&gt;extension    = $input-&gt;getClientOriginalExtension();
        $this-&gt;hashedName   = sha1(time() . $this-&gt;fileName);
        $this-&gt;path         = $this-&gt;hashedName . '.' .$this-&gt;extension;

        // Resize the image
        $image = \Image::make($input);
        $image-&gt;fit(900, 350);

        // Set the destination
        $destination = photos_path() . '/cover-' . $this-&gt;path;

        // Save the image
        $image-&gt;save($destination);

        return $this;
    }

}


As you can see, the first 4 lines from each method is exactly the same. How can I improve this?

EDIT: I've followed the constructor method. I added the constructor.

Here is the final class:

&lt;?php 
namespace Dnianas\Uploads;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class Photo 
{    

    /**
     * The image filename.
     * @var string
     */
    protected $filename;

    /**
     * The image extension.
     * @var string
     */
    protected $extension;

    /**
     * The image hashed value that we hashed using SHA1.
     * @var string
     */
    protected $hashedName;

    /**
     * The filename with the hashed name and the extension.
     * @var string
     */
    public $path;

    /**
     * The input from the user, Usually it's the file.
     * @var object
     */
    public $input;

    public function __construct(UploadedFile $input)
    {
        $this-&gt;input        = $input;
        $this-&gt;fileName     = $this-&gt;input-&gt;getClientOriginalName();
        $this-&gt;extension    = $this-&gt;input-&gt;getClientOriginalExtension();
        $this-&gt;hashedName   = sha1(time() . $this-&gt;fileName);
        $this-&gt;path         = $this-&gt;hashedName . '.' .$this-&gt;extension;
    }

    public function makeProfilePicture()
    {
        // Resize the image
        $image = \Image::make($this-&gt;input);
        $image-&gt;fit(300, 300);

        // Set the destination
        $destination = photos_path() . '/' . $this-&gt;path;

        // Save the image
        $image-&gt;save($destination);

        // Return the object
        return $this;
    }

    public function makeCoverPhoto()
    {
        // Resize the image
        $image = \Image::make($this-&gt;input);
        $image-&gt;fit(900, 350);

        // Set the destination
        $destination = photos_path() . '/cover-' . $this-&gt;path;

        // Save the image
        $image-&gt;save($destination);

        return $this;
    }

}


And in the controllers I do this:

$profilePicture = (new Photo($input))-&gt;makeProfilePicture();
Add a constructor. I'm pretty sure you can type hint your dependency too, so long as you know what you're working with, you can adjust to your needs.

use Symfony\Component\HttpFoundation\File\UploadedFile;

class Photo {    

    protected $fileName;
    protected $extension;
    protected $hashedName;
    protected $path;

    public function __construct(UploadedFile $input)
    {
        $this-&gt;fileName     = $input-&gt;getClientOriginalName();
        $this-&gt;extension    = $input-&gt;getClientOriginalExtension();
        $this-&gt;hashedName   = sha1(time() . $this-&gt;fileName);
        $this-&gt;path         = $this-&gt;hashedName . '.' .$this-&gt;extension;
    }

    // methods...

}


Please also note in your code you wrote protected $filename; then referenced back to that in your methods as $this-&gt;fileName - make sure you get the n or N consistent.

You can always make a getter for the path attribute instead of making it public keep it protected.

public function getPath()
{
    return $this-&gt;path;
}


Hope that helps!