src/Controller/DefaultController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\BlueInkHelper;
  4. use GuzzleHttp\Exception\RequestException;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. class DefaultController extends AbstractController {
  10.     /**
  11.      * @Route("/")
  12.      */
  13.     public function index()
  14.     {
  15.         return $this->render('default/index.html.twig');
  16.     }
  17.     /**
  18.      * @Route("/examples/upload")
  19.      */
  20.     public function example_upload()
  21.     {
  22.         return $this->render('examples/upload.html.twig');
  23.     }
  24.     /**
  25.      * Route handler that gets called when data is POST'ed to /embed/from_upload.
  26.      *
  27.      * Creates a BlueInk signing Bundle, and returns the URL for an embedded signing session.
  28.      * Note that the uploaded file is not provided in the POST data. Instead, it is a pre-existing
  29.      * file on a remote server, which will be fetched by its URL.
  30.      *
  31.      * In this example, we are using the Symfony framework, which does some automagic things
  32.      * like dependency injection, and setting up this route handler with the @Route comment below.
  33.      * Those details are not important for purposes of this example, but checkout the Symfony
  34.      * framework to learn more.
  35.      *
  36.      * @Route("/embed/from_upload", methods={"POST"})
  37.      */
  38.     public function embed_from_upload(Request $requestLoggerInterface $loggerBlueInkHelper $blueInkHelper)
  39.     {
  40.         // Get the $apiKey and $apiUrl. The $apiKey is private and should never be
  41.         // shared. In this example project it is stored in the environment, which is good
  42.         // practice for keeping App secrets private. We allow the API URL to be overridden
  43.         // as well, but in most deployments you can use the default BlueInk API URL.
  44.         $apiKey $this->getParameter('app.blueink_api_key');
  45.         $apiUrl $this->getParameter('app.blueink_api_url');
  46.         // Extract data uploaded in the request.
  47.         $name $request->request->get('name');
  48.         $email $request->request->get('email');
  49.         $company $request->request->get('company');
  50.         $title $request->request->get('title');
  51.         $color $request->request->get('color');
  52.         // We need an email to send final documents to, so for demo purposes we grab
  53.         // a default if no email was provided.
  54.         if (!$email) {
  55.             $email $this->getParameter('app.blueink_default_signer_email');
  56.         }
  57.         try {
  58.             // This method handles the actual creation of the Bundle and
  59.             // retrieving the embedded signing URL. See BlueInkHelper.php
  60.             // for details.
  61.             $embedUrl $blueInkHelper->createBundleFromDocument($apiKey$apiUrl$email$name$company$title$color);
  62.         } catch (RequestException $e) {
  63.             // createBundleFromDocument() can throw a RequestException, indicating
  64.             // one of the requests to the BlueInk API failed
  65.             // Try to get the response, so we can provide more details
  66.             // on any error that occurred.
  67.             $response $e->getResponse();
  68.             if ($response) {
  69.                 $status $response->getStatusCode();
  70.                 // Error details are returned as JSON in the response body.
  71.                 // The error details should point you to the specific issue
  72.                 // with a failed BlueInk API request.
  73.                 $responseBody $response->getBody();
  74.                 $logger->info($responseBody);
  75.                 $errorData = [
  76.                     'error' => "BlueInk API request failed with status code $status"
  77.                 ];
  78.             } else {
  79.                 $logger->warning('No response from BlueInk API');
  80.                 $errorData = [
  81.                     'error' => "BlueInk API request failed. No response when attempting to connect to $apiUrl"
  82.                 ];
  83.             }
  84.             return $this->json($errorData503);
  85.         }
  86.         // Return the embedded signing URL so that the frontend can
  87.         // create an embedded signing iFrame.
  88.         return $this->json(['embedUrl' => $embedUrl]);
  89.     }
  90. }