webrobert's avatar

This ok? Logic for laravel package submission

I am just wrapping up a package to submit to laravel to add ClickSend as an additional sms option for notifications... clicksend-notification-channel

I tried to mimic the conventions in the vonage package, all but this one file. I've never submitted or written a package for that matter. For a quick peer check, Is this okay?

Here is my logic for credentials...

/**
 * Create a new ClickSend Client.
 *
 * @return \ClickSend\Api\SMSApi
 *
 * @throws \RuntimeException
 */
public function client() : Client
{
    [$username, $password] = $this->firstComboOrFail([
        'accountCredentials' => [
            $this->config['account_username'], $this->config['account_password']
        ],
        'apiCredentials' => [
            $this->config['api_username'], $this->config['api_key']
        ]
    ]);

    $credentials = Configuration::getDefaultConfiguration()
        ->setUsername($username)
        ->setPassword($password);

    return new Client($this->client, $credentials);
}

private function firstComboOrFail(array $combos)
{
    $credentials = collect($combos)
        ->first( fn($pair) => ! in_array(null, $pair, true));

    if(! $credentials) {
        throw new RuntimeException('Please provide your ClickSend API credentials.
        Possible combinations: api_username + api_key OR account_username + account_password.');
    }

    return $credentials;
}

For comparison here is the vonage file (note: Vonage offers more possible credential combos).

I think mine is more readable. But I could be missing some convention or standard?

0 likes
1 reply
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I only have a few packages under my belt but for me this is very easy to understand. I would feel safe in changing the code as I can tell what happens at a glance

Personally I would prefer my own custom exception, but that's the only change I can spot

1 like

Please or to participate in this conversation.