mardi 29 novembre 2016

Method refactoring?

In the TokenRepository you can see 3 similar methods. It create new entry to the tokens table but each method has different fields.

How can I refactor this? Should I merge 3 methods into 1 method or should I use strategy pattern?

TokenRepository Class:

class TokenRepository
{
   public function createTokenDigitalOcean(User $user, $name, $accessToken, $refreshToken = null)
   {
        return $user->tokens()->create([
            'name'          => $name,
            'provider'      => 'digital_ocean',
            'access_token'  => $accessToken,
            'refresh_token' => $refreshToken,
        ]);
    }

    public function createTokenLinode(User $user, $name, $key)
    {
        return $user->tokens()->create([
            'name'       => $name,
            'provider'   => 'linode',
            'linode_key' => $key,
        ]);
    }

    public function createTokenAws(User $user, $name, $key, $secret)
    {
        return $user->tokens()->create([
            'name'       => $name,
            'provider'   => 'aws',
            'aws_key'    => $key,
            'aws_secret' => $secret,
        ]);
    }
}

I have 3 classes like DigitalOceanProvider, LinodeProvider and AwsProvider. For example of using LinodeProvider class.

class LinodeProvider 
{
  public function callback()
  {
    $this->tokenRepo->createTokenLinode($user, $name, $key);
  }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2gEO6Z5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire