Skip to content

Commit

Permalink
refactor: переписал приложение используя DDD
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyrcha committed Apr 2, 2024
1 parent 43e49f5 commit 207c713
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 45 deletions.
8 changes: 4 additions & 4 deletions app/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Homework 13
# Homework 15

1. `cd app`
2. `cp .env.example .env`
3. `docker-compose up --build -d`
4. `docker-compose exec -it postrges bash`
4. `docker compose exec -it postrges bash`
5. `psql -U test test < /var/www/application.local/sql/ddl.sql`
6. `psql -U test test < /var/www/application.local/sql/dml.sql`
7. `docker-compose exec -it php bash`
7. `docker compose exec -it php bash`
8. `cd /var/www/application.local`
9. `composer install`
10. `php public/index.php`
10. `php public/index.php all`
3 changes: 1 addition & 2 deletions app/fpm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ RUN apt-get update && apt-get install -y \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& pecl install memcached \
&& docker-php-ext-enable memcached \

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

CMD ["php-fpm"]
27 changes: 25 additions & 2 deletions app/www/application.local/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@

declare(strict_types=1);

use AYamaliev\hw13\App;
use AYamaliev\hw13\Application\Db\Connection;
use AYamaliev\hw13\Application\Dto\NewsDto;
use AYamaliev\hw13\Infrastructure\NewsController;

require __DIR__ . '/../vendor/autoload.php';

try {
(new App())->run();
$connection = Connection::get()->connect();
$controller = new NewsController($connection);

switch ($argv[1]) {
case 'all':
$result = $controller->getAll();
break;
case 'get':
$result = $controller->getById((int)$argv[2]);
break;
case 'delete':
$result = $controller->deleteById((int)$argv[2]);
break;
case 'add':
$newsDto = new NewsDto($argv);
$result = $controller->add($newsDto);
break;
default:
$result = 'Use `php public/index.php [all|get 1|delete 1|add --title=title --text=text --image=path-to-image --created_at=2024-04-12]`';
}

print_r($result);
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
30 changes: 0 additions & 30 deletions app/www/application.local/src/App.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

namespace AYamaliev\hw13\DataMapper;
namespace AYamaliev\hw13\Application\DataMapper;

use AYamaliev\hw13\IdentityMap\IdentityMap;
use AYamaliev\hw13\Domain\IdentityMap\IdentityMap;
use AYamaliev\hw13\Domain\News;
use PDO;
use PDOStatement;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AYamaliev\hw13\Db;
namespace AYamaliev\hw13\Application\Db;

final class Connection
{
Expand Down
54 changes: 54 additions & 0 deletions app/www/application.local/src/Application/Dto/NewsDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace AYamaliev\hw13\Application\Dto;

class NewsDto
{
private string $title;
private string $text;
private string $image;
private string $created_at;

public function __construct(array $arguments)
{
foreach ($arguments as $argument) {
[$argumentName, $argumentValue] = explode('=', $argument);

switch ($argumentName) {
case '--title':
$this->title = $argumentValue;
break;
case '--text':
$this->text = $argumentValue;
break;
case '--image':
$this->image = $argumentValue;
break;
case '--created_at':
$this->created_at = $argumentValue;
break;
}
}
}
public function getTitle(): string
{
return $this->title;
}

public function getText(): string
{
return $this->text;
}

public function getImage(): string
{
return $this->image;
}

public function getCreatedAt(): string
{
return $this->created_at;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace AYamaliev\hw13\IdentityMap;
namespace AYamaliev\hw13\Domain\IdentityMap;

class IdentityMap
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AYamaliev\hw13\IdentityMap;
namespace AYamaliev\hw13\Domain\IdentityMap;

interface IdentityMapInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace AYamaliev\hw13\DataMapper;
namespace AYamaliev\hw13\Domain;

use AYamaliev\hw13\IdentityMap\IdentityMapInterface;
use AYamaliev\hw13\Domain\IdentityMap\IdentityMapInterface;

class News implements IdentityMapInterface
{
Expand Down
50 changes: 50 additions & 0 deletions app/www/application.local/src/Infrastructure/NewsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace AYamaliev\hw13\Infrastructure;

use AYamaliev\hw13\Application\DataMapper\NewsMapper;
use AYamaliev\hw13\Application\Dto\NewsDto;
use AYamaliev\hw13\Domain\IdentityMap\IdentityMap;
use AYamaliev\hw13\Domain\News;

class NewsController
{
public \PDO $pdo;
public IdentityMap $identityMap;
public NewsMapper $newsMapper;

public function __construct($connection)
{
$this->identityMap = new IdentityMap();
$this->pdo = $connection;
$this->newsMapper = new NewsMapper($this->pdo, $this->identityMap);
}

public function getAll(): array
{
return $this->newsMapper->getAll();
}

public function getById(int $id): ?News
{
return $this->newsMapper->findById($id);
}

public function deleteById(int $id): bool
{
$news = $this->newsMapper->findById($id);
return $this->newsMapper->delete($news);
}

public function add(NewsDto $newsDto): News
{
return $this->newsMapper->insert([
'title' => $newsDto->getTitle(),
'text' => $newsDto->getText(),
'image' => $newsDto->getImage(),
'created_at' => $newsDto->getCreatedAt(),
]);
}
}

0 comments on commit 207c713

Please sign in to comment.