1
0
mirror of https://github.com/flarum/core.git synced 2025-07-26 11:10:41 +02:00
Files
php-flarum/src/Install/Controller/InstallController.php
Franz Liedke 3417c0cbee Cleanup code from #1876
- Extract a method for email address generation
- Consistent types
- No docblocks for types where superfluous
- Tweak console output
- Don't inherit from integration test's base class in unit test
2019-09-24 01:00:22 +02:00

133 lines
3.7 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Install\Controller;
use Flarum\Http\SessionAuthenticator;
use Flarum\Install\AdminUser;
use Flarum\Install\BaseUrl;
use Flarum\Install\DatabaseConfig;
use Flarum\Install\Installation;
use Flarum\Install\StepFailed;
use Flarum\Install\ValidationFailed;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;
class InstallController implements RequestHandlerInterface
{
/**
* @var Installation
*/
protected $installation;
/**
* @var SessionAuthenticator
*/
protected $authenticator;
/**
* InstallController constructor.
* @param Installation $installation
* @param SessionAuthenticator $authenticator
*/
public function __construct(Installation $installation, SessionAuthenticator $authenticator)
{
$this->installation = $installation;
$this->authenticator = $authenticator;
}
/**
* @param Request $request
* @return ResponseInterface
*/
public function handle(Request $request): ResponseInterface
{
$input = $request->getParsedBody();
$baseUrl = BaseUrl::fromUri($request->getUri());
try {
$pipeline = $this->installation
->baseUrl($baseUrl)
->databaseConfig($this->makeDatabaseConfig($input))
->adminUser($this->makeAdminUser($input))
->settings([
'forum_title' => Arr::get($input, 'forumTitle'),
'mail_from' => $baseUrl->toEmail('noreply'),
'welcome_title' => 'Welcome to '.Arr::get($input, 'forumTitle'),
])
->build();
} catch (ValidationFailed $e) {
return new Response\HtmlResponse($e->getMessage(), 500);
}
try {
$pipeline->run();
} catch (StepFailed $e) {
return new Response\HtmlResponse($e->getPrevious()->getMessage(), 500);
}
$session = $request->getAttribute('session');
$this->authenticator->logIn($session, 1);
return new Response\EmptyResponse;
}
private function makeDatabaseConfig(array $input): DatabaseConfig
{
$host = Arr::get($input, 'mysqlHost');
$port = 3306;
if (Str::contains($host, ':')) {
list($host, $port) = explode(':', $host, 2);
}
return new DatabaseConfig(
'mysql',
$host,
intval($port),
Arr::get($input, 'mysqlDatabase'),
Arr::get($input, 'mysqlUsername'),
Arr::get($input, 'mysqlPassword'),
Arr::get($input, 'tablePrefix')
);
}
/**
* @param array $input
* @return AdminUser
* @throws ValidationFailed
*/
private function makeAdminUser(array $input): AdminUser
{
return new AdminUser(
Arr::get($input, 'adminUsername'),
$this->getConfirmedAdminPassword($input),
Arr::get($input, 'adminEmail')
);
}
private function getConfirmedAdminPassword(array $input): string
{
$password = Arr::get($input, 'adminPassword');
$confirmation = Arr::get($input, 'adminPasswordConfirmation');
if ($password !== $confirmation) {
throw new ValidationFailed('The admin password did not match its confirmation.');
}
return $password;
}
}