It may be a little outwit, but in order to make life a little easier in the future, you can create separate implementations of each of them that extend the abstract class. This way you can combine and define an interface and easily add new types of tokens.
<?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 ]; } }
Then we create our Digital Ocean token class. This class can either use the default implementation or override 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 ]; } }
Now TokenRepository just takes care of attaching this token to the 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 is not working code, as I have not tried to run it, but it is just another idea on how you can organize and assign responsibility. Hope this helps, and welcome feedback from others.