crowdvalley/crowdentials-api-wrapper-bundle
Installation
composer require crowdvalley/crowdentials-api-wrapper-bundle
Add the bundle to config/bundles.php:
return [
// ...
CrowdValley\CrowdentialsApiWrapperBundle\CrowdentialsApiWrapperBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console crowdentials:install
Update config/packages/crowdentials_api_wrapper.yaml with your API credentials:
crowdentials_api_wrapper:
api_key: '%env(CROWDENTIALS_API_KEY)%'
api_secret: '%env(CROWDENTIALS_API_SECRET)%'
base_uri: 'https://api.crowdvalley.com'
First Use Case: Authentication Inject the client into a service/controller:
use CrowdValley\CrowdentialsApiWrapper\Client;
class AuthController extends AbstractController {
public function __construct(private Client $client) {}
public function login(Request $request) {
$response = $this->client->auth()->login([
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
]);
// Handle response
}
}
Service Layer Integration Create a dedicated service to abstract API calls:
namespace App\Service;
use CrowdValley\CrowdentialsApiWrapper\Client;
class CrowdentialsService {
public function __construct(private Client $client) {}
public function verifyEmail(string $token): bool {
return $this->client->auth()->verifyEmail($token)->isSuccess();
}
}
Event-Driven Extensions
Listen to Crowdentials events (e.g., CrowdentialsEvent::USER_CREATED) via Symfony’s event dispatcher:
# config/services.yaml
services:
App\EventListener\CrowdentialsListener:
tags:
- { name: kernel.event_listener, event: crowdentials.user.created, method: onUserCreated }
Form Integration Use Crowdentials validators in Symfony forms:
use CrowdValley\CrowdentialsApiWrapper\Validator\Constraints as CrowdentialsAssert;
$builder->add('email', EmailType::class, [
'constraints' => [
new CrowdentialsAssert\EmailExists(),
],
]);
Command-Line Operations Create console commands for bulk operations:
use CrowdValley\CrowdentialsApiWrapper\Client;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SyncUsersCommand extends Command {
protected static $defaultName = 'crowdentials:sync-users';
public function __construct(private Client $client) {}
protected function execute(InputInterface $input, OutputInterface $output): int {
$users = $this->client->users()->list(['limit' => 100]);
// Process users...
}
}
Rate Limiting
$this->client->users()->list(['cache' => true]); // Uses Symfony cache system
Token Expiry
TokenExpiredException gracefully:
try {
$this->client->auth()->me();
} catch (\CrowdValley\CrowdentialsApiWrapper\Exception\TokenExpiredException $e) {
$this->client->auth()->refreshToken();
// Retry logic
}
Webhook Validation
CrowdentialsWebhookValidator:
$validator = new \CrowdValley\CrowdentialsApiWrapper\Webhook\Validator(
$this->client->getConfig()->getApiSecret()
);
if (!$validator->validate($request->getContent(), $request->headers->get('X-Signature'))) {
throw new \RuntimeException('Invalid webhook signature');
}
Logging
Enable debug logging for API calls in config/packages/crowdentials_api_wrapper.yaml:
crowdentials_api_wrapper:
debug: '%kernel.debug%'
Testing
Use the CrowdentialsApiWrapperBundleTestCase for unit tests:
use CrowdValley\CrowdentialsApiWrapperBundle\Tests\CrowdentialsApiWrapperBundleTestCase;
class MyTest extends CrowdentialsApiWrapperBundleTestCase {
public function testSomething() {
$this->assertTrue(true);
}
}
Custom Endpoints Extend the client for private endpoints:
namespace App\Service;
use CrowdValley\CrowdentialsApiWrapper\Client;
use CrowdValley\CrowdentialsApiWrapper\Endpoint\AbstractEndpoint;
class CustomEndpoint extends AbstractEndpoint {
protected $uri = 'custom/endpoint';
}
// Register in services.yaml:
services:
App\Service\CustomEndpoint:
arguments:
$client: '@crowdentials.client'
Environment-Specific Config
Override config per environment (e.g., config/packages/dev/crowdentials_api_wrapper.yaml):
crowdentials_api_wrapper:
base_uri: 'https://sandbox.api.crowdvalley.com'
Error Handling Centralize API error handling with a decorator:
use CrowdValley\CrowdentialsApiWrapper\ClientInterface;
use CrowdValley\CrowdentialsApiWrapper\Exception\ApiException;
class DecoratedClient implements ClientInterface {
public function __construct(private ClientInterface $client) {}
public function call(string $method, string $uri, array $params = []) {
try {
return $this->client->call($method, $uri, $params);
} catch (ApiException $e) {
// Log, translate, or retry
throw new \RuntimeException('API Error: ' . $e->getMessage());
}
}
}
How can I help you explore Laravel packages today?