diff --git a/app/Caches/ChapterCounter.php b/app/Caches/ChapterCounter.php new file mode 100644 index 00000000..c3f840c9 --- /dev/null +++ b/app/Caches/ChapterCounter.php @@ -0,0 +1,37 @@ +lifetime; + } + + public function getKey($id = null) + { + return "chapter_counter:{$id}"; + } + + public function getContent($id = null) + { + $chapterRepo = new ChapterRepo(); + + $chapter = $chapterRepo->findById($id); + + return [ + 'user_count' => $chapter->user_count, + 'lesson_count' => $chapter->lesson_count, + 'comment_count' => $chapter->comment_count, + 'agree_count' => $chapter->agree_count, + 'oppose_count' => $chapter->oppose_count, + ]; + } + +} diff --git a/app/Caches/CourseCounter.php b/app/Caches/CourseCounter.php new file mode 100644 index 00000000..bed56542 --- /dev/null +++ b/app/Caches/CourseCounter.php @@ -0,0 +1,38 @@ +lifetime; + } + + public function getKey($id = null) + { + return "course_counter:{$id}"; + } + + public function getContent($id = null) + { + $courseRepo = new CourseRepo(); + + $course = $courseRepo->findById($id); + + return [ + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, + 'comment_count' => $course->comment_count, + 'consult_count' => $course->consult_count, + 'review_count' => $course->review_count, + 'favorite_count' => $course->favorite_count, + ]; + } + +} diff --git a/app/Caches/CoursePackageList.php b/app/Caches/CoursePackageList.php index 2767cca2..2f59296b 100644 --- a/app/Caches/CoursePackageList.php +++ b/app/Caches/CoursePackageList.php @@ -5,6 +5,7 @@ namespace App\Caches; use App\Models\Package as PackageModel; use App\Repos\Course as CourseRepo; use App\Repos\Package as PackageRepo; +use Phalcon\Mvc\Model\Resultset; class CoursePackageList extends Cache { @@ -27,19 +28,19 @@ class CoursePackageList extends Cache $packages = $courseRepo->findPackages($id); - if ($packages->count() == 0) { - return []; - } - return $this->handleContent($packages); } /** - * @param PackageModel[] $packages + * @param Resultset|PackageModel[] $packages * @return array */ protected function handleContent($packages) { + if ($packages->count() == 0) { + return []; + } + $result = []; foreach ($packages as $package) { @@ -64,6 +65,10 @@ class CoursePackageList extends Cache $courses = $packageRepo->findCourses($packageId); + if ($courses->count() == 0) { + return []; + } + $result = []; $baseUrl = kg_ci_base_url(); diff --git a/app/Console/Tasks/RebuildChapterCacheTask.php b/app/Console/Tasks/RebuildChapterCacheTask.php index a1763e33..4f648df0 100644 --- a/app/Console/Tasks/RebuildChapterCacheTask.php +++ b/app/Console/Tasks/RebuildChapterCacheTask.php @@ -7,7 +7,6 @@ use App\Caches\ChapterCounter as ChapterCounterCache; use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\Chapter as ChapterRepo; use App\Services\ChapterCacheSyncer; -use Phalcon\Mvc\Model\Resultset; class RebuildChapterCacheTask extends Task { @@ -41,9 +40,6 @@ class RebuildChapterCacheTask extends Task $chapterRepo = new ChapterRepo(); - /** - * @var Resultset $chapters - */ $chapters = $chapterRepo->findByIds($chapterIds); if ($chapters->count() == 0) { @@ -51,24 +47,32 @@ class RebuildChapterCacheTask extends Task } $chapterCache = new ChapterCache(); + $counterCache = new ChapterCounterCache(); foreach ($chapters as $chapter) { + $chapter->user_count = $chapterRepo->countUsers($chapter->id); + $chapter->lesson_count = $chapterRepo->countLessons($chapter->id); $chapter->comment_count = $chapterRepo->countComments($chapter->id); - $chapter->like_count = $chapterRepo->countLikes($chapter->id); + $chapter->agree_count = $chapterRepo->countAgrees($chapter->id); + $chapter->oppose_count = $chapterRepo->countOpposes($chapter->id); + $chapter->update(); $chapterCache->rebuild($chapter->id); + $counterCache->rebuild($chapter->id); } + + $this->redis->sRem($key, ...$chapterIds); } protected function getCacheKey() { $syncer = new ChapterCacheSyncer(); - return $syncer->getCacheKey(); + return $syncer->getSyncKey(); } } diff --git a/app/Console/Tasks/RebuildCourseCacheTask.php b/app/Console/Tasks/RebuildCourseCacheTask.php index cb55d9a7..f78a3c8f 100644 --- a/app/Console/Tasks/RebuildCourseCacheTask.php +++ b/app/Console/Tasks/RebuildCourseCacheTask.php @@ -5,10 +5,8 @@ namespace App\Console\Tasks; use App\Caches\Course as CourseCache; use App\Caches\CourseCounter as CourseCounterCache; use App\Library\Cache\Backend\Redis as RedisCache; -use App\Models\Course as CourseModel; use App\Repos\Course as CourseRepo; use App\Services\CourseCacheSyncer; -use Phalcon\Mvc\Model\Resultset; class RebuildCourseCacheTask extends Task { @@ -42,9 +40,6 @@ class RebuildCourseCacheTask extends Task $courseRepo = new CourseRepo(); - /** - * @var Resultset|CourseModel[] $courses - */ $courses = $courseRepo->findByIds($courseIds); if ($courses->count() == 0) { @@ -52,18 +47,22 @@ class RebuildCourseCacheTask extends Task } $courseCache = new CourseCache(); + $counterCache = new CourseCounterCache(); foreach ($courses as $course) { $course->user_count = $courseRepo->countUsers($course->id); + $course->lesson_count = $courseRepo->countLessons($course->id); $course->comment_count = $courseRepo->countComments($course->id); + $course->consult_count = $courseRepo->countConsults($course->id); $course->review_count = $courseRepo->countReviews($course->id); $course->favorite_count = $courseRepo->countFavorites($course->id); $course->update(); $courseCache->rebuild($course->id); + $counterCache->rebuild($course->id); } @@ -74,7 +73,7 @@ class RebuildCourseCacheTask extends Task { $syncer = new CourseCacheSyncer(); - return $syncer->getCacheKey(); + return $syncer->getSyncKey(); } } diff --git a/app/Http/Admin/Services/Category.php b/app/Http/Admin/Services/Category.php index 7e2bf2e5..faae26c5 100644 --- a/app/Http/Admin/Services/Category.php +++ b/app/Http/Admin/Services/Category.php @@ -2,6 +2,9 @@ namespace App\Http\Admin\Services; +use App\Caches\Category as CategoryCache; +use App\Caches\CategoryList as CategoryListCache; +use App\Caches\CategoryTreeList as CategoryTreeListCache; use App\Models\Category as CategoryModel; use App\Repos\Category as CategoryRepo; use App\Validators\Category as CategoryValidator; @@ -86,6 +89,7 @@ class Category extends Service $category->update(); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; } @@ -122,6 +126,7 @@ class Category extends Service $category->update($data); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; } @@ -139,6 +144,7 @@ class Category extends Service $category->update(); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; } @@ -152,6 +158,7 @@ class Category extends Service $category->update(); $this->updateCategoryStats($category); + $this->rebuildCategoryCache($category); return $category; } @@ -169,6 +176,21 @@ class Category extends Service $category->update(); } + protected function rebuildCategoryCache(CategoryModel $category) + { + $itemCache = new CategoryCache(); + + $itemCache->rebuild($category->id); + + $listCache = new CategoryListCache(); + + $listCache->rebuild(); + + $treeListCache = new CategoryTreeListCache(); + + $treeListCache->rebuild(); + } + protected function enableChildCategories($parentId) { $categoryRepo = new CategoryRepo(); diff --git a/app/Http/Admin/Services/Chapter.php b/app/Http/Admin/Services/Chapter.php index af1151d7..98cd2406 100644 --- a/app/Http/Admin/Services/Chapter.php +++ b/app/Http/Admin/Services/Chapter.php @@ -2,6 +2,7 @@ namespace App\Http\Admin\Services; +use App\Caches\CourseChapterList as CourseChapterListCache; use App\Models\Chapter as ChapterModel; use App\Models\Course as CourseModel; use App\Repos\Chapter as ChapterRepo; @@ -59,6 +60,7 @@ class Chapter extends Service $this->updateChapterStats($chapter); $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; } @@ -100,6 +102,7 @@ class Chapter extends Service $this->updateChapterStats($chapter); $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; } @@ -118,6 +121,7 @@ class Chapter extends Service $this->updateChapterStats($chapter); $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; } @@ -132,6 +136,7 @@ class Chapter extends Service $this->updateChapterStats($chapter); $this->updateCourseStats($chapter); + $this->rebuildChapterCache($chapter); return $chapter; } @@ -168,6 +173,13 @@ class Chapter extends Service } } + protected function rebuildChapterCache(ChapterModel $chapter) + { + $cache = new CourseChapterListCache(); + + $cache->rebuild($chapter->course_id); + } + protected function findOrFail($id) { $validator = new ChapterValidator(); diff --git a/app/Http/Admin/Services/Course.php b/app/Http/Admin/Services/Course.php index d955024f..12b7c970 100644 --- a/app/Http/Admin/Services/Course.php +++ b/app/Http/Admin/Services/Course.php @@ -3,6 +3,9 @@ namespace App\Http\Admin\Services; use App\Builders\CourseList as CourseListBuilder; +use App\Caches\CourseCategoryList as CourseCategoryListCache; +use App\Caches\CourseRelatedList as CourseRelatedListCache; +use App\Caches\CourseTeacherList as CourseTeacherListCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\Course as CourseModel; use App\Models\CourseCategory as CourseCategoryModel; @@ -348,6 +351,10 @@ class Course extends Service } } } + + $cache = new CourseTeacherListCache(); + + $cache->rebuild(); } protected function saveCategories(CourseModel $course, $categoryIds) @@ -388,6 +395,17 @@ class Course extends Service } } } + + $categoryId = $newCategoryIds[0] ?? 0; + + if ($categoryId) { + $course->category_id = $categoryId; + $course->update(); + } + + $cache = new CourseCategoryListCache(); + + $cache->rebuild($course->id); } protected function saveRelatedCourses(CourseModel $course, $courseIds) @@ -449,6 +467,10 @@ class Course extends Service } } } + + $cache = new CourseRelatedListCache(); + + $cache->rebuild($course->id); } protected function handleCourses($pager) diff --git a/app/Http/Admin/Services/Nav.php b/app/Http/Admin/Services/Nav.php index b0a0d36d..bfe3634a 100644 --- a/app/Http/Admin/Services/Nav.php +++ b/app/Http/Admin/Services/Nav.php @@ -92,6 +92,8 @@ class Nav extends Service $this->updateNavStats($nav); + $this->rebuildNavCache(); + return $nav; } @@ -140,6 +142,8 @@ class Nav extends Service $this->updateNavStats($nav); + $this->rebuildNavCache(); + return $nav; } @@ -157,6 +161,8 @@ class Nav extends Service $this->updateNavStats($nav); + $this->rebuildNavCache(); + return $nav; } @@ -170,6 +176,8 @@ class Nav extends Service $this->updateNavStats($nav); + $this->rebuildNavCache(); + return $nav; } @@ -182,10 +190,16 @@ class Nav extends Service } $childCount = $navRepo->countChildNavs($nav->id); - $nav->child_count = $childCount; - $nav->update(); + $nav->child_count = $childCount; + + $nav->update(); + } + + protected function rebuildNavCache() + { $cache = new NavTreeListCache(); + $cache->rebuild(); } diff --git a/app/Http/Admin/Services/Slide.php b/app/Http/Admin/Services/Slide.php index 1708a369..afa43bed 100644 --- a/app/Http/Admin/Services/Slide.php +++ b/app/Http/Admin/Services/Slide.php @@ -2,6 +2,7 @@ namespace App\Http\Admin\Services; +use App\Caches\SlideList as SlideListCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\Slide as SlideModel; use App\Repos\Slide as SlideRepo; @@ -59,6 +60,8 @@ class Slide extends Service $slide->create($data); + $this->rebuildSlideCache(); + return $slide; } @@ -106,6 +109,8 @@ class Slide extends Service $slide->update($data); + $this->rebuildSlideCache(); + return $slide; } @@ -117,6 +122,8 @@ class Slide extends Service $slide->update(); + $this->rebuildSlideCache(); + return $slide; } @@ -128,9 +135,18 @@ class Slide extends Service $slide->update(); + $this->rebuildSlideCache(); + return $slide; } + protected function rebuildSlideCache() + { + $cache = new SlideListCache(); + + $cache->rebuild(); + } + protected function findOrFail($id) { $validator = new SlideValidator(); diff --git a/app/Library/Helper.php b/app/Library/Helper.php index e3694a5c..69be136a 100644 --- a/app/Library/Helper.php +++ b/app/Library/Helper.php @@ -53,7 +53,7 @@ function kg_uniqid($prefix = '', $more = false) */ function kg_json_encode($data) { - $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; + $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION; return json_encode($data, $options); } diff --git a/app/Listeners/ChapterCounter.php b/app/Listeners/ChapterCounter.php new file mode 100644 index 00000000..f9b91d9f --- /dev/null +++ b/app/Listeners/ChapterCounter.php @@ -0,0 +1,69 @@ +counter = new CacheChapterCounter(); + } + + public function incrUserCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hIncrBy($chapter->id, 'user_count'); + } + + public function decrUserCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hDecrBy($chapter->id, 'user_count'); + } + + public function incrCommentCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hIncrBy($chapter->id, 'comment_count'); + } + + public function decrCommentCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hDecrBy($chapter->id, 'comment_count'); + } + + public function incrAgreeCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hIncrBy($chapter->id, 'agree_count'); + } + + public function decrAgreeCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hDecrBy($chapter->id, 'agree_count'); + } + + public function incrOpposeCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hIncrBy($chapter->id, 'oppose_count'); + } + + public function decrOpposeCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hDecrBy($chapter->id, 'oppose_count'); + } + + public function incrLessonCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hIncrBy($chapter->id, 'lesson_count'); + } + + public function decrLessonCount(Event $event, $source, ChapterModel $chapter) + { + $this->counter->hDecrBy($chapter->id, 'lesson_count'); + } + +} \ No newline at end of file diff --git a/app/Listeners/CourseCounter.php b/app/Listeners/CourseCounter.php new file mode 100644 index 00000000..fd44517d --- /dev/null +++ b/app/Listeners/CourseCounter.php @@ -0,0 +1,79 @@ +counter = new CacheCourseCounter(); + } + + public function incrUserCount(Event $event, $source, CourseModel $course) + { + $this->counter->hIncrBy($course->id, 'user_count'); + } + + public function decrUserCount(Event $event, $source, CourseModel $course) + { + $this->counter->hDecrBy($course->id, 'user_count'); + } + + public function incrCommentCount(Event $event, $source, CourseModel $course) + { + $this->counter->hIncrBy($course->id, 'comment_count'); + } + + public function decrCommentCount(Event $event, $source, CourseModel $course) + { + $this->counter->hDecrBy($course->id, 'comment_count'); + } + + public function incrConsultCount(Event $event, $source, CourseModel $course) + { + $this->counter->hIncrBy($course->id, 'consult_count'); + } + + public function decrConsultCount(Event $event, $source, CourseModel $course) + { + $this->counter->hDecrBy($course->id, 'consult_count'); + } + + public function incrReviewCount(Event $event, $source, CourseModel $course) + { + $this->counter->hIncrBy($course->id, 'review_count'); + } + + public function decrReviewCount(Event $event, $source, CourseModel $course) + { + $this->counter->hDecrBy($course->id, 'review_count'); + } + + public function incrFavoriteCount(Event $event, $source, CourseModel $course) + { + $this->counter->hIncrBy($course->id, 'favorite_count'); + } + + public function decrFavoriteCount(Event $event, $source, CourseModel $course) + { + $this->counter->hDecrBy($course->id, 'favorite_count'); + } + + public function incrLessonCount(Event $event, $source, CourseModel $course) + { + $this->counter->hIncrBy($course->id, 'lesson_count'); + } + + public function decrLessonCount(Event $event, $source, CourseModel $course) + { + $this->counter->hDecrBy($course->id, 'lesson_count'); + } + +} \ No newline at end of file diff --git a/app/Models/Category.php b/app/Models/Category.php index 69a37811..d994ab83 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -2,9 +2,6 @@ namespace App\Models; -use App\Caches\Category as CategoryCache; -use App\Caches\CategoryList as CategoryListCache; -use App\Caches\CategoryTreeList as CategoryTreeListCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Category extends Model @@ -121,29 +118,4 @@ class Category extends Model $this->update_time = time(); } - public function afterCreate() - { - $this->rebuildCache(); - } - - public function afterUpdate() - { - $this->rebuildCache(); - } - - public function rebuildCache() - { - $itemCache = new CategoryCache(); - - $itemCache->rebuild($this->id); - - $listCache = new CategoryListCache(); - - $listCache->rebuild(); - - $treeListCache = new CategoryTreeListCache(); - - $treeListCache->rebuild(); - } - } diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index 01f5560e..79401f5a 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -218,7 +218,7 @@ class Chapter extends Model { $this->update_time = time(); - if (!empty($this->attrs)) { + if (is_array($this->attrs) && !empty($this->attrs)) { $this->attrs = kg_json_encode($this->attrs); } } diff --git a/app/Models/ChapterVod.php b/app/Models/ChapterVod.php index e3bbf660..12fc5d4b 100644 --- a/app/Models/ChapterVod.php +++ b/app/Models/ChapterVod.php @@ -65,7 +65,7 @@ class ChapterVod extends Model { $this->create_time = time(); - if (!empty($this->file_transcode)) { + if (is_array($this->file_transcode) && !empty($this->file_transcode)) { $this->file_transcode = kg_json_encode($this->file_transcode); } } @@ -74,7 +74,7 @@ class ChapterVod extends Model { $this->update_time = time(); - if (!empty($this->file_transcode)) { + if (is_array($this->file_transcode) && !empty($this->file_transcode)) { $this->file_transcode = kg_json_encode($this->file_transcode); } } diff --git a/app/Models/Course.php b/app/Models/Course.php index 92515895..cf1252a0 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -270,7 +270,7 @@ class Course extends Model { $this->update_time = time(); - if (!empty($this->attrs)) { + if (is_array($this->attrs) && !empty($this->attrs)) { $this->attrs = kg_json_encode($this->attrs); } } diff --git a/app/Models/Nav.php b/app/Models/Nav.php index ea6d0e2b..dbe9bd6c 100644 --- a/app/Models/Nav.php +++ b/app/Models/Nav.php @@ -2,7 +2,6 @@ namespace App\Models; -use App\Caches\NavTreeList as NavTreeListCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Nav extends Model @@ -145,23 +144,6 @@ class Nav extends Model $this->update_time = time(); } - public function afterCreate() - { - $this->rebuildCache(); - } - - public function afterUpdate() - { - $this->rebuildCache(); - } - - public function rebuildCache() - { - $cache = new NavTreeListCache(); - - $cache->rebuild(); - } - public static function positionTypes() { return [ diff --git a/app/Models/Order.php b/app/Models/Order.php index 7b657a3e..504e268c 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -152,7 +152,7 @@ class Order extends Model $this->sn = date('YmdHis') . rand(1000, 9999); $this->create_time = time(); - if (!empty($this->item_info)) { + if (is_array($this->item_info) && !empty($this->item_info)) { $this->item_info = kg_json_encode($this->item_info); } else { $this->item_info = ''; @@ -163,7 +163,7 @@ class Order extends Model { $this->update_time = time(); - if (!empty($this->item_info)) { + if (is_array($this->item_info) && !empty($this->item_info)) { $this->item_info = kg_json_encode($this->item_info); } } diff --git a/app/Models/Slide.php b/app/Models/Slide.php index 6abd7054..8260dd9f 100644 --- a/app/Models/Slide.php +++ b/app/Models/Slide.php @@ -2,7 +2,6 @@ namespace App\Models; -use App\Caches\SlideList as SlideListCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Slide extends Model @@ -119,23 +118,6 @@ class Slide extends Model $this->update_time = time(); } - public function afterCreate() - { - $this->rebuildCache(); - } - - public function afterUpdate() - { - $this->rebuildCache(); - } - - public function rebuildCache() - { - $cache = new SlideListCache(); - - $cache->rebuild(); - } - public static function targetTypes() { return [ diff --git a/app/Services/ChapterCacheSyncer.php b/app/Services/ChapterCacheSyncer.php new file mode 100644 index 00000000..01f0bfbf --- /dev/null +++ b/app/Services/ChapterCacheSyncer.php @@ -0,0 +1,46 @@ +cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + } + + public function addItem($chapterId) + { + $key = $this->getSyncKey(); + + $this->redis->sAdd($key, $chapterId); + + $this->redis->expire($key, $this->lifetime); + } + + public function getSyncKey() + { + return 'chapter_cache_sync'; + } + +} diff --git a/app/Services/CourseCacheSyncer.php b/app/Services/CourseCacheSyncer.php new file mode 100644 index 00000000..65538a77 --- /dev/null +++ b/app/Services/CourseCacheSyncer.php @@ -0,0 +1,46 @@ +cache = $this->getDI()->get('cache'); + + $this->redis = $this->cache->getRedis(); + } + + public function addItem($courseId) + { + $key = $this->getSyncKey(); + + $this->redis->sAdd($key, $courseId); + + $this->redis->expire($key, $this->lifetime); + } + + public function getSyncKey() + { + return 'course_cache_sync'; + } + +} diff --git a/app/Services/Frontend/Course/ChapterList.php b/app/Services/Frontend/Course/ChapterList.php index b94fa56e..5eae58de 100644 --- a/app/Services/Frontend/Course/ChapterList.php +++ b/app/Services/Frontend/Course/ChapterList.php @@ -40,7 +40,11 @@ class ChapterList extends Service return $this->handleChapters($chapters); } - protected function handleChapters(Resultset $chapters) + /** + * @param Resultset $chapters + * @return array + */ + protected function handleChapters($chapters) { if ($chapters->count() == 0) { return []; diff --git a/app/Services/Frontend/Course/PackageList.php b/app/Services/Frontend/Course/PackageList.php index f3e4624b..4de932be 100644 --- a/app/Services/Frontend/Course/PackageList.php +++ b/app/Services/Frontend/Course/PackageList.php @@ -2,6 +2,7 @@ namespace App\Services\Frontend\Course; +use App\Models\Package as PackageModel; use App\Repos\Course as CourseRepo; use App\Repos\Package as PackageRepo; use App\Services\Frontend\CourseTrait; @@ -25,7 +26,7 @@ class PackageList extends Service } /** - * @param Resultset $packages + * @param Resultset|PackageModel[] $packages * @return array */ protected function handlePackages($packages) @@ -58,6 +59,10 @@ class PackageList extends Service $courses = $packageRepo->findCourses($packageId); + if ($courses->count() == 0) { + return []; + } + $result = []; $baseUrl = kg_ci_base_url();