88class  LoomDownloader
99{
1010    private  $ client ;
11+ 
1112    private  $ outputDirectory ;
1213
13-     public  function  __construct (string  $ outputDirectory  = null )
14+     public  function  __construct (? string  $ outputDirectory  = null )
1415    {
15-         $ this  ->client  = new  Client () ;
16+         $ this  ->client  = new  Client ;
1617        $ this  ->outputDirectory  = $ outputDirectory  ?? sys_get_temp_dir ();
1718    }
1819
1920    public  function  downloadVideo ($ url )
2021    {
2122        $ id  = $ this  ->extractId ($ url );
2223        $ downloadUrl  = $ this  ->fetchLoomDownloadUrl ($ id );
23-          
24+ 
2425        return  $ this  ->fetchVideoContent ($ downloadUrl );
2526    }
2627
2728    public  function  saveVideo ($ url , $ destination  = null )
2829    {
2930        $ id  = $ this  ->extractId ($ url );
3031        $ downloadUrl  = $ this  ->fetchLoomDownloadUrl ($ id );
31-          
32+ 
3233        if  ($ destination  === null ) {
33-             $ destination  = $ this  ->outputDirectory  .  "/ {$ id }.mp4 " ;
34+             $ destination  = $ this  ->outputDirectory . "/ {$ id }.mp4 " ;
3435        }
3536
3637        $ this  ->ensureDirectoryExists (dirname ($ destination ));
@@ -43,43 +44,47 @@ protected function fetchLoomDownloadUrl($id)
4344        try  {
4445            $ response  = $ this  ->client ->post ("https://www.loom.com/api/campaigns/sessions/ {$ id }/transcoded-url " );
4546            $ body  = json_decode ($ response ->getBody (), true );
47+ 
4648            return  $ body ['url ' ];
4749        } catch  (GuzzleException   $ e ) {
48-             throw  new  \Exception (" Failed to fetch Loom download URL:  "  .  $ e ->getMessage ());
50+             throw  new  \Exception (' Failed to fetch Loom download URL:  ' . $ e ->getMessage ());
4951        }
5052    }
5153
5254    protected  function  fetchVideoContent ($ url )
5355    {
5456        try  {
5557            $ response  = $ this  ->client ->get ($ url );
58+ 
5659            return  $ response ->getBody ()->getContents ();
5760        } catch  (GuzzleException   $ e ) {
58-             throw  new  \Exception (" Failed to download video:  "  .  $ e ->getMessage ());
61+             throw  new  \Exception (' Failed to download video:  ' . $ e ->getMessage ());
5962        }
6063    }
6164
6265    protected  function  saveVideoToFile ($ url , $ destination )
6366    {
6467        try  {
6568            $ this  ->client ->get ($ url , ['sink '  => $ destination ]);
69+ 
6670            return  $ destination ;
6771        } catch  (GuzzleException   $ e ) {
68-             throw  new  \Exception (" Failed to save video:  "  .  $ e ->getMessage ());
72+             throw  new  \Exception (' Failed to save video:  ' . $ e ->getMessage ());
6973        }
7074    }
7175
7276    protected  function  extractId ($ url )
7377    {
7478        $ url  = explode ('? ' , $ url )[0 ];
7579        $ parts  = explode ('/ ' , $ url );
80+ 
7681        return  end ($ parts );
7782    }
7883
7984    protected  function  ensureDirectoryExists ($ directory )
8085    {
81-         if  (!is_dir ($ directory )) {
86+         if  (!  is_dir ($ directory )) {
8287            mkdir ($ directory , 0777 , true );
8388        }
8489    }
85- }
90+ }
0 commit comments