server/src/app/Services/TranscriptManager.php
author ymh <ymh.work@gmail.com>
Tue, 27 Sep 2016 23:43:29 +0200
changeset 304 20071981ba2a
parent 163 59c68fc4848e
child 396 c8f651e7e4ff
permissions -rw-r--r--
add location and geonames resolvers and api
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
namespace CorpusParole\Services;
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
use CorpusParole\Models\Document;
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
use GuzzleHttp\Client;
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
class TranscriptManager implements TranscriptManagerInterface {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
    public function __construct(Client $client) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
        $this->client = $client;
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    }
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    public function getConverterUrl(string $converterKey, Document $document, string $url) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        $response = $this->client->get($url);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        $statusCode = $response->getStatusCode();
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        if($statusCode < 200 || $statusCode > 299 ) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
            throw new TranscriptManagerException("Can not get transcript content : $statusCode -> ".$response->getReasonPhrase());
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        }
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        return $this->getConverter($converterKey, $document, $response->getBody());
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    }
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    public function getConverter(string $converterKey, Document $document, string $source) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        $converterClassMapping = config('corpusparole.transcrit_decoder_mapping');
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        if(!array_key_exists($converterKey, $converterClassMapping)) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            throw new TranscriptManagerException("Transcript type $converterKey doe not exists");
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        }
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        $converterClass = $converterClassMapping[$converterKey];
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        if(empty($converterClass)) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            throw new TranscriptManagerException("Transcript type $converterKey doe not exists (empty class)");
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        }
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        return new $converterClass($document, $source);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    }
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
}