-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from DeGraciaMathieu/refacto/clean-archi
Refactoring into clean architecture
- Loading branch information
Showing
37 changed files
with
7,104 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.