/**
  * @param ClientMetadata $data
  * @return Client
  */
 private function registerClient(EntityManager $em, ClientMetadata $data)
 {
     if ($data->getClient() === null) {
         $client = $data->toClient();
     } else {
         $client = $data->getClient();
     }
     if ($client->getName() === null) {
         $firstUrl = $this->getHost($client->getRedirectUris()[0]);
         $client->setName($firstUrl);
     }
     if ($client->getDescription() === null) {
         $client->setDescription('');
     }
     if ($client->getTermsOfUseUrl() === null) {
         $client->setTermsOfUseUrl('');
     }
     if ($client->getSiteUrl() === null) {
         $client->setSiteUrl('');
     }
     if (count($data->getContacts()) > 0) {
         $owners = $em->getRepository($this->getParameter('user.class'))->findByEmail($data->getContacts());
         foreach ($owners as $person) {
             if ($person->getConfirmationToken() !== null) {
                 continue;
             }
             $client->getOwners()->add($person);
         }
     }
     $publicScopes = explode(' ', $this->getParameter('lc_public_scopes'));
     $client->setAllowedScopes($publicScopes);
     $em->persist($client);
     $data->setClient($client);
     $em->persist($data);
     $em->flush();
     return $client;
 }