Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
ianfortier authored and github-actions[bot] committed Oct 12, 2024
1 parent 015eb6b commit caded5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
25 changes: 15 additions & 10 deletions src/LoomDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@
class LoomDownloader
{
private $client;

private $outputDirectory;

public function __construct(string $outputDirectory = null)
public function __construct(?string $outputDirectory = null)
{
$this->client = new Client();
$this->client = new Client;
$this->outputDirectory = $outputDirectory ?? sys_get_temp_dir();
}

public function downloadVideo($url)
{
$id = $this->extractId($url);
$downloadUrl = $this->fetchLoomDownloadUrl($id);

return $this->fetchVideoContent($downloadUrl);
}

public function saveVideo($url, $destination = null)
{
$id = $this->extractId($url);
$downloadUrl = $this->fetchLoomDownloadUrl($id);

if ($destination === null) {
$destination = $this->outputDirectory . "/{$id}.mp4";
$destination = $this->outputDirectory."/{$id}.mp4";
}

$this->ensureDirectoryExists(dirname($destination));
Expand All @@ -43,43 +44,47 @@ protected function fetchLoomDownloadUrl($id)
try {
$response = $this->client->post("https://www.loom.com/api/campaigns/sessions/{$id}/transcoded-url");
$body = json_decode($response->getBody(), true);

return $body['url'];
} catch (GuzzleException $e) {
throw new \Exception("Failed to fetch Loom download URL: " . $e->getMessage());
throw new \Exception('Failed to fetch Loom download URL: '.$e->getMessage());
}
}

protected function fetchVideoContent($url)
{
try {
$response = $this->client->get($url);

return $response->getBody()->getContents();
} catch (GuzzleException $e) {
throw new \Exception("Failed to download video: " . $e->getMessage());
throw new \Exception('Failed to download video: '.$e->getMessage());
}
}

protected function saveVideoToFile($url, $destination)
{
try {
$this->client->get($url, ['sink' => $destination]);

return $destination;
} catch (GuzzleException $e) {
throw new \Exception("Failed to save video: " . $e->getMessage());
throw new \Exception('Failed to save video: '.$e->getMessage());
}
}

protected function extractId($url)
{
$url = explode('?', $url)[0];
$parts = explode('/', $url);

return end($parts);
}

protected function ensureDirectoryExists($directory)
{
if (!is_dir($directory)) {
if (! is_dir($directory)) {
mkdir($directory, 0777, true);
}
}
}
}
16 changes: 8 additions & 8 deletions tests/LoomDownloaderTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

use LoomDownloader\LoomDownloader;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use LoomDownloader\LoomDownloader;

beforeEach(function () {
$this->mockHandler = new MockHandler();
$this->mockHandler = new MockHandler;
$handlerStack = HandlerStack::create($this->mockHandler);
$this->mockClient = new Client(['handler' => $handlerStack]);
});
Expand All @@ -19,7 +19,7 @@
new Response(200, [], 'fake video content')
);

$downloader = new LoomDownloader();
$downloader = new LoomDownloader;
$reflector = new ReflectionClass($downloader);
$clientProperty = $reflector->getProperty('client');
$clientProperty->setAccessible(true);
Expand All @@ -36,7 +36,7 @@
new Response(200, [], 'fake video content')
);

$downloader = new LoomDownloader();
$downloader = new LoomDownloader;
$reflector = new ReflectionClass($downloader);
$clientProperty = $reflector->getProperty('client');
$clientProperty->setAccessible(true);
Expand All @@ -56,7 +56,7 @@
new RequestException('Error Communicating with Server', new \GuzzleHttp\Psr7\Request('GET', 'test'))
);

$downloader = new LoomDownloader();
$downloader = new LoomDownloader;
$reflector = new ReflectionClass($downloader);
$clientProperty = $reflector->getProperty('client');
$clientProperty->setAccessible(true);
Expand All @@ -71,11 +71,11 @@
new RequestException('Error Downloading Video', new \GuzzleHttp\Psr7\Request('GET', 'test'))
);

$downloader = new LoomDownloader();
$downloader = new LoomDownloader;
$reflector = new ReflectionClass($downloader);
$clientProperty = $reflector->getProperty('client');
$clientProperty->setAccessible(true);
$clientProperty->setValue($downloader, $this->mockClient);

$downloader->downloadVideo('https://www.loom.com/share/123abc');
})->throws(\Exception::class, 'Failed to download video');
})->throws(\Exception::class, 'Failed to download video');

0 comments on commit caded5e

Please sign in to comment.