Skip to content

Commit

Permalink
Merge pull request #4 from DeGraciaMathieu/refacto/clean-archi
Browse files Browse the repository at this point in the history
Refactoring into clean architecture
  • Loading branch information
DeGraciaMathieu authored May 12, 2024
2 parents b85224c + e51b2d5 commit 5cd9b62
Show file tree
Hide file tree
Showing 37 changed files with 7,104 additions and 232 deletions.
43 changes: 43 additions & 0 deletions app/Application/UseCases/InspectHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Application\UseCases;

use Throwable;
use App\Domain\Ports\FileService;
use App\Domain\Ports\AnalyzeService;
use App\Application\UseCases\InspectInput;
use App\Application\UseCases\InspectOutput;

class InspectHandler
{
public function __construct(
private FileService $fileService,
private AnalyzeService $analyzeService,
) {
}

public function handle(
InspectInput $input,
InspectOutput $output,
): void {

try {

$output->hello();

$fileAggregator = $this->fileService->all(
path: $input->path(),
);

$wordsAggregator = $this->analyzeService->getWords(
fileAggregator: $fileAggregator,
withMethod: $input->withMethod(),
);

$output->present($wordsAggregator);

} catch (Throwable $th) {
$output->error($th);
}
}
}
11 changes: 11 additions & 0 deletions app/Application/UseCases/InspectInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Application\UseCases;

use App\Domain\ValueObjects\Path;

interface InspectInput
{
public function path(): Path;
public function withMethod(): bool;
}
13 changes: 13 additions & 0 deletions app/Application/UseCases/InspectOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Application\UseCases;

use Throwable;
use App\Domain\Aggregators\WordAggregator;

interface InspectOutput
{
public function hello();
public function present(WordAggregator $wordAggregator);
public function error(Throwable $th);
}
45 changes: 0 additions & 45 deletions app/Bags/WordsBag.php

This file was deleted.

93 changes: 0 additions & 93 deletions app/Commands/Inspect.php

This file was deleted.

20 changes: 20 additions & 0 deletions app/Domain/Aggregators/FileAggregator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Domain\Aggregators;

use App\Domain\Entities\FileEntity;

class FileAggregator
{
private array $files = [];

public function add(FileEntity $fileEntity): void
{
$this->files[] = $fileEntity;
}

public function files(): array
{
return $this->files;
}
}
51 changes: 51 additions & 0 deletions app/Domain/Aggregators/WordAggregator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Domain\Aggregators;

use App\Domain\Entities\WordEntity;
use App\Domain\Services\WordSorter;

class WordAggregator
{
private array $words = [];

public function add(WordEntity $wordEntity): void
{
if ($wordEntity->canBeIgnored()) {
return;
}

$this->increment($wordEntity);
}

public function sort(): void
{
uasort($this->words, function ($a, $b) {
return $this->sortWordsByDesc($a, $b);
});
}

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

private function increment(WordEntity $wordEntity): void
{
$word = $wordEntity->value();

/**
* Wow, it works well, don't judge me
*/
@$this->words[$word]++;
}

private function sortWordsByDesc($a, $b): int
{
if ($a == $b) {
return 0;
}

return ($a < $b) ? 1 : -1;
}
}
23 changes: 23 additions & 0 deletions app/Domain/Entities/FileEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Domain\Entities;

use App\Domain\ValueObjects\Path;

class FileEntity
{
public function __construct(
public Path $fullPath,
public Path $displayPath,
public string $contents,
) {}

public static function from(array $attributes): self
{
return new self(
fullPath: Path::from($attributes['fullPath']),
displayPath: Path::from($attributes['displayPath']),
contents: $attributes['contents'],
);
}
}
27 changes: 27 additions & 0 deletions app/Domain/Entities/WordEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Domain\Entities;

class WordEntity
{
public function __construct(
private string $value,
) {}

public static function from(string $word): self
{
$word = strtolower($word);

return new self($word);
}

public function canBeIgnored(): bool
{
return $this->value === 'this';
}

public function value(): string
{
return $this->value;
}
}
16 changes: 16 additions & 0 deletions app/Domain/Ports/AnalyzeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domain\Ports;

use App\Domain\Aggregators\FileAggregator;
use App\Domain\Aggregators\WordAggregator;

interface AnalyzeService
{
/**
* Retrieve all words from a set of files.
*
* withMethod argument allows including the words present in the function signatures.
*/
public function getWords(FileAggregator $fileAggregator, bool $withMethod): WordAggregator;
}
14 changes: 14 additions & 0 deletions app/Domain/Ports/FileService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Domain\Ports;

use App\Domain\Aggregators\FileAggregator;
use App\Domain\ValueObjects\Path;

interface FileService
{
/**
* Retrieves all files recursively in an absolute path.
*/
public function all(Path $path): FileAggregator;
}
27 changes: 27 additions & 0 deletions app/Domain/ValueObjects/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Domain\ValueObjects;

class Path
{
public function __construct(
private string $value,
) {}

public static function from(string $value): self
{
static::guard($value);

return new self($value);
}

private static function guard(string $value): void
{
//
}

public function value(): string
{
return $this->value;
}
}
Loading

0 comments on commit 5cd9b62

Please sign in to comment.