Method refactoring? Method refactoring? laravel laravel

Method refactoring?


This may be a bit overkill, but in order to make life a bit easier in the future, you could create separate implementations of each that extend an abstract class. This way you can unify and define the interface and easily add new token types.

<?php namespace Foo\Tokens;abstract class Token{    protected $name = '';    protected $key = '';    protected $provider = '';    public function __construct($name, $key)    {        $this->name = $name;        $this->key = $key;    }    public function data()    {        return [            'name' => $this->name,            'provider' => $this->provider,            'token' => $this->key        ];    }}

Next, we create our Digital Ocean token class. This class can either use the default implementation or redefine it.

<?php namespace Foo\Tokens;use Foo\Tokens\Token;class DigitalOceanToken extends Token{    protected $provider = 'digital_ocean';    public function __construct($name, $key, $refreshToken = null)    {        parent::__construct($name, $key);        $this->refreshToken = $refreshToken;    }    public function data()    {        return [            'name' => $this->name,            'provider' => $this->provider,            'key' => $this->key,            'refreshToken' => $this->refreshToken        ];    }}

The TokenRepository now merely cares about attaching a given token to a user.

<?php namespace Foo;use User;use Foo\Tokens\Token;class TokenRepository{   public function createToken(User $user, Token $token)   {        return $user->tokens()->create(            $token->data()        );    }}

And your service providers are as simple as...

<?php use Foo\Tokens\AwsToken;class AwsProvider{    public function callback()    {        $this->tokenRepo->createToken(            $user, new AwsToken($name, $key, $secret)        );    }}

This isn't working code, as I've not attempted to run it however it's just another idea of how you can organize and assign responsibility. Hope it helps, and welcome feedback from others.


According to me you should implement it like this:

class TokenRepository{    public function createTokenForVendor(User $user, $inputs)    {        return $user->tokens()->create($inputs);    }}

and inside your callback:

class VendorProvider {  public function callback()  {    switch($tokenType) {        case 'DigitalOcean':            $inputs = [            'name'          => $name,            'provider'      => 'digital_ocean',            'access_token'  => $accessToken,            'refresh_token' => $refreshToken,        ];        break;      case 'Linode':            $inputs = [            'name'       => $name,            'provider'   => 'linode',            'linode_key' => $key,        ];        break;      case 'Aws':            $inputs = [            'name'       => $name,            'provider'   => 'aws',            'aws_key'    => $key,            'aws_secret' => $secret,        ];        break;    }    $this->tokenRepo->createTokenForVendor($user, $inputs);  }}

Hoping you should do some code structure revamp.

Hope this helps!