* @author Connor S. Parks */ class ComponentsComposer { /** * The user session object. * * @var \Illuminate\Contracts\Auth\Guard */ protected $guard; /** * Creates a new components composer instance. * * @param \Illuminate\Contracts\Auth\Guard $guard * * @return void */ public function __construct(Guard $guard) { $this->guard = $guard; } /** * Bind data to the view. * * @param \Illuminate\Contracts\View\View $view * * @return void */ public function compose(View $view) { $componentGroups = $this->getVisibleGroupedComponents(); $ungroupedComponents = Component::ungrouped()->orderBy('status', 'desc')->get(); $view->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents); } /** * Get visible grouped components. * * @return \Illuminate\Support\Collection */ protected function getVisibleGroupedComponents() { $componentGroupsBuilder = ComponentGroup::query(); if (!$this->guard->check()) { $componentGroupsBuilder->visible(); } $usedComponentGroups = Component::grouped()->pluck('group_id'); return $componentGroupsBuilder->used($usedComponentGroups) ->get(); } }