#dynamic select trait

1 messages · Page 1 of 1 (latest)

celest heron
#

I have 3 trait's for 3 cell providers
these traits has same method names
I store the provide type in the db

I'm looking for someway to change between the traits according to the stored value
so when I run get balance() it's automatically run the method from the correct trait.

I think about this solution in model

public function traitmethods($methodname){
 $class = $this->provider 
return $class::$methodname;
}

can I do something like this ?

Number::find(1)->provider::Getbalance
restive timber
fading brook
#

@celest heron Please don’t post in multiple places, as I’ve responded to this same question elsewhere: #help message

restive timber
#

Which even boiled down to basically the same answer, feels even more redundant

celest heron
celest heron
#

the problem is each provider has different api so I don't think builder pattern will really fits, I'm not expert so the image you see isn't the same on my side.

celest heron
celest heron
restive timber
# celest heron the problem is each provider has different api so I don't think builder pattern...

That's exactly what a builder patterns solves tho. Think about Laravel's cache, it's a single interface you can use to set stuff in the cache. It doesn't matter how the cache is implemented, it selects the cache you have configured. Setting the actual values in cache is done differently in each builder (redis, file cache, memcached etc)
You have 3 providers, probably to send sms or something? So an interface and each implements that interface. Then depending on what's configured you create an instance of the provider and call send() or whatever

celest heron
#

it's just above my experience I'll take some time to understand it. this my first time deal with builder

celest heron
#

I understand know how it works
thanks so much

celest heron
#

do I need to use the dependency injection like they did(director class)or just instance the implementation class?

fading brook
#

I think you need to read up on some OOP fundamentals, as you seem to be getting far too concerned with implementation and details whilst, but asking quite basic questions at the same time.

#

As Robert and I have told you, you should have a single interface that describes the common functionality, and then various classes—one per provider—that implements that interface and also adds the specific implementation for that specific provider.

#

A (simplified) example:

use App\ValueObjects\PhoneNumber;

interface SmsProvider
{
    public function sendSms(PhoneNumber $from, PhoneNumber $to, string $message);
}
use Vonage\Client;
use Vonage\SMS\Message\SMS;

class VonageSmsProvider implements SmsProvider
{
    protected $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function sendSms(PhoneNumber $from, PhoneNumber $to, string $message)
    {
        $text = new SMS($to, $from, $message);

        $response = $this->client->sms()->send($text)->current();

        return $response->getStatus() == 0;
    }
}

You can use the container to build the VonageSmsProvider class:

$this->app->singleton(VonageSmsProvider::class, function () {
    $credentials = new Basic(
        $this->app['config']['services.vonage.api_key'],
        $this->app['config']['services.vonage.api_secret'],
    );

    $client = new Client($credentials);

    return new VonageSmsProvider($client);
});

And then also specify which provider should implement the interface in your application:

$this->app->singleton(SmsProvider::class, VonageSmsProvider::class);

So when you type-hint SmsProvider, Laravel will work out you want an instance of VonageSmsProvider, and build an instance of that class.

celest heron
celest heron
fading brook
#
class Foo extends Model
{
    public function getSmsProvider(): SmsProvider
    {
        return match ($this->provider) {
            case 'vonage' => app(VonageSmsProvider::class),
            default => throw new InvalidArgumentException("[{$this->provider}] is an invalid provider"),
        }
    }
}
#
$foo->getSmsProvider->sendSms($from, $to, $message);
celest heron
#

cool !!!!

#

that's awesome 👌