Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

глава 4. fix переносов скобок #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 29 additions & 24 deletions manuscript/4-application-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
```php
public function store(
Request $request,
ImageUploader $imageUploader)
{
ImageUploader $imageUploader
) {
$this->validate($request, [
'email' => 'required|email',
'name' => 'required',
Expand All @@ -17,7 +17,8 @@ public function store(

$avatarFileName = ...;
$imageUploader->upload(
$avatarFileName, $request->file('avatar'));
$avatarFileName, $request->file('avatar')
);

$user = new User();
$user->email = $request['email'];
Expand All @@ -26,7 +27,7 @@ public function store(
$user->subscribed = $request->has('subscribed');
$user->birthDate = new DateTime($request['birthDate']);

if(!$user->save()) {
if (!$user->save()) {
return redirect()->back()->withMessage('...');
}

Expand Down Expand Up @@ -57,7 +58,7 @@ final class UserService
Методы контроллеров начинают расти и обычно содержат две большие части:

```php
public function doSomething(Request $request, $id)
public function doSomething(Request $request, int $id)
{
$entity = Entity::find($id);

Expand Down Expand Up @@ -106,8 +107,8 @@ final class UserService
private EmailSender $emailSender;

public function __construct(
ImageUploader $imageUploader, EmailSender $emailSender)
{
ImageUploader $imageUploader, EmailSender $emailSender
) {
$this->imageUploader = $imageUploader;
$this->emailSender = $emailSender;
}
Expand All @@ -116,7 +117,8 @@ final class UserService
{
$avatarFileName = ...;
$this->imageUploader->upload(
$avatarFileName, $request['avatar']);
$avatarFileName, $request['avatar']
);

$user = new User();
$user->email = $request['email'];
Expand Down Expand Up @@ -186,8 +188,8 @@ final class UserCreateDto
private bool $subscribed;

public function __construct(
string $email, DateTime $birthDate, bool $subscribed)
{
string $email, DateTime $birthDate, bool $subscribed
) {
$this->email = $email;
$this->birthDate = $birthDate;
$this->subscribed = $subscribed;
Expand Down Expand Up @@ -241,7 +243,8 @@ final class UserService
{
$avatarFileName = ...;
$this->imageUploader->upload(
$avatarFileName, $request->avatarFile);
$avatarFileName, $request->avatarFile
);

$user = new User();
$user->email = $request->email;
Expand Down Expand Up @@ -271,7 +274,8 @@ public function store(Request $request, UserService $userService)
$dto = new UserCreateDto(
$request['email'],
new DateTime($request['birthDate']),
$request->has('subscribed'));
$request->has('subscribed')
);

if (!$userService->create($dto)) {
return redirect()->back()->withMessage('...');
Expand Down Expand Up @@ -309,15 +313,16 @@ final class UserCreateRequest extends FormRequest
return new UserCreateDto(
$this->get('email'),
new DateTime($this->get('birthDate')),
$this->has('subscribed'));
$this->has('subscribed')
);
}
}

final class UserController extends Controller
{
public function store(
UserCreateRequest $request, UserService $userService)
{
UserCreateRequest $request, UserService $userService
) {
if (!$userService->create($request->getDto())) {
return redirect()->back()->withMessage('...');
}
Expand All @@ -342,7 +347,7 @@ final class UserController extends Controller
```php
class PostController
{
public function publish($id, PostService $postService)
public function publish(int $id, PostService $postService)
{
$post = Post::find($id);

Expand Down Expand Up @@ -397,7 +402,7 @@ public function handle(PostService $postService)
```php
class PostController
{
public function publish($id, PostService $postService)
public function publish(int $id, PostService $postService)
{
$post = $postService->getById($id);

Expand All @@ -420,7 +425,7 @@ class PostController
```php
class PostController
{
public function publish($id, PostService $postService)
public function publish(int $id, PostService $postService)
{
if (!$postService->publish($id)) {
return redirect()->back()->withMessage('...');
Expand Down Expand Up @@ -485,7 +490,7 @@ final class PostService
```php
final class PublishPostCommand
{
public function execute($id)
public function execute(int $id)
{
//...
}
Expand All @@ -502,8 +507,8 @@ final class ChangeUserPasswordCommand
final class ChangeUserPasswordCommandHandler
{
public function handle(
ChangeUserPasswordCommand $command)
{
ChangeUserPasswordCommand $command
) {
//...
}
}
Expand All @@ -513,8 +518,8 @@ final class ChangeUserPasswordCommandHandler
final class UserCommandHandler
{
public function handleChangePassword(
ChangeUserPasswordCommand $command)
{
ChangeUserPasswordCommand $command
) {
//...
}
}
Expand All @@ -534,4 +539,4 @@ final class UserCommandHandler
Выделение слоя приложения - весьма ответственный шаг и причина должна быть серьезной. Их всего две:

1. Разные интерфейсы к одним и тем же действиям (Web, API, Console, различные боты). Тут вынесение общей логики просится само.
2. Разросшиеся и сложные логики обработки веб-запроса, например, и бизнес-логики. В данном случае разделение логик по разным местам может заметно улучшить связность кода. Как правило, это ведет к повышенной багостойкости кода.
2. Разросшиеся и сложные логики обработки веб-запроса, например, и бизнес-логики. В данном случае разделение логик по разным местам может заметно улучшить связность кода. Как правило, это ведет к повышенной багостойкости кода.