-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed download after major changes at laracasts
- Loading branch information
Sergio Torrella
committed
Nov 9, 2018
1 parent
9f64e3a
commit 7835edb
Showing
11 changed files
with
234 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Algolia Controller | ||
*/ | ||
namespace App\Algolia; | ||
|
||
use AlgoliaSearch\Client; | ||
use App\Downloader; | ||
use App\Exceptions\AlgoliaException; | ||
|
||
/** | ||
* Class Controller | ||
* @package App\Algolia | ||
*/ | ||
class Controller | ||
{ | ||
/** | ||
* Client lib | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* Receives dependencies | ||
* | ||
* @param Client $client | ||
*/ | ||
public function __construct(Client $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* Grabs all lessons & series from the algolia api. | ||
* | ||
* @return array | ||
* @throws \AlgoliaSearch\AlgoliaException | ||
*/ | ||
public function getAllLessons() | ||
{ | ||
$index = $this->client->initIndex(ALGOLIA_INDEX_NAME); | ||
|
||
$page = 0; | ||
$params = [ | ||
'facetFilters' => [ | ||
[ | ||
'type:lesson', | ||
'type:series', | ||
], | ||
], | ||
'attributesToRetrieve' => [ | ||
'path', | ||
'type', | ||
'slug', | ||
'episode_count', | ||
], | ||
'page' => $page, | ||
]; | ||
|
||
$array = [ | ||
'lessons' => [], | ||
'series' => [], | ||
]; | ||
|
||
do { | ||
try { | ||
$res = $index->search('', $params); | ||
} catch (\Exception $e) { | ||
throw new AlgoliaException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
$params['page'] = $res['page'] + 1; | ||
|
||
foreach ($res['hits'] as $lessonInfo) { | ||
switch ($lessonInfo['type']) { | ||
case 'lesson': | ||
$path = $lessonInfo['path']; | ||
if (preg_match('/'.LARACASTS_LESSONS_PATH.'\/(.+)/', $path, $matches)) { // lesson | ||
$array['lessons'][] = $matches[1]; | ||
} | ||
break; | ||
case 'series': | ||
$serieSlug = $lessonInfo['slug']; | ||
foreach (range(1, $lessonInfo['episode_count']) as $episode) { | ||
$array['series'][$serieSlug][] = $episode; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
} while ($res['page'] <= $res['nbPages']); | ||
|
||
Downloader::$currentLessonNumber = count($array['lessons']); | ||
|
||
return $array; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Algolia Exception | ||
*/ | ||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
/** | ||
* Class AlgoliaException | ||
* @package App\Exceptions | ||
*/ | ||
class AlgoliaException extends Exception | ||
{ | ||
/** | ||
* AlgoliaException constructor. | ||
* | ||
* @param string $message | ||
* @param int $code | ||
* @param Throwable|null $previous | ||
*/ | ||
public function __construct(string $message = "", int $code = 0, Throwable $previous = null) | ||
{ | ||
parent::__construct('Algolia error: ' . $message, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.