server/src/app/Services/TranscriptManager.php
changeset 163 59c68fc4848e
child 396 c8f651e7e4ff
equal deleted inserted replaced
162:a6cf5a06f02d 163:59c68fc4848e
       
     1 <?php
       
     2 namespace CorpusParole\Services;
       
     3 
       
     4 use CorpusParole\Models\Document;
       
     5 
       
     6 use GuzzleHttp\Client;
       
     7 
       
     8 class TranscriptManager implements TranscriptManagerInterface {
       
     9 
       
    10     public function __construct(Client $client) {
       
    11         $this->client = $client;
       
    12     }
       
    13 
       
    14 
       
    15     public function getConverterUrl(string $converterKey, Document $document, string $url) {
       
    16 
       
    17         $response = $this->client->get($url);
       
    18         $statusCode = $response->getStatusCode();
       
    19         if($statusCode < 200 || $statusCode > 299 ) {
       
    20             throw new TranscriptManagerException("Can not get transcript content : $statusCode -> ".$response->getReasonPhrase());
       
    21         }
       
    22 
       
    23         return $this->getConverter($converterKey, $document, $response->getBody());
       
    24 
       
    25     }
       
    26 
       
    27     public function getConverter(string $converterKey, Document $document, string $source) {
       
    28 
       
    29         $converterClassMapping = config('corpusparole.transcrit_decoder_mapping');
       
    30         if(!array_key_exists($converterKey, $converterClassMapping)) {
       
    31             throw new TranscriptManagerException("Transcript type $converterKey doe not exists");
       
    32         }
       
    33         $converterClass = $converterClassMapping[$converterKey];
       
    34         if(empty($converterClass)) {
       
    35             throw new TranscriptManagerException("Transcript type $converterKey doe not exists (empty class)");
       
    36         }
       
    37 
       
    38         return new $converterClass($document, $source);
       
    39 
       
    40     }
       
    41 }