Skip to content

Commit

Permalink
Merge pull request #632 from zghosts/di-container
Browse files Browse the repository at this point in the history
Implement pimple as a psr11 compatible di container
  • Loading branch information
mcneely authored Feb 14, 2019
2 parents b15826f + e836584 commit e87b9a6
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 36 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"source" : "https://github.com/joindin/joindin-api"
},
"require" : {
"ext-pdo": "*",
"ext-json": "*",
"swiftmailer/swiftmailer": "^v5.4.9",
"michelf/php-markdown": "^1.8",
"ext-pdo": "*",
"guzzlehttp/guzzle": "^4.2.4",
"guzzlehttp/oauth-subscriber": "0.1.*"
"guzzlehttp/oauth-subscriber": "0.1.*",
"michelf/php-markdown": "^1.8",
"pimple/pimple": "^3.2",
"swiftmailer/swiftmailer": "^v5.4.9"
},
"require-dev": {
"exussum12/coverage-checker": "^0.11.0",
Expand Down
105 changes: 102 additions & 3 deletions composer.lock

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

57 changes: 39 additions & 18 deletions src/controllers/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@
*/
class ContactController extends BaseApiController
{
/**
* @var ContactEmailService
*/
private $emailService;

/**
* @var SpamCheckService
*/
private $spamCheckService;

/**
* ContactController constructor.
*
* @param ContactEmailService $emailService
* @param SpamCheckServiceInterface $spamCheckService
* @param array $config
*/
public function __construct(
ContactEmailService $emailService,
SpamCheckServiceInterface $spamCheckService,
array $config = []
) {
$this->emailService = $emailService;
$this->spamCheckService = $spamCheckService;

parent::__construct($config);
}

public function handle(Request $request, PDO $db)
{
// really need to not require this to be declared
Expand Down Expand Up @@ -32,20 +60,22 @@ public function contact(Request $request, PDO $db)
// only trusted clients can contact us to save on spam
$clientId = $request->getParameter('client_id');
$clientSecret = $request->getParameter('client_secret');
$this->oauthModel = $request->getOauthModel($db);
if (! $this->oauthModel->isClientPermittedPasswordGrant($clientId, $clientSecret)) {
$oauthModel = $request->getOauthModel($db);
if (! $oauthModel->isClientPermittedPasswordGrant($clientId, $clientSecret)) {
throw new Exception("This client cannot perform this action", 403);
}

$fields = ['name', 'email', 'subject', 'comment'];
$error = [];
$data = [];
foreach ($fields as $name) {
$value = $request->getParameter($name);
if (empty($value)) {
$error[] = "'$name'";
}
$data[$name] = $value;
}

if (! empty($error)) {
$message = 'The field';
$message .= count($error) == 1 ? ' ' : 's ';
Expand All @@ -55,24 +85,15 @@ public function contact(Request $request, PDO $db)
throw new Exception($message, 400);
}

// run it by akismet if we have it
if (isset($this->config['akismet']['apiKey'], $this->config['akismet']['blog'])) {
$spamCheckService = new SpamCheckService(
$this->config['akismet']['apiKey'],
$this->config['akismet']['blog']
);
$isValid = $spamCheckService->isCommentAcceptable(
$data['comment'],
$request->getClientIP(),
$request->getClientUserAgent()
);
if (!$isValid) {
throw new Exception("Comment failed spam check", 400);
}
if (!$this->spamCheckService->isCommentAcceptable(
$data,
$request->getClientIP(),
$request->getClientUserAgent()
)) {
throw new Exception("Comment failed spam check", 400);
}

$emailService = new ContactEmailService($this->config);
$emailService->sendEmail($data);
$this->emailService->sendEmail($data);

$view = $request->getView();

Expand Down
124 changes: 124 additions & 0 deletions src/inc/ContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

use Pimple\Container;

class ContainerFactory
{
/**
* @var \Psr\Container\ContainerInterface
*/
private static $container;

/**
* Builds a Psr11 compatible container
*
* @param array $config
* @param bool $rebuild
*
* @return \Psr\Container\ContainerInterface
*/
public static function build(array $config, $rebuild = false)
{
if (!isset(static::$container) || $rebuild) {
$container = new Container();

$container[SpamCheckServiceInterface::class] = function ($c) {
return new NullSpamCheckService();
};

//add $config
if (isset($config['akismet']['apiKey'], $config['akismet']['blog'])) {
$container[SpamCheckServiceInterface::class] = function ($c) use ($config) {
return new SpamCheckService(
$config['akismet']['apiKey'],
$config['akismet']['blog']
);
};
}

$container[ContactEmailService::class] = function ($c) use ($config) {
return new ContactEmailService($config);
};

$container[ContactController::class] = $container->factory(function (Container $c) use ($config) {
return new ContactController(
$c[ContactEmailService::class],
$c[SpamCheckServiceInterface::class],
$config
);
});

$container[ApplicationsController::class] = $container->factory(function ($c) use ($config) {
return new ApplicationsController($config);
});

$container[DefaultController::class] = $container->factory(function ($c) use ($config) {
return new DefaultController($config);
});

$container[EmailsController::class] = $container->factory(function ($c) use ($config) {
return new EmailsController($config);
});

$container[Event_commentsController::class] = $container->factory(function ($c) use ($config) {
return new Event_commentsController($config);
});

$container[Event_hostsController::class] = $container->factory(function ($c) use ($config) {
return new Event_hostsController($config);
});

$container[EventImagesController::class] = $container->factory(function ($c) use ($config) {
return new EventImagesController($config);
});

$container[EventsController::class] = $container->factory(function ($c) use ($config) {
return new EventsController($config);
});

$container[FacebookController::class] = $container->factory(function ($c) use ($config) {
return new FacebookController($config);
});

$container[LanguagesController::class] = $container->factory(function ($c) use ($config) {
return new LanguagesController($config);
});

$container[Talk_commentsController::class] = $container->factory(function ($c) use ($config) {
return new Talk_commentsController($config);
});

$container[TalkLinkController::class] = $container->factory(function ($c) use ($config) {
return new TalkLinkController($config);
});

$container[TalksController::class] = $container->factory(function ($c) use ($config) {
return new TalksController($config);
});

$container[TalkTypesController::class] = $container->factory(function ($c) use ($config) {
return new TalkTypesController($config);
});

$container[TokenController::class] = $container->factory(function ($c) use ($config) {
return new TokenController($config);
});

$container[TracksController::class] = $container->factory(function ($c) use ($config) {
return new TracksController($config);
});

$container[TwitterController::class] = $container->factory(function ($c) use ($config) {
return new TwitterController($config);
});

$container[UsersController::class] = $container->factory(function ($c) use ($config) {
return new UsersController($config);
});

static::$container = new \Pimple\Psr11\Container($container);
}

return static::$container;
}
}
5 changes: 4 additions & 1 deletion src/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function handle_exception($e)
// config setup
define('BASEPATH', '.');
include '../config.php';

$container = ContainerFactory::build($config);

if ($config['mode'] == "development") {
ini_set("html_errors", 0);
}
Expand Down Expand Up @@ -65,7 +68,7 @@ function handle_exception($e)
$router = new ApiRouter($config, $routers, ['2']);

$route = $router->getRoute($request);
$return_data = $route->dispatch($request, $ji_db, $config);
$return_data = $route->dispatch($request, $ji_db, $container);

if ($return_data && isset($request->user_id)) {
$return_data['meta']['user_uri'] = $request->base . '/' . $request->version . '/users/' . $request->user_id;
Expand Down
Loading

0 comments on commit e87b9a6

Please sign in to comment.