Skip to content

Commit

Permalink
A terrible little hack until I get time to refactor it
Browse files Browse the repository at this point in the history
  • Loading branch information
nark3d committed Jan 2, 2016
1 parent 22fb31b commit 11f7430
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,52 @@ class File extends ValueObject
*/
public function exists()
{
return file_exists($this->getValue());
if ($this->isUrl()) {
return $this->isReachableUrl();
} else {
return file_exists($this->getValue());
}
}

public function isUrl()
{
return filter_var($this->getValue(), FILTER_VALIDATE_URL);
}

/**
* Refactor this, it's terrible.
*
* @param bool|true $followRedirects
* @return bool|mixed
*/
public function isReachableUrl($followRedirects = true)
{
if (! $ch = curl_init($this->getValue())) {
return false;
}
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
curl_setopt($ch, CURLOPT_NOBODY, true); // don't need body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // catch output (do NOT print!)

if ($followRedirects) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
}

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TI/MEOUT, 5);
curl_exec($ch);
if (curl_errno($ch)) { // should be 0
curl_close($ch);
return false;
}

$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@curl_close($ch);

return $code;
}

/**
Expand Down Expand Up @@ -52,6 +97,6 @@ public static function fromString($string)

public static function fromUrl($url)
{
return self::fromString(file_get_contents($url));
return self::fromString($url);
}
}

0 comments on commit 11f7430

Please sign in to comment.