simioluwatomi's avatar

Write this as one method

I have two methods that do practically the same thing; uploading an image to S3. However, they have different arguments and the only different thing about the arguments is the Form Request class. I'm thinking there should be a way to write this so that its one single method.

Thank you for your time

/**
     * @param StoreMessageRequest $request
     * @param $s3
     * @return string
     */
    public function uploadAlbumArt(StoreMessageRequest $request, $s3)
    {
        $albumArtName = md5($request->albumArtName . microtime()) . '.jpg';
        $albumArt = Image::make(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->albumArtImage)))->stream();
        $albumArtPath = 'albumarts/' . $albumArtName;
        $s3->put($albumArtPath, (string)$albumArt, 'public');
        return $albumArtPath;
    }

    /**
     * @param UpdateMessageRequest $request
     * @param $s3
     * @return string
     */
    public function updateAlbumArt(UpdateMessageRequest $request, $s3)
    {
        $albumArtName = md5($request->albumArtName . microtime()) . '.jpg';
        $albumArt = Image::make(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->albumArtImage)))->stream();
        $albumArtPath = 'albumarts/' . $albumArtName;
        $s3->put($albumArtPath, (string)$albumArt, 'public');
        return $albumArtPath;
    }
0 likes
4 replies
biishmar's avatar
biishmar
Best Answer
Level 11

put that in common method and call it with different parameter.. like

public function albumArt($request, $s3)
    {
        $albumArtName = md5($request->albumArtName . microtime()) . '.jpg';
        $albumArt = Image::make(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->albumArtImage)))->stream();
        $albumArtPath = 'albumarts/' . $albumArtName;
        $s3->put($albumArtPath, (string)$albumArt, 'public');
        return $albumArtPath;
    }

public function uploadAlbumArt(StoreMessageRequest $request, $s3)
    {
        $this->albumArt( $request, $s3);
    }

public function updateAlbumArt(StoreMessageRequest $request, $s3)
    {
        $this->albumArt( $request, $s3);
    }
1 like
arthurvillar's avatar

Can you have one class called MessageRequest that would handle both store and update functionalities?

sutherland's avatar

To expand on @arthurvillar's answer, you can check the request method in your request class if you need to conditionally add rules depending on whether it's a POST or PATCH request.

    public function rules()
    {
        $rules = [
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
        ];

        if ($this->isMethod('POST')) {
            $rules['referral'] = 'required';
        }

        if ($this->isMethod('PATCH')) {
            $rules['field'] = 'required';
        }

        return $rules;
    }

Not sure if this is a good practice or not but I've found it useful in a few cases.

1 like
simioluwatomi's avatar

It will be really cool if I could have it as just one Form request class but that Form Request class would become messy. Because each of those Form request classes are already adding extra parameters to the request based on if else conditions...

I think I'm gonna go with @biishmar's reply... Thanks guys

@arthurvillar I would really love to see that implementation

Edit

This is what I ended up writing.

    /**
     * @param $request
     * @param $s3
     * @return string
     */
    public function uploadAlbumArt($request, $s3)
    {
        $albumArtName = md5($request->albumArtName . microtime()) . '.jpg';
        $albumArt = Image::make(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->albumArtImage)))->stream();
        $albumArtPath = 'albumarts/' . $albumArtName;
        $s3->put($albumArtPath, (string)$albumArt, 'public');
        return $albumArtPath;
    }

Please or to participate in this conversation.