Skip to content

Instantly share code, notes, and snippets.

@adamgoose
Created April 17, 2014 01:12
Show Gist options
  • Save adamgoose/10946439 to your computer and use it in GitHub Desktop.
Save adamgoose/10946439 to your computer and use it in GitHub Desktop.
<?php namespace Flight\Importers\Drivers;
use File;
use Flight\Asset;
use Flight\Flight;
class Image implements ImporterDriverInterface {
/**
* Open Graph Objects
* @var array
*/
protected $graphObjects;
/**
* Flight to import to
* @var Flight\Flight
*/
protected $flight;
/**
* Setup the driver
* @param array $graphObjects
*/
public function __construct(array $graphObjects, Flight $flight)
{
$this->graphObjects = $graphObjects;
$this->flight = $flight;
}
/**
* Import the image, and create the asset model
* @return Asset
*/
public function doImport()
{
if($this->getImage() != '') {
// Delete any existing asset files
if($this->flight->asset)
File::delete(public_path().$this->flight->asset->file);
$asset = $this->flight->asset ?: new Asset;
$asset->fill([
'type' => 'image',
'file' => $this->getImageName(),
]);
File::put(public_path().$this->flight->path.$this->getImageName(), file_get_contents($this->getImage()));
$this->flight->asset()->save($asset);
return $asset;
}
}
/**
* Get the image URL from the OpenGraph info
* @return string URL of image
*/
private function getImage()
{
return $this->graphObjects['og:image'][0]['og:image:url'];
}
/**
* Get the image's filename
* @return string Image filename
*/
private function getImageName()
{
return pathinfo(parse_url($this->getImage(), PHP_URL_PATH), PATHINFO_BASENAME);
}
}
<?php namespace Flight\Importers;
use Str;
use Flight\Asset;
use Flight\Flight;
use Opengraph\Reader as Graph;
class Import {
/**
* Domains for import type detection
* @var array
*/
protected $domains = [
'youtube' => ['youtube.com', 'youtu.be'],
'vimeo' => ['vimeo.com'],
];
/**
* URL of asset to import
* @var string
*/
protected $url;
/**
* Flight to import to
* @var Flight\Flight
*/
protected $flight;
/**
* Import type
* @var string
*/
protected $type;
/**
* Import Driver
* @var ImporterDriverInterface
*/
protected $driver;
/**
* Setup the class
* @param string $url
*/
public function __construct($url, Flight $flight)
{
$this->url = $url;
$this->flight = $flight;
$this->graph = new Graph;
$this->graph->parse(file_get_contents($url));
$this->detectImportType();
$this->fetchDriver();
}
/**
* Helper static method to instantiate and preform import.
* Only useful in production!
* @param string $url
* @param Flight $flight
* @return Asset
*/
public static function to($url, Flight $flight)
{
$importer = new self($url, $flight);
return $importer->doImport();
}
/**
* Do the import (on the driver)
* @return Asset
*/
public function doImport()
{
return $this->driver->doImport();
}
/**
* Get OpenGraph META tags
* @return array
*/
public function getGraphObjects()
{
return $this->graph->getArrayCopy();
}
/**
* Return the import type
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Return the import driver
* @return ImporterDriverInterface
*/
public function getDriver()
{
return $this->driver;
}
/**
* Detect import type
* @return void
*/
private function detectImportType()
{
if(Str::contains($this->getImportHostname(), $this->domains['youtube']))
$this->type = 'Youtube';
elseif(Str::contains($this->getImportHostname(), $this->domains['vimeo']))
$this->type = 'Vimeo';
else
$this->type = 'Image';
}
/**
* Parse the import URL's hostname
* @return string Hostname of URL
*/
private function getImportHostname()
{
return parse_url($this->url, PHP_URL_HOST);
}
/**
* Fetch the appropriate import driver
* @return void
*/
private function fetchDriver()
{
$driver = 'Flight\Importers\Drivers\\'.$this->type;
$this->driver = new $driver($this->getGraphObjects(), $this->flight);
}
}
<?php namespace Flight\Importers\Drivers;
use Flight\Flight;
interface ImporterDriverInterface {
public function __construct(array $graphObjects, Flight $flight);
public function doImport();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment