PHP code example of jeffersongoncalves / laravel-zero-credentials

1. Go to this page and download the library: Download jeffersongoncalves/laravel-zero-credentials library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jeffersongoncalves / laravel-zero-credentials example snippets


use JeffersonGoncalves\LaravelZero\Credentials\CredentialsContract;

final class Credentials implements CredentialsContract
{
    public function __construct(
        public readonly string $username,
        public readonly string $apiToken,
    ) {}

    public static function fromArray(array $data): static
    {
        return new static(
            username: $data['username'] ?? '',
            apiToken: $data['api_token'] ?? '',
        );
    }

    public function toArray(): array
    {
        return ['username' => $this->username, 'api_token' => $this->apiToken];
    }

    public function isValid(): bool
    {
        return $this->username !== '' && $this->apiToken !== '';
    }
}

use JeffersonGoncalves\LaravelZero\Credentials\AbstractAuthService;
use JeffersonGoncalves\LaravelZero\Credentials\CredentialsContract;

final class AuthService extends AbstractAuthService
{
    protected function appName(): string
    {
        return 'bb-cli'; // => ~/.bb-cli/config.json
    }

    protected function fromArray(array $data): CredentialsContract
    {
        return Credentials::fromArray($data);
    }
}

$auth = new AuthService;

$auth->save(new Credentials('alice', 'secret-token'));

$auth->isAuthenticated();          // true
$auth->load()->username;           // 'alice'
$auth->getConfigPath();            // /home/alice/.bb-cli/config.json

$auth->forget();                   // deletes the file

use JeffersonGoncalves\LaravelZero\Credentials\AuthenticationException;

if (! $auth->isAuthenticated()) {
    throw new AuthenticationException("Run 'bb auth:save' first.");
}

protected function configDir(): string
{
    $base = getenv('XDG_CONFIG_HOME') ?: $this->getHomeDir().'/.config';

    return $base.'/bb-cli';
}