Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# PHP2021
# Домашняя работа №6 - Командная разработка

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus
## Выполнил: Мелёшкин В.В.

Класс `EmailValidator` был вынесен во внешний пакет. Добавить его в проект можно при
помощи команды:

`composer require vmeleshkin/validators`
11 changes: 11 additions & 0 deletions balancer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu:latest

RUN apt-get update && apt-get install -y nginx

COPY hosts/balancer.local.conf /etc/nginx/sites-enabled/my-application.local.conf

WORKDIR /var/www/my-application.local
VOLUME /var/www/my-application.local
EXPOSE 80

CMD [ "nginx", "-g", "daemon off;"]
19 changes: 19 additions & 0 deletions balancer/hosts/balancer.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
upstream nginx-webservers {
server webserver1;
server webserver2;
}

server {
listen 80;

server_name my-application.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

location / {
proxy_pass http://nginx-webservers;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
10 changes: 10 additions & 0 deletions code/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

require_once('vendor/autoload.php');

try {
$app = new App\Application();
$app->run();
} catch (Exception $e) {
echo App\Response::generateBadRequestResponse($e->getMessage());
}
35 changes: 35 additions & 0 deletions code/app_src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App;

use VMeleshkin\Validators\EmailValidator;

class Application
{
private $request;

public function __construct()
{
if (!RequestValidator::checkRequestType('POST')) {
throw new \Exception('ERROR_REQUEST_METHOD');
}

if (empty($_POST)) {
throw new \Exception('EMPTY_REQUEST');
} else {
$this->request = $_POST;
}
}

public function run()
{
$mailValidator = new EmailValidator();
$validationResult = $mailValidator->validateEmail($this->request['email']);

if ($validationResult === 'EMAIL_OK') {
echo Response::generateOkResponse($validationResult);
} else {
throw new \Exception($validationResult);
}
}
}
11 changes: 11 additions & 0 deletions code/app_src/RequestValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App;

class RequestValidator
{
public static function checkRequestType(string $typeNeeded): bool
{
return $_SERVER['REQUEST_METHOD'] == $typeNeeded;
}
}
35 changes: 35 additions & 0 deletions code/app_src/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App;

class Response
{
private static array $arErrors = [
'ERROR_REQUEST_METHOD' => 'Ошибочный метод запроса :(',
'EMPTY_REQUEST' => 'Пустой запрос :/',
'EMPTY_INPUT' => 'В поле ничего не введено :|',
'DATA_NO_GENERATED' => 'Данные не сгенерированы o_0',
'EMAIL_OK' => 'Адрес валиден и MX-запись найдена :)',
'EMAIL_MX_FAILED' => 'MX запись для адреса, не найдено :(',
'EMAIL_VALID_FAILED' => 'Адрес не прошёл валидацию :('
];

public static function generateOkResponse(string $code)
{
header('HTTP/1.0 200 Ok');

return self::getErrorMessage($code) . '<br>';
}

public static function generateBadRequestResponse(string $errorCode)
{
header('HTTP/1.0 400 Bad Request');

return self::getErrorMessage($errorCode) . PHP_EOL;
}

private static function getErrorMessage(string $errorCode): string
{
return self::$arErrors[$errorCode];
}
}
Loading