# Symfony

[`addresszen/symfony-bundle`](https://packagist.org/packages/addresszen/symfony-bundle) configures the [PHP SDK](/docs/sdks/php.md) from your application config and lets you inject the client into a controller or service. It supports Symfony 6.4, 7 and 8 on PHP 8.2 or later.

## Install

```bash
composer require addresszen/symfony-bundle
```

Symfony Flex registers the bundle. Without Flex, add it to `config/bundles.php`:

config/bundles.phpphp

```php
return [
    // ...
    AddressZen\Symfony\AddressZenSymfonyBundle::class => ['all' => true],
];
```

## Configure

Set your API key, and the Management Key if you manage keys, licensees or configurations from this application. Both belong in environment variables, never in a committed file.

config/packages/addresszen.yamlyaml

```yaml
addresszen:
    api_key: '%env(ADDRESSZEN_API_KEY)%'
    management_key: '%env(ADDRESSZEN_MANAGEMENT_KEY)%'
```

.env.localbash

```bash
ADDRESSZEN_API_KEY=ak_test
ADDRESSZEN_MANAGEMENT_KEY=uk_...
```

| Option           | Purpose                                                                                                                 |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `api_key`        | Required. Your API key, or a licensee key beginning `sl_`                                                               |
| `management_key` | Your Management Key for key, licensee and configuration operations. Leave it unset elsewhere                            |
| `base_uri`       | Defaults to `https://api.addresszen.com/v1`                                                                             |
| `http_client`    | Service id of a PSR-18 client to send requests with. See below for the default                                          |
| `headers`        | Headers sent with every request. A `User-Agent` naming your application is sent ahead of the bundle and SDK identifiers |

## Use the client

Type-hint `AddressZen\Client`. It is a private, autowirable service, also under `AddressZen\Contracts\ClientContract`.

src/Controller/AddressController.phpphp

```php
use AddressZen\Client;
use AddressZen\Types\UsaVerifyMatch;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\Routing\Attribute\Route;

final class AddressController
{
    public function __construct(private readonly Client $client) {}

    #[Route('/verify')]
    public function verify(#[MapQueryParameter] string $address): JsonResponse
    {
        $result = $this->client->addressVerify(['query' => $address])->result;

        return new JsonResponse([
            'verified' => $result instanceof UsaVerifyMatch,
            'result' => $result,
        ]);
    }

    #[Route('/suggest')]
    public function suggest(#[MapQueryParameter] string $query): JsonResponse
    {
        return new JsonResponse($this->client->findAddress(['query' => $query])->result->hits);
    }
}
```

Response objects are `JsonSerializable`, so `JsonResponse` renders them with the API's field names.

Each API operation is one method on the client, such as `addressVerify()` or `keyUsage()`. The [endpoints reference](/docs/sdks/php/endpoints.md) lists them all. The service id is `addresszen.client`.

## HTTP client

The bundle uses the `Psr\Http\Client\ClientInterface` service when your application has one. Symfony's HttpClient component registers it once a PSR-17 factory is installed:

```bash
composer require symfony/http-client nyholm/psr7
```

Timeouts, retries and proxies then come from `framework.http_client`:

config/packages/framework.yamlyaml

```yaml
framework:
    http_client:
        default_options:
            timeout: 5
```

Without that service, the SDK discovers any installed PSR-18 client, such as Guzzle. To use a specific client, set `http_client` to its service id.

## Test code that uses the SDK

Replace the HTTP client in the test environment with a mock that answers from fixtures. `php-http/mock-client` is a PSR-18 client that returns queued responses.

```bash
composer require --dev php-http/mock-client
```

config/services\_test.yamlyaml

```yaml
services:
    Http\Mock\Client: ~

addresszen:
    api_key: ak_test
    http_client: Http\Mock\Client
```

tests/AddressControllerTest.phpphp

```php
$mock = static::getContainer()->get(Http\Mock\Client::class);
$mock->addResponse(new Nyholm\Psr7\Response(200, ['Content-Type' => 'application/json'], $verifyBody));

$client->request('GET', '/verify?address=1010 Cauthen Ln, Alamogordo, NM 88310');
```

Errors are the SDK's own exceptions. See [error handling](/docs/sdks/php/errors.md).
