Skip to content

Commit

Permalink
Added: option to turn off throw exception
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGalek committed Apr 11, 2017
1 parent a643d0b commit 7681347
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
20 changes: 20 additions & 0 deletions docs/en/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,23 @@ Every properties you find in [Video documentation](https://github.com/TroiaStudi
</div>
```

### Exception
you can turn of throw exception and get empty video object

#### Basic
```php
$apiKey = 'MySuperSecretYoutubeApiKey';
$youtube = new \TroiaStudio\YoutubeAPI\Reader($apiKey, null, false);
```

#### Nette

config.neon
```neon
extensions:
youtubeAPI: TroiaStudio\YoutubeAPI\DI\Extension
youtubeAPI:
apiKey: 'MySuperSecretYoutubeApiKey'
exception: false
```
8 changes: 5 additions & 3 deletions src/TroiaStudio/Utils/Youtube-API/DI/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public function loadConfiguration()
$builder = $this->getContainerBuilder();
$config = $this->getConfig([
'apiKey' => null,
'httpClient' => null
'httpClient' => null,
'exception' => true
]);

$builder->addDefinition($this->prefix('troiastudioyoutubeapi'))
->setClass('TroiaStudio\YoutubeAPI\Reader', [
'apiKey' => $config['apiKey'],
'httpClient' => $config['httpClient']
'apiKey' => $config['apiKey'],
'httpClient' => $config['httpClient'],
'exception' => $config['exception']
]);
}
}
12 changes: 10 additions & 2 deletions src/TroiaStudio/Utils/Youtube-API/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class Reader
/** @var Client */
private $httpClient;

public function __construct($apiKey = null, $httpClient = null)
/** @var bool */
private $exception;

public function __construct($apiKey = null, $httpClient = null, $exception = true)
{
if ($apiKey !== null) {
$this->apiKey = $apiKey;
Expand All @@ -58,6 +61,7 @@ public function __construct($apiKey = null, $httpClient = null)
]);
}
$this->httpClient = $httpClient;
$this->exception = $exception;
}

public function setUrl($url)
Expand Down Expand Up @@ -145,7 +149,11 @@ private function createVideo($data, $videoId)
$json = Nette\Utils\Json::decode($data);

if (!isset($json->items[0]->snippet) || !isset($json->items[0]->contentDetails) || !isset($json->items[0]->status) || !isset($json->items[0]->statistics)) {
throw new \RuntimeException("Empty YouTube response, probably wrong '{$videoId}' video id.");
if ($exception) {
throw new \RuntimeException("Empty YouTube response, probably wrong '{$videoId}' video id.");
} else {
$this->videos[$videoId] = new Video;
}
}

$snippet = $json->items[0]->snippet;
Expand Down
16 changes: 16 additions & 0 deletions src/TroiaStudio/Utils/Youtube-API/Thumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace TroiaStudio\YoutubeAPI;

use Nette;

class Thumbnail
{
/** @var string */
public $url = '';

/** @var integer */
public $width = 0;

/** @var integer */
public $height = 0;
}
15 changes: 14 additions & 1 deletion src/TroiaStudio/Utils/Youtube-API/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,18 @@ class Video
/**
* @var array
*/
public $thumbs;
public $thumbs = [
'default' => null,
'medium' => null,
'high' => null,
'standard' => null,
'maxres' => null
];

public function __construct()
{
foreach ($this->thumbs as $index => $thumb) {
$this->thumbs[$index] = new Thumbnail;
}
}
}

0 comments on commit 7681347

Please sign in to comment.