- How do I authenticate with Zammad in Laravel using OAuth2 tokens instead of username/password?
- Use the `oauth2_token` parameter in the `Client` constructor. For token management, pair this with Laravel Passport or Sanctum to handle token rotation, revocation, and storage in the database. Store tokens in Laravel’s cache or session for short-lived access, or encrypt them in the database for long-term use.
- Can this client work with Laravel’s queue system for bulk operations like ticket imports?
- Yes. Dispatch jobs (e.g., `ZammadTicketImportJob`) to Laravel’s queue, then use the client’s methods like `import()` or `save()` within the job. Offload heavy operations to workers like Redis or database queues, and handle failures with Laravel’s retry logic or dead-letter queues.
- What Laravel versions and PHP versions does this package support?
- The package requires PHP 7.2+ and is compatible with Laravel 6+. Test thoroughly with your Laravel version, as some newer Laravel features (e.g., Symfony HTTP client in Laravel 9+) may require adjustments. Check the package’s `composer.json` for PHP version constraints.
- How do I mock the Zammad API for testing in Laravel?
- Use Laravel’s HTTP client mocking (e.g., `Http::fake()`) to intercept calls to the Zammad API. For unit tests, mock the `Client` instance directly with Mockery or PHPUnit’s mock builder. For integration tests, use Vapor or a local Zammad instance with test data, or stub API responses with `Http::fake()` and predefined responses.
- Will this client work with Laravel’s API Resources to standardize responses?
- Absolutely. Wrap Zammad `Resource` objects in Laravel’s `ApiResource` classes to transform data before serialization. For example, create a `TicketResource` that maps Zammad’s ticket fields to your API schema. Use `ApiResource::collection()` to return consistent JSON structures across your Laravel API.
- How do I handle rate limits or throttling for high-frequency API calls (e.g., webhooks)?
- Leverage Laravel’s `throttle` middleware or the `throttle` trait in controllers to limit requests. For webhooks, use Laravel’s `queue:work` to process incoming payloads asynchronously. Monitor API response headers (e.g., `X-RateLimit-Remaining`) and implement exponential backoff in your client logic if needed.
- Can I sync Zammad data locally to Laravel models, and how do I handle conflicts?
- Yes, use the client to fetch Zammad data and hydrate Laravel models (e.g., `Ticket::create($ticketResource->toArray())`). For conflicts, implement optimistic locking by comparing `updated_at` timestamps or using Laravel’s `lockForUpdate()`. For critical data, consider a CQRS pattern where Laravel models are read-only and writes go through Zammad.
- What’s the best way to structure this client in a Laravel project for maintainability?
- Encapsulate Zammad logic in Laravel services (e.g., `ZammadTicketService`, `ZammadUserSyncService`) and inject the `Client` instance via the service container. Use facades sparingly; prefer dependency injection. Group related services in a `Services/Zammad` directory and document their API contracts for clarity.
- Are there alternatives to this client if I need more advanced features like webhook handling?
- For webhooks, consider extending this client or using Laravel’s `Broadcast` system to listen for Zammad webhook events. For more advanced use cases, evaluate Zammad’s official JavaScript SDK or build a custom solution with Guzzle. However, this client is the most idiomatic for PHP/Laravel integration and covers 90% of use cases.
- How do I monitor API performance (e.g., response times) in Laravel?
- Log API response times using Laravel Telescope or a monitoring tool like Prometheus. Wrap client calls in a custom middleware or decorator that records metrics (e.g., `startTime = microtime(true); $client->get(...); logDuration(startTime)`). For critical paths, set up alerts in Laravel Forge or New Relic if response times exceed thresholds.