<?php
namespace Fifa\ConnectId\Sdk\ExceptionHandler\Handler;

use Fifa\ConnectId\Sdk\Api\ApiException;
use Fifa\ConnectId\Sdk\Api\Model\ConflictResponseType;
use Fifa\ConnectId\Sdk\Exception\FifaConflictException;

/**
 * Class ConflictResponseHandler
 * @package Fifa\ConnectId\Sdk\ExceptionHandler\Handler
 */
class ConflictResponseHandler implements HandlerInterface
{
    /**
     * Handler method for FifaConflictException
     *
     * @param ApiException $e
     * @return FifaConflictException|null
     */
    public function handle(ApiException $e): ?FifaConflictException
    {
        if ($e->getCode() == 409 && $e->getResponseObject() instanceof ConflictResponseType) {
            return new FifaConflictException($e, $e->getResponseObject(), is_string($e->getResponseBody()) ? $e->getResponseBody() : $e->getMessage());
        }

        return null;
    }
}