jeudi 26 janvier 2017

Looking for design pattern - one type of object with different sources

With CsvReader:

class CsvReader {
  public static function createFromString($csvString){
    /.../
    return new static($something)
  }
}

I'm planning to operate with csvString coming from different sources: reading file, from body of http response, etc. So im looking for design pattern for easy creation of this csvReaders objects. For now, i have ended with something like this:

use SplFileInfo as UploadedFile;
use CsvReader as Reader;
use GuzzleHttp\Client as HttpClient;

class CsvFactory {
  public function createFromUploadedFile(UploadedFile $uploadedFile){
    return Reader::createFromString($uploadedFile->toString());
  }

  public function createFromHttpEndpoint(HttpClient $client, $url, $options = array()){
    $response = $client->request('GET', $url, $options);

    if($response->getStatusCode() != 200){
       throw new Exception('Http Code Not Ok', $response->getStatusCode());
    }

   return Reader::createFromString($response->getBody());
 }
}

I feel that it could be accomplished in the better way. But how?

Aucun commentaire:

Enregistrer un commentaire