Skip to content

Commit

Permalink
Add type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
geneowak committed Dec 12, 2024
1 parent e35137a commit a98c86f
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 191 deletions.
39 changes: 14 additions & 25 deletions App/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,31 @@ class Downloader
{
/**
* Http resolver object
*
* @var Resolver
*/
private $client;
private readonly Resolver $client;

/**
* System object
*
* @var SystemController
*/
private $system;
private readonly \App\System\Controller $system;

/**
* Ubench lib
*
* @var Ubench
*/
private $bench;
private readonly Ubench $bench;

// [string => number[]]
private $filters = [];
private array $filters = [];

/**
* @var LaracastsController
*/
private $laracasts;
private readonly LaracastsController $laracasts;

/** @var bool Don't scrap pages and only get from existing cache */
private $cacheOnly = false;
private bool $cacheOnly = false;

/**
* Receives dependencies
*
* @param bool $retryDownload
*/
public function __construct(HttpClient $httpClient, Filesystem $system, Ubench $bench, $retryDownload = false)
public function __construct(HttpClient $httpClient, Filesystem $system, Ubench $bench)
{
$this->client = new Resolver($httpClient, $bench);
$this->system = new SystemController($system);
Expand All @@ -69,7 +58,7 @@ public function __construct(HttpClient $httpClient, Filesystem $system, Ubench $
/**
* All the logic
*/
public function start($options)
public function start(array $options): void
{
$counter = [
'series' => 1,
Expand All @@ -86,7 +75,7 @@ public function start($options)

$localSeries = $this->system->getSeries();

if (empty($this->filters)) {
if ($this->filters === []) {
$cachedData = $this->system->getCache();

$onlineSeries = $this->laracasts->getSeries($cachedData, $this->cacheOnly);
Expand Down Expand Up @@ -163,7 +152,7 @@ public function authenticate($email, $password)
/**
* Download Episodes
*/
public function downloadEpisodes($newEpisodes, &$counter, $newEpisodesCount)
public function downloadEpisodes($newEpisodes, array &$counter, $newEpisodesCount): void
{
$this->system->createFolderIfNotExists(SERIES_FOLDER);

Expand All @@ -190,7 +179,7 @@ public function downloadEpisodes($newEpisodes, &$counter, $newEpisodesCount)
}
}

protected function setFilters()
protected function setFilters(): bool
{
$shortOptions = 's:';
$shortOptions .= 'e:';
Expand Down Expand Up @@ -235,7 +224,7 @@ protected function setFilters()
return true;
}

private function setSeriesFilter($options)
private function setSeriesFilter($options): void
{
if (isset($options['s']) || isset($options['series-name'])) {
$series = $options['s'] ?? $options['series-name'];
Expand All @@ -247,13 +236,13 @@ private function setSeriesFilter($options)
$slugify = new Slugify;
$slugify->addRule("'", '');

$this->filters['series'] = array_map(fn ($serie) => $slugify->slugify($serie), $series);
$this->filters['series'] = array_map(fn ($serie): string => $slugify->slugify($serie), $series);

Utils::write(sprintf('Series names provided: %s', json_encode($this->filters['series'])));
}
}

private function setEpisodesFilter($options)
private function setEpisodesFilter($options): void
{
$this->filters['episodes'] = [];

Expand Down
28 changes: 11 additions & 17 deletions App/Html/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ class Parser
* Return list of topics data
*
* @param string $html
* @return array
*/
public static function getTopicsData($html)
public static function getTopicsData($html): array
{
$data = self::getData($html);

return array_map(fn ($topic) => [
return array_map(fn ($topic): array => [
'slug' => str_replace(LARACASTS_BASE_URL.'/topics/', '', $topic['path']),
'path' => $topic['path'],
'episode_count' => $topic['episode_count'],
'series_count' => $topic['series_count'],
], $data['props']['topics']);
}

public static function getSerieData($serieHtml)
public static function getSerieData($serieHtml): array
{
$data = self::getData($serieHtml);

Expand All @@ -42,27 +41,23 @@ public static function getSerieData($serieHtml)
* Return full list of series for given topic HTML page.
*
* @param string $html
* @return array
*/
public static function getSeriesDataFromTopic($html)
public static function getSeriesDataFromTopic($html): array
{
$data = self::getData($html);

$series = $data['props']['topic']['series'];

return array_combine(
array_column($series, 'slug'),
array_map(fn ($serie) => self::extractSerieData($serie), $series)
array_map(fn ($serie): array => self::extractSerieData($serie), $series)
);
}

/**
* Only extracts data we need for each serie and returns them
*
* @param array $serie
* @return array
*/
public static function extractSerieData($serie)
public static function extractSerieData(array $serie): array
{
return [
'slug' => $serie['slug'],
Expand All @@ -77,9 +72,8 @@ public static function extractSerieData($serie)
*
* @param string $episodeHtml
* @param number[] $filteredEpisodes
* @return array
*/
public static function getEpisodesData($episodeHtml, $filteredEpisodes = [])
public static function getEpisodesData($episodeHtml, $filteredEpisodes = []): array
{
$episodes = [];

Expand Down Expand Up @@ -117,7 +111,7 @@ public static function getEpisodeDownloadLink($episodeHtml)
return $data['props']['downloadLink'];
}

public static function extractLarabitsSeries($html)
public static function extractLarabitsSeries($html): array
{
$html = str_replace('\/', '/', html_entity_decode((string) $html));

Expand All @@ -126,14 +120,14 @@ public static function extractLarabitsSeries($html)
return array_unique($matches[1]);
}

public static function getCsrfToken($html)
public static function getCsrfToken($html): string
{
preg_match('/"csrfToken": \'([^\s]+)\'/', (string) $html, $matches);

return $matches[1];
}

public static function getUserData($html)
public static function getUserData($html): array
{

$data = self::getData($html);
Expand All @@ -153,7 +147,7 @@ public static function getUserData($html)
* @param string $html
* @return array
*/
private static function getData($html)
private static function getData($html): mixed
{
$parser = new Crawler($html);

Expand Down
41 changes: 12 additions & 29 deletions App/Http/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ class Resolver
{
/**
* Guzzle cookie
*
* @var CookieJar
*/
private $cookies;
private readonly CookieJar $cookies;

/**
* Receives dependencies
Expand All @@ -48,9 +46,8 @@ public function __construct(
*
* @param string $email
* @param string $password
* @return array
*/
public function login($email, $password)
public function login($email, $password): array
{
$token = $this->getCsrfToken();

Expand All @@ -77,10 +74,8 @@ public function login($email, $password)

/**
* Returns CSRF token
*
* @return string
*/
public function getCsrfToken()
public function getCsrfToken(): string
{
$this->client->get(LARACASTS_BASE_URL, [
'cookies' => $this->cookies,
Expand All @@ -93,7 +88,7 @@ public function getCsrfToken()
]);

$token = current(
array_filter($this->cookies->toArray(), fn ($cookie) => $cookie['Name'] === 'XSRF-TOKEN')
array_filter($this->cookies->toArray(), fn ($cookie): bool => $cookie['Name'] === 'XSRF-TOKEN')
);

return urldecode((string) $token['Value']);
Expand All @@ -102,11 +97,9 @@ public function getCsrfToken()
/**
* Download the episode of the serie.
*
* @param string $serieSlug
* @param array $episode
* @return bool
*/
public function downloadEpisode($serieSlug, $episode)
public function downloadEpisode(string $serieSlug, array $episode)
{
try {
$number = sprintf('%02d', $episode['number']);
Expand Down Expand Up @@ -139,12 +132,9 @@ public function downloadEpisode($serieSlug, $episode)
}

/**
* @param string $serieSlug
* @param string $number
* @param string $episodeName
* @return string
*/
private function getFilename($serieSlug, $number, $episodeName)
private function getFilename(string $serieSlug, string $number, $episodeName): string
{
return BASE_FOLDER
.DIRECTORY_SEPARATOR
Expand All @@ -160,10 +150,8 @@ private function getFilename($serieSlug, $number, $episodeName)

/**
* Returns topics page html
*
* @return string
*/
public function getTopicsHtml()
public function getTopicsHtml(): string
{
return $this->client
->get(LARACASTS_BASE_URL.'/'.LARACASTS_TOPICS_PATH, ['cookies' => $this->cookies, 'verify' => false])
Expand All @@ -175,9 +163,8 @@ public function getTopicsHtml()
* Returns html content of specific url
*
* @param string $url
* @return string
*/
public function getHtml($url)
public function getHtml($url): string
{
return $this->client
->get($url, ['cookies' => $this->cookies, 'verify' => false])
Expand All @@ -188,11 +175,10 @@ public function getHtml($url)
/**
* Get Laracasts download link for given episode
*
* @param string $serieSlug
* @param int $episodeNumber
* @return string
*/
private function getLaracastsLink($serieSlug, $episodeNumber)
private function getLaracastsLink(string $serieSlug, $episodeNumber)
{
$episodeHtml = $this->getHtml("series/$serieSlug/episodes/$episodeNumber");

Expand All @@ -205,7 +191,7 @@ private function getLaracastsLink($serieSlug, $episodeNumber)
*
* @return string
*/
private function getRedirectUrl($url)
private function getRedirectUrl($url): array
{
$response = $this->client->get($url, [
'cookies' => $this->cookies,
Expand All @@ -218,11 +204,8 @@ private function getRedirectUrl($url)

/**
* Helper to download the video.
*
*
* @return bool
*/
private function downloadVideo($downloadUrl, $saveTo)
private function downloadVideo($downloadUrl, string $saveTo): bool
{
$this->bench->start();

Expand Down Expand Up @@ -257,7 +240,7 @@ private function downloadVideo($downloadUrl, $saveTo)
/**
* @param string $url
*/
private function prepareDownloadLink($url)
private function prepareDownloadLink($url): array
{
$url = $this->getRedirectUrl($url);
$url = $this->getRedirectUrl($url);
Expand Down
15 changes: 4 additions & 11 deletions App/Laracasts/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public function __construct(private readonly Resolver $client) {}
*
* @param array $cachedData
* @param bool $cacheOnly
* @return array
*/
public function getSeries($cachedData, $cacheOnly = false)
public function getSeries($cachedData, $cacheOnly = false): array
{
$seriesCollection = new SeriesCollection($cachedData);

Expand Down Expand Up @@ -86,7 +85,7 @@ public function getSeries($cachedData, $cacheOnly = false)
return $seriesCollection->get();
}

public function getFilteredSeries($filters)
public function getFilteredSeries($filters): array
{
$seriesCollection = new SeriesCollection([]);

Expand All @@ -109,10 +108,8 @@ public function getFilteredSeries($filters)
* Determine is specific topic has been changed compared to cached data
*
* @param SeriesCollection $series
* @param array $topic
* @return bool
* */
public function isTopicUpdated($series, $topic)
public function isTopicUpdated($series, array $topic): bool
{
$series = $series->where('topic', $topic['slug']);

Expand All @@ -121,12 +118,8 @@ public function isTopicUpdated($series, $topic)

/**
* Determine is specific series has been changed compared to cached data
*
* @param SeriesCollection $series
* @param array $serie
* @return bool
*/
private function isSerieUpdated($series, $serie)
private function isSerieUpdated(SeriesCollection $series, array $serie): bool
{
$target = $series->where('slug', $serie['slug'])->first();

Expand Down
Loading

0 comments on commit a98c86f

Please sign in to comment.