Skip to content

Commit

Permalink
Move GDPR from BaseController to ViewServiceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Jan 2, 2024
1 parent ee87202 commit a02c4d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
12 changes: 0 additions & 12 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,10 @@ protected function view($view = null, $data = [])
{
if (!$this->request->ajax()) {
$data['breadcrumb'] = $this->breadcrumb->render();
$data['gdpr'] = [
'content' => TwigLiteral::fromHtml((new UserSettings)->cookieAgreement()),
'accepted' => $this->gdprAccepted(),
];
}
return view($view, $data);
}

private function gdprAccepted(): bool
{
if ($this->request->user()) {
return (bool)$this->auth->gdpr;
}
return false;
}

/**
* @param string $name
* @param $value
Expand Down
34 changes: 29 additions & 5 deletions app/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php
namespace Coyote\Providers;

use Coyote\Domain\User\UserSettings;
use Coyote\Http\Composers\InitialStateComposer;
use Coyote\Http\Factories\CacheFactory;
use Coyote\Services\Forum\UserDefined;
use Coyote\Services\Guest;
use Illuminate\Contracts\View\View;
use Coyote\User;
use Coyote\View\Twig\TwigLiteral;
use Illuminate\Contracts\Cache;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Factory;
use Lavary\Menu\Builder;
use Lavary\Menu\Menu;

Expand All @@ -18,18 +23,37 @@ class ViewServiceProvider extends ServiceProvider
public function boot(): void
{
$cache = app(Cache\Repository::class);
$this->app['view']->composer(['layout', 'adm.home'], InitialStateComposer::class);
$this->app['view']->composer('layout', function (View $view) use ($cache) {
/** @var Factory $view */
$view = $this->app['view'];
$view->composer(['layout', 'adm.home'], InitialStateComposer::class);
$view->composer('layout', function (View $view) use ($cache) {
$view->with([
'__master_menu' => $this->buildMasterMenu(),

// temporary code
'__dark_theme' => $this->app[Guest::class]->getSetting('dark.theme', true),
'github_stars' => $cache->remember('homepage:github_stars', 30 * 60, fn() => $this->githubStars())
'github_stars' => $cache->remember('homepage:github_stars', 30 * 60, fn() => $this->githubStars()),

'gdpr' => [
'content' => TwigLiteral::fromHtml((new UserSettings)->cookieAgreement()),
'accepted' => $this->gdprAccepted(),
],
]);
});
}

private function gdprAccepted(): bool
{
/** @var Request $request */
$request = $this->app['request'];
$user = $request->user();
if ($user) {
/** @var User $user */
return (bool)$user->gdpr;
}
return false;
}

private function buildMasterMenu(): Builder
{
/** @var Menu $menu */
Expand Down Expand Up @@ -79,7 +103,7 @@ private function githubStars(): ?int
'https://api.github.com/repos/pradoslaw/coyote',
false,
\stream_context_create([
'http' => ['header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36']
'http' => ['header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'],
]));
if ($result !== false) {
$data = @\json_decode($result, true);
Expand Down

0 comments on commit a02c4d2

Please sign in to comment.