Skip to content

Commit

Permalink
Merge pull request #281 from php-school/phpstan-and-cleanup
Browse files Browse the repository at this point in the history
Fix phpstan and remove lots of unused code
  • Loading branch information
AydinHassan authored Mar 10, 2024
2 parents e8c2ec5 + 65b03f4 commit 5b1d6f1
Show file tree
Hide file tree
Showing 80 changed files with 651 additions and 1,087 deletions.
1 change: 0 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ SEND_GRID_API_KEY=sendgripapikey
[email protected]
REDIS_HOST=redis
CACHE.ENABLE=false
CACHE.FPC.ENABLE=false
DISPLAY_ERRORS=true
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ jobs:

- name: Run phpcs
run: composer cs
#
# - name: Run psalm
# run: composer static

- name: Run PHPStan
run: composer static
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ docker compose exec php composer app:gen:blog

Then navigate to `http://localhost` !

Pages are cached on first view.
If you need to clear the cache, run `docker compose exec php composer app:cc`.

You can disable the cache by setting `CACHE.FPC.ENABLE` to `false` in your `.env` file.

## Build CSS & JS

This needs to be done for the main website (non cloud) to run in development mode.
Expand Down
62 changes: 7 additions & 55 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@
use PhpSchool\Website\InputFilter\Login as LoginInputFilter;
use PhpSchool\Website\InputFilter\SubmitWorkshop as SubmitWorkshopInputFilter;
use PhpSchool\Website\InputFilter\WorkshopComposerJson as WorkshopComposerJsonInputFilter;
use PhpSchool\Website\Middleware\FpcCache;
use PhpSchool\Website\Middleware\Session as SessionMiddleware;
use PhpSchool\Website\PhpRenderer;
use PhpSchool\Website\Repository\DoctrineORMBlogRepository;
use PhpSchool\Website\Repository\EventRepository;
use PhpSchool\Website\Repository\WorkshopInstallRepository;
Expand Down Expand Up @@ -122,47 +120,23 @@
'app' => factory(function (ContainerInterface $c): App {
$app = Bridge::create($c);
$app->addRoutingMiddleware();
$app->add($c->get(FpcCache::class));

$app->add(function (Request $request, RequestHandler $handler) use($c) : Response {
$renderer = $this->get(PhpRenderer::class);
/** @var Session $session */
$session = $this->get(Session::class);

$student = $session->get('student');

$request = $request->withAttribute('student', $student);
$renderer->addAttribute('student', $student);
$renderer->addAttribute(
'totalExerciseCount',
$c->get(CloudWorkshopRepository::class)->totalExerciseCount()
);

return $handler->handle($request)
->withHeader('cache-control', 'no-cache');
});
$app->add(StudentRefresher::class);
$app->add(new SessionMiddleware(['name' => 'phpschool']));

$app->add(function (Request $request, RequestHandler $handler) use ($c){
$renderer = $c->get(PhpRenderer::class);
$renderer->addAttribute('userAgent', new Agent);
$renderer->addAttribute('route', $request->getUri()->getPath());

return $handler->handle($request);
});

return $app;
}),
FpcCache::class => factory(function (ContainerInterface $c): FpcCache {
return new FpcCache($c->get('cache.fpc'));
}),
'cache.fpc' => factory(function (ContainerInterface $c): CacheInterface {
if (!$c->get('config')['enablePageCache']) {
return new NullAdapter;
}
return new RedisAdapter(new Predis\Client(['host' => $c->get('config')['redisHost']]), 'fpc');
}),
'cache' => factory(function (ContainerInterface $c): CacheInterface {
if (!$c->get('config')['enableCache']) {
return new NullAdapter;
Expand All @@ -183,18 +157,6 @@

return new RedisAdapter($redisConnection, 'default');
}),
PhpRenderer::class => factory(function (ContainerInterface $c): PhpRenderer {

$settings = $c->get('config')['renderer'];
$renderer = new PhpRenderer(
$settings['template_path'],
[
'links' => $c->get('config')['links'],
],
);

return $renderer;
}),
LoggerInterface::class => factory(function (ContainerInterface $c): LoggerInterface{
$settings = $c->get('config')['logger'];
$logger = new Logger($settings['name']);
Expand All @@ -215,7 +177,7 @@

//commands
ClearCache::class => factory(function (ContainerInterface $c): ClearCache {
return new ClearCache($c->get('cache.fpc'));
return new ClearCache($c->get('cache'));
}),
CreateAdminUser::class => factory(function (ContainerInterface $c): CreateAdminUser {
return new CreateAdminUser($c->get(EntityManagerInterface::class));
Expand Down Expand Up @@ -275,30 +237,28 @@

ClearCacheAction::class => function (ContainerInterface $c): ClearCacheAction {
return new ClearCacheAction(
$c->get('cache.fpc'),
$c->get('cache'),
);
},

Requests::class => \DI\factory(function (ContainerInterface $c): Requests {
return new Requests(
$c->get(WorkshopRepository::class),
$c->get(PhpRenderer::class)
);
}),

All::class => \DI\factory(function (ContainerInterface $c): All {
return new All(
$c->get(WorkshopRepository::class),
$c->get(WorkshopInstallRepository::class),
$c->get(PhpRenderer::class)
);
}),

Approve::class => \DI\factory(function (ContainerInterface $c): Approve {
return new Approve(
$c->get(WorkshopRepository::class),
$c->get(WorkshopFeed::class),
$c->get('cache.fpc'),
$c->get('cache'),
$c->get(EmailNotifier::class),
$c->get(LoggerInterface::class)
);
Expand All @@ -308,7 +268,7 @@
return new Promote(
$c->get(WorkshopRepository::class),
$c->get(WorkshopFeed::class),
$c->get('cache.fpc'),
$c->get('cache'),
);
}),

Expand All @@ -317,15 +277,14 @@
$c->get(WorkshopRepository::class),
$c->get(WorkshopInstallRepository::class),
$c->get(WorkshopFeed::class),
$c->get('cache.fpc'),
$c->get('cache'),
);
}),

View::class => function (ContainerInterface $c): View {
return new View(
$c->get(WorkshopRepository::class),
$c->get(WorkshopInstallRepository::class),
$c->get(PhpRenderer::class)
);
},

Expand Down Expand Up @@ -410,7 +369,7 @@
},

EventAll::class => function (ContainerInterface $c): EventAll {
return new EventAll($c->get(EventRepository::class), $c->get(PhpRenderer::class));
return new EventAll($c->get(EventRepository::class));
},

EventCreate::class => function (ContainerInterface $c): EventCreate {
Expand All @@ -424,14 +383,13 @@
return new EventUpdate(
$c->get(EventRepository::class),
$c->get('form.event'),
$c->get(PhpRenderer::class),
);
},

EventDelete::class => function (ContainerInterface $c): EventDelete {
return new EventDelete(
$c->get(EventRepository::class),
$c->get('cache.fpc'),
$c->get('cache'),
);
},

Expand Down Expand Up @@ -473,7 +431,6 @@
StudentAuthenticator::class => function (ContainerInterface $c): StudentAuthenticator {
return new StudentAuthenticator(
$c->get(Session::class),
$c->get(StudentRepository::class)
);
},

Expand Down Expand Up @@ -528,10 +485,6 @@ public function parse($markdown): string
);
},

Styles::class => function (ContainerInterface $c) {
return new Styles($c->get(PhpRenderer::class));
},

CloudWorkshopRepository::class => function (ContainerInterface $c): CloudWorkshopRepository {
return new CloudWorkshopRepository($c->get(WorkshopRepository::class));
},
Expand Down Expand Up @@ -602,7 +555,6 @@ public function parse($markdown): string
'github-website' => 'https://github.com/php-school/phpschool.io',
],

'enablePageCache' => filter_var($_ENV['CACHE.FPC.ENABLE'], FILTER_VALIDATE_BOOLEAN),
'enableCache' => filter_var($_ENV['CACHE.ENABLE'], FILTER_VALIDATE_BOOLEAN),
'redisHost' => $_ENV['REDIS_HOST'],
'devMode' => filter_var($_ENV['DEV_MODE'], FILTER_VALIDATE_BOOLEAN),
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"jenssegers/agent": "^2.3",
"sendgrid/sendgrid": "^7.9",
"vlucas/phpdotenv": "^5.3",
"adamwathan/bootforms": "^0.9",
"mnapoli/front-yaml": "^1.5",
"laminas/laminas-inputfilter": "^2.28",
"laminas/laminas-uri": "^2.11",
Expand Down
96 changes: 1 addition & 95 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@
$group->post('/tour/complete', TourComplete::class);
$group->post('/reset', ResetState::class);
})
->add($container->get(StudentAuthenticator::class))
->add(Styles::class);
->add($container->get(StudentAuthenticator::class));

// Run app
$app->run();
24 changes: 23 additions & 1 deletion src/Action/Admin/Event/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@
use Psr\Http\Message\ResponseInterface as Response;
use Laminas\Filter\Exception\RuntimeException;

/**
* @phpstan-import-type EventData from \PhpSchool\Website\InputFilter\Event
*/
class Create
{
use JsonUtils;

private EventRepository $repository;

/**
* @var FormHandler<EventData>
*/
private FormHandler $formHandler;

/**
* @param FormHandler<EventData> $formHandler
*/
public function __construct(
EventRepository $repository,
FormHandler $formHandler,
Expand Down Expand Up @@ -47,11 +57,23 @@ public function __invoke(Request $request, Response $response): MessageInterface
);
}

$date = \DateTime::createFromFormat('Y-m-d\TH:i', $values['date']);

if (false === $date) {
return $this->withJson(
[
'success' => false,
'form_errors' => ['date' => 'Invalid date format']
],
$response
);
}

$event = new Event(
$values['name'],
$values['description'],
$values['link'] ?? null,
\DateTime::createFromFormat('Y-m-d\TH:i', $values['date']),
$date,
$values['venue'],
isset($values['poster']['tmp_name']) ? basename($values['poster']['tmp_name']) : null
);
Expand Down
Loading

0 comments on commit 5b1d6f1

Please sign in to comment.