Skip to content

Commit

Permalink
added types
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Dec 23, 2017
1 parent 6bf2042 commit fca831b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
18 changes: 7 additions & 11 deletions Nextras/YoutubeApi/Reader.php
Expand Up @@ -17,7 +17,7 @@ class Reader
{ {
use Nette\SmartObject; use Nette\SmartObject;
/** @var string */ /** @var string */
const FETCH_URL = 'https://www.googleapis.com/youtube/v3/videos?key=%s&part=snippet,contentDetails&id=%s'; private const FETCH_URL = 'https://www.googleapis.com/youtube/v3/videos?key=%s&part=snippet,contentDetails&id=%s';


/** @var string */ /** @var string */
private $apiKey; private $apiKey;
Expand All @@ -26,7 +26,7 @@ class Reader
private $httpClient; private $httpClient;




public function __construct($apiKey, Client $httpClient = null) public function __construct(string $apiKey, ?Client $httpClient = null)
{ {
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
if ($httpClient === null) { if ($httpClient === null) {
Expand All @@ -38,10 +38,8 @@ public function __construct($apiKey, Client $httpClient = null)


/** /**
* Fetches video data by youtube url * Fetches video data by youtube url
* @param string $videoUrl YouTube url
* @return Video
*/ */
public function getVideoByUrl($videoUrl) public function getVideoByUrl(string $videoUrl): Video
{ {
$url = new Nette\Http\Url($videoUrl); $url = new Nette\Http\Url($videoUrl);


Expand All @@ -59,17 +57,15 @@ public function getVideoByUrl($videoUrl)




/** /**
* Fetchs video data * Fetches video data
* @param string $videoId
* @return Video
*/ */
public function getVideo($videoId) public function getVideo(string $videoId): Video
{ {
return $this->parseData($this->getData($videoId), $videoId); return $this->parseData($this->getData($videoId), $videoId);
} }




protected function getData($videoId) protected function getData(string $videoId): string
{ {
$url = sprintf(self::FETCH_URL, $this->apiKey, $videoId); $url = sprintf(self::FETCH_URL, $this->apiKey, $videoId);
$response = $this->httpClient->get($url, [ $response = $this->httpClient->get($url, [
Expand All @@ -84,7 +80,7 @@ protected function getData($videoId)
} }




protected function parseData($data, $videoId) protected function parseData(string $data, string $videoId): Video
{ {
$data = Nette\Utils\Json::decode($data); $data = Nette\Utils\Json::decode($data);
if (!isset($data->items[0]->snippet) || !isset($data->items[0]->contentDetails)) { if (!isset($data->items[0]->snippet) || !isset($data->items[0]->contentDetails)) {
Expand Down
3 changes: 1 addition & 2 deletions Nextras/YoutubeApi/Video.php
Expand Up @@ -11,7 +11,6 @@
use Nette; use Nette;





class Video class Video
{ {
use Nette\SmartObject; use Nette\SmartObject;
Expand All @@ -30,7 +29,7 @@ class Video


/** @var array */ /** @var array */
public $thumbs; public $thumbs;

/** @var string */ /** @var string */
public $embed; public $embed;
} }

0 comments on commit fca831b

Please sign in to comment.