Implemented SourceClassInformationService

This commit is contained in:
Kevin Frantz 2019-02-03 03:16:45 +01:00
parent 4c4af9308a
commit 4e471c40cd
3 changed files with 199 additions and 0 deletions

View File

@ -0,0 +1,132 @@
<?php
namespace App\Domain\SourceManagement;
/**
* @author kevinfrantz
*/
final class SourceClassInformationService implements SourceClassInformationServiceInterface
{
/**
* @var string Folder with the source entities
*/
const FOLDER = __DIR__.'/../../Entity/Source';
/**
* @var string Namespace praefix for sources
*/
const SOURCE_CLASS_NAMESPACE = 'App\\Entity\\Source';
/**
* @var string Suffix to identifie php files
*/
const PHP_SUFFIX = '.php';
/**
* @var string Suffix to identify interfaces
*/
const INTERFACE_SUFFIX = 'Interface'.self::PHP_SUFFIX;
/**
* @var string[]|array Containes all source classes
*/
private $allClasses = [];
/**
* @param string $path
*
* @return bool
*/
private function isPHP(string $path): bool
{
return self::PHP_SUFFIX === substr($path, -strlen(self::PHP_SUFFIX));
}
/**
* @param string $path
*
* @return bool
*/
private function isInterface(string $path): bool
{
return self::INTERFACE_SUFFIX === substr($path, -strlen(self::INTERFACE_SUFFIX));
}
/**
* @param string $path
*
* @return string
*/
private function transformPathToClass(string $path): string
{
$withoutSuffix = str_replace(self::PHP_SUFFIX, '', $path);
$withoutFolder = str_replace(self::FOLDER, '', $withoutSuffix);
$class = str_replace('/', '\\', $withoutFolder);
$fullclass = self::SOURCE_CLASS_NAMESPACE.$class;
return $fullclass;
}
/**
* @param string $path
*/
private function addToClasses(string $path): void
{
$this->allClasses[] = $this->transformPathToClass($path);
}
private function loadClasses()
{
$recursiveDirectoryIterator = new \RecursiveDirectoryIterator(self::FOLDER);
$files = new \RecursiveIteratorIterator($recursiveDirectoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$path = $file->getPathname();
if ($this->isPHP($path) && !$this->isInterface($path)) {
$this->addToClasses($path);
}
}
}
public function __construct()
{
$this->loadClasses();
}
/**
* @param string $subPath
* @param string $rootPath
*
* @return bool
*/
private function isSubSourceClass(string $subPath, string $rootPath): bool
{
return substr($rootPath, 0, strlen($subPath)) === $subPath;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\SourceManagement\SourceClassInformationServiceInterface::getAllSourceClasses()
*/
public function getAllSourceClasses(): array
{
return $this->allClasses;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\SourceManagement\SourceClassInformationServiceInterface::getAllSubSourceClasses()
*/
public function getAllSubSourceClasses(string $subNamespace): array
{
$subSourceClasses = [];
foreach ($this->allClasses as $class) {
if ($this->isSubSourceClass($subNamespace, $class)) {
$subSourceClasses[] = $class;
}
}
return $subSourceClasses;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Domain\SourceManagement;
/**
* Offers informations about the source classes.
*
* @author kevinfrantz
*/
interface SourceClassInformationServiceInterface
{
/**
* @return array|string[] Returns all source classes
*/
public function getAllSourceClasses(): array;
/**
* @param string $subNamespace The subpath of the classes
*
* @return array|string[] Returns all source classes of a subfolder
*/
public function getAllSubSourceClasses(string $subNamespace): array;
}

View File

@ -0,0 +1,44 @@
<?php
namespace tests\Unit\Domain\SourceManagement;
use App\Domain\SourceManagement\SourceClassInformationServiceInterface;
use App\Domain\SourceManagement\SourceClassInformationService;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\PureSource;
use App\Entity\Source\Complex\AbstractComplexSource;
/**
* @author kevinfrantz
*/
class SourceClassInformationServiceTest extends TestCase
{
/**
* @var SourceClassInformationServiceInterface
*/
private $sourceClassInformationService;
public function setUp(): void
{
$this->sourceClassInformationService = new SourceClassInformationService();
}
public function testPureSource(): void
{
$allClasses = $this->sourceClassInformationService->getAllSourceClasses();
$this->assertTrue(in_array(PureSource::class, $allClasses));
}
public function testNotSource(): void
{
$allClasses = $this->sourceClassInformationService->getAllSourceClasses();
$this->assertFalse(in_array('ALLALALABBBB', $allClasses));
}
public function testSubSource(): void
{
$allClasses = $this->sourceClassInformationService->getAllSubSourceClasses('App\\Entity\\Source\\Complex');
$this->assertFalse(in_array(PureSource::class, $allClasses));
$this->assertTrue(in_array(AbstractComplexSource::class, $allClasses));
}
}