diff --git a/src/Rule/VideoScan.php b/src/Rule/VideoScan.php index c335983..d61a3e7 100644 --- a/src/Rule/VideoScan.php +++ b/src/Rule/VideoScan.php @@ -14,6 +14,7 @@ class VideoScan extends BaseRule const FAILED_CONNECTION = -1; const FAILED_CAPTIONS = 0; const SUCCESSFUL_CAPTIONS = 1; + const NO_API_CREDITS = -2; public $youtube = null; public $vimeo = null; @@ -40,8 +41,13 @@ public function check() $captions = $this->getCaptionData($url, $provider); + if (self::NO_API_CREDITS === $captions) { + $this->setError("{$provider->getName()} videos cannot be scanned at this time. Please try again at a later time."); + continue; + } + if (self::FAILED_CONNECTION === $captions) { - $this->setError('Failed provider API connection.'); + $this->setError("Failed {$provider->getName()} API connection."); continue; } diff --git a/src/Video/Kaltura.php b/src/Video/Kaltura.php index 8a7c98a..73e24d2 100644 --- a/src/Video/Kaltura.php +++ b/src/Video/Kaltura.php @@ -15,6 +15,7 @@ class Kaltura { const KALTURA_FAILED_CONNECTION = -1; const KALTURA_FAIL = 0; const KALTURA_SUCCESS = 1; + const PROVIDER_NAME = 'Kaltura'; private $client; private $language; @@ -127,4 +128,8 @@ function getVideoData($link_url) return !isset($result->objects) ? $result->objects : self::KALTURA_FAILED_CONNECTION; } + public function getName() { + return self::PROVIDER_NAME; + } + } \ No newline at end of file diff --git a/src/Video/Vimeo.php b/src/Video/Vimeo.php index 6e4a6b0..cf90786 100644 --- a/src/Video/Vimeo.php +++ b/src/Video/Vimeo.php @@ -9,6 +9,7 @@ class Vimeo const VIMEO_FAILED_CONNECTION = -1; const VIMEO_FAIL = 0; const VIMEO_SUCCESS = 1; + const PROVIDER_NAME = "Vimeo"; private $regex = '@vimeo\.com/[^0-9]*([0-9]{7,9})@i'; private $search_url = 'https://api.vimeo.com/videos/'; @@ -94,4 +95,8 @@ function getVideoData($link_url) return isset($result->data) ? $result->data : self::VIMEO_FAILED_CONNECTION; } + + public function getName() { + return self::PROVIDER_NAME; + } } diff --git a/src/Video/Youtube.php b/src/Video/Youtube.php index 6cfc7b1..dfa5ec7 100644 --- a/src/Video/Youtube.php +++ b/src/Video/Youtube.php @@ -9,6 +9,8 @@ class Youtube const YOUTUBE_FAILED_REQUEST = -1; const YOUTUBE_FAIL = 0; const YOUTUBE_SUCCESS = 1; + const YOUTUBE_NO_CREDITS = -2; + const PROVIDER_NAME = 'Youtube'; private $regex = array( '@youtube\.com/embed/([^"\&\? ]+)@i', @@ -127,17 +129,26 @@ function getVideoData($link_url) $url = $this->search_url . $youtube_id . '&key=' . $this->api_key; $response = $this->client->request('GET', $url); + $result = json_decode($response->getBody()); + // validate response if ($response->getStatusCode() >= 400) { + foreach ($result->error->errors as $error) { + if ($error->reason === "quotaExceeded") { + return self::YOUTUBE_NO_CREDITS; + } + } return self::YOUTUBE_FAILED_REQUEST; } - $result = json_decode($response->getBody()); - if (!empty($result->error)) { return self::YOUTUBE_FAILED_REQUEST; } return isset($result->items) ? $result->items : self::YOUTUBE_FAILED_REQUEST; } + + public function getName() { + return self::PROVIDER_NAME; + } } diff --git a/tests/VideoScanTest.php b/tests/VideoScanTest.php index 0295885..955a7d9 100644 --- a/tests/VideoScanTest.php +++ b/tests/VideoScanTest.php @@ -184,4 +184,30 @@ public function testCheckInvalidKaltura() $this->assertEquals(0, $ruleMock->check(), 'No issues when API connection fails.'); $this->assertCount(1, $ruleMock->getErrors(), 'One error found when API connection fails.'); } + + public function testCheckNoApiCreditsYoutube() + { + $html = ''; + $dom = new \DOMDocument('1.0', 'utf-8'); + $dom->loadHTML($html); + $options = [ + 'vimeoApiKey' => 'test', + 'youtubeApiKey' => 'test', + 'kalturaApiKey' => 'test', + 'kalturaUsername' => 'test' + ]; + $response = VideoScan::NO_API_CREDITS; + + $ruleMock = $this->getMockBuilder(VideoScan::class) + ->setConstructorArgs([$dom, $options]) + ->onlyMethods(array('getCaptionData')) + ->getMock(); + + $ruleMock->expects($this->once()) + ->method('getCaptionData') + ->will($this->returnValue($response)); + + $this->assertEquals(0, $ruleMock->check(), 'No issues when credits run out.'); + $this->assertCount(1, $ruleMock->getErrors(), 'One error found when credits run out.'); + } } \ No newline at end of file diff --git a/tests/YoutubeTest.php b/tests/YoutubeTest.php index 104da6b..43a808c 100644 --- a/tests/YoutubeTest.php +++ b/tests/YoutubeTest.php @@ -163,4 +163,12 @@ public function testCaptionsAutoGeneratedSuccess(){ $this->assertEquals($youtube->captionsAutoGenerated($response), YouTube::YOUTUBE_SUCCESS); } + // public function testNoApiCredits(){ + // $client = new \GuzzleHttp\Client(['http_errors' => false]); + // $youtube = new Youtube($client, 'en', 'AIzaSyChmQQw6pZ1H2j4bqid4Ael46mYaNSy7Jg'); + // $url = "https://www.youtube.com/watch?v=WTGn88ZL-pg"; + + // $this->assertEquals($youtube->getVideoData($url), YouTube::YOUTUBE_NO_CREDITS); + // } + } \ No newline at end of file