Skip to content

Commit

Permalink
Implement application
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeliot-Tm committed Jun 6, 2024
1 parent 832ba3d commit 338883f
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .todo-registrar.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Aeliot\TodoRegistrar\Config;
use Aeliot\TodoRegistrar\Service\File\Finder;

return (new Config())
->setFinder(new Finder());
56 changes: 56 additions & 0 deletions bin/todo-registrar
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env php
<?php

use Aeliot\TodoRegistrar\ApplicationFactory;
use Aeliot\TodoRegistrar\Config;

$autoloaderPath = (static function (): string {
$paths = [
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../vendor/autoload.php',
];

foreach ($paths as $path) {
if (file_exists($path)) {
return realpath($path);
}
}

throw new RuntimeException('Cannot find autoloader');
})();

require_once $autoloaderPath;

$projectPath = dirname($autoloaderPath, 2);

$absolutePathMaker = static function (string $path) use ($projectPath): string {
if (preg_match('#^(?:[[:alpha:]]:[/\\\\]|/)#', $path)) {
return $path;
}

return $projectPath . '/' . $path;
};

$options = (static function () use ($absolutePathMaker): array {
$values = [];
$options = getopt('c:', ['config']);
$defaults = [
'config' => ['c', '.todo-registrar.dist.php'],
];

foreach ($defaults as $long => [$short, $default]) {
if (isset($options[$short], $options[$long])) {
throw new InvalidArgumentException(sprintf('Option %s is duplicated', $long));
}
$values[$long] = $absolutePathMaker($options[$short] ?? $options[$long] ?? $default);
}

return $values;
})();

/** @var Config $config */
$config = require $options['config'];

(new ApplicationFactory())->create($config)->run();
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"Aeliot\\TodoRegistrar\\Test\\": "tests/"
}
},
"bin": [
"bin/todo-registrar"
],
"config": {
"allow-plugins": {
"composer/*": true
Expand Down
24 changes: 24 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar;

use Aeliot\TodoRegistrar\Service\File\Finder;
use Aeliot\TodoRegistrar\Service\FileProcessor;

final class Application
{
public function __construct(
private Finder $finder,
private FileProcessor $fileProcessor,
) {
}

public function run(): void
{
foreach ($this->finder as $file) {
$this->fileProcessor->process($file);
}
}
}
55 changes: 55 additions & 0 deletions src/ApplicationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar;

use Aeliot\TodoRegistrar\Service\Comment\Detector;
use Aeliot\TodoRegistrar\Service\Comment\Extractor;
use Aeliot\TodoRegistrar\Service\CommentRegistrar;
use Aeliot\TodoRegistrar\Service\File\Saver;
use Aeliot\TodoRegistrar\Service\File\Tokenizer;
use Aeliot\TodoRegistrar\Service\FileProcessor;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarFactory;
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface;

final class ApplicationFactory
{
public function create(Config $config): Application
{
$registrar = $this->createRegistrar($config);
$commentRegistrar = $this->createCommentRegistrar($registrar);
$fileProcessor = $this->createFileProcessor($commentRegistrar);

return new Application(
$config->getFinder(),
$fileProcessor,
);
}

private function createCommentRegistrar(RegistrarInterface $registrar): CommentRegistrar
{
return new CommentRegistrar(
new Detector(),
new Extractor(),
$registrar,
);
}

private function createFileProcessor(CommentRegistrar $commentRegistrar): FileProcessor
{
return new FileProcessor(
$commentRegistrar,
new Saver(),
new Tokenizer(),
);
}

private function createRegistrar(Config $config): RegistrarInterface
{
return (new RegistrarFactory())->createRegistrar(
$config->getRegistrarType(),
$config->getRegistrarConfig(),
);
}
}
54 changes: 54 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar;

use Aeliot\TodoRegistrar\Enum\RegistrarType;
use Aeliot\TodoRegistrar\Service\File\Finder;

final class Config
{
private Finder $finder;
/**
* @var array<string,mixed>
*/
private array $registrarConfig;
private RegistrarType $registrarType;

public function getFinder(): Finder
{
return $this->finder;
}

public function setFinder(Finder $finder): self
{
$this->finder = $finder;

return $this;
}

/**
* @return array<string,mixed>
*/
public function getRegistrarConfig(): array
{
return $this->registrarConfig;
}

public function getRegistrarType(): RegistrarType
{
return $this->registrarType;
}

/**
* @param array<string,mixed> $config
*/
public function setRegistrar(RegistrarType $type, array $config): self
{
$this->registrarType = $type;
$this->registrarConfig = $config;

return $this;
}
}
16 changes: 16 additions & 0 deletions src/Enum/RegistrarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Enum;

enum RegistrarType: string
{
case AzureBoards = 'AzureBoards';
case Github = 'Github';
case Gitlab = 'Gitlab';
case JIRA = 'JIRA';
case PivotalTracker = 'PivotalTracker';
case Redmine = 'Redmine';
case YouTrack = 'YouTrack';
}
26 changes: 26 additions & 0 deletions src/Service/Registrar/RegistrarFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Service\Registrar;

use Aeliot\TodoRegistrar\Enum\RegistrarType;

final class RegistrarFactory
{
/**
* @param array<string,mixed> $config
*/
public function createRegistrar(RegistrarType $type, array $config): RegistrarInterface
{
return $this->getExactFactory($type)->create($config);
}

private function getExactFactory(RegistrarType $type): RegistrarFactoryInterface
{
return match ($type) {
// TODO add factory of different registrars
default => throw new \DomainException(sprintf('Not supported registrar type "%s"', $type->value)),
};
}
}
13 changes: 13 additions & 0 deletions src/Service/Registrar/RegistrarFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Service\Registrar;

interface RegistrarFactoryInterface
{
/**
* @param array<string,mixed> $config
*/
public function create(array $config): RegistrarInterface;
}

0 comments on commit 338883f

Please sign in to comment.