Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ install:
- composer update --prefer-dist

script:
- vendor/bin/phpunit
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
- vendor/bin/coveralls
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
"require": {
"php": ">=5.5",
"simple-bus/symfony-bridge": "^4.1",
"clearcodehq/command-bus-launcher": "dev-master",
"symfony/framework-bundle": "~2.3",
"symfony/console": "~2.3",
"symfony/dependency-injection": "~2.3",
"symfony/http-kernel": "~2.3",
"symfony/config": "~2.3"
"symfony/config": "~2.3",
"ramsey/uuid": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"satooshi/php-coveralls": "^0.6.1"
},
"autoload": {
"psr-4" : { "ClearcodeHQ\\CommandBusLauncherBundle\\": "src" }
"psr-4" : { "ClearcodeHQ\\": "src" }
},
"autoload-dev": {
"psr-4" : { "tests\\ClearcodeHQ\\CommandBusLauncherBundle\\" : "tests" }
"psr-4" : { "tests\\ClearcodeHQ\\" : "tests" }
}
}
10 changes: 6 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
<phpunit bootstrap="tests/bootstrap.php" colors="true">

<testsuites>
<testsuite name="tests">
<testsuite name="unit">
<directory suffix="Test.php">./tests</directory>
<exclude>./tests/Command</exclude>
</testsuite>

<testsuite name="command">
<directory suffix="Test.php">./tests/Command</directory>
</testsuite>
</testsuites>

Expand All @@ -18,7 +23,4 @@
</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ClearcodeHQ\CommandBusLauncherBundle\Command;
namespace ClearcodeHQ\Bundle\CommandBusLauncherBundle\Command;

use ClearcodeHQ\CommandBusLauncher\CommandLauncherException;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace ClearcodeHQ\CommandBusLauncherBundle;
namespace ClearcodeHQ\Bundle\CommandBusLauncherBundle;

use ClearcodeHQ\CommandBusLauncherBundle\DependencyInjection\Compiler\CommandHandlersCompilerPass;
use ClearcodeHQ\Bundle\CommandBusLauncherBundle\DependencyInjection\Compiler\CommandHandlersCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

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

namespace ClearcodeHQ\CommandBusLauncherBundle\DependencyInjection;
namespace ClearcodeHQ\Bundle\CommandBusLauncherBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ClearcodeHQ\CommandBusLauncherBundle\DependencyInjection\Compiler;
namespace ClearcodeHQ\Bundle\CommandBusLauncherBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ClearcodeHQ\CommandBusLauncherBundle\DependencyInjection;
namespace ClearcodeHQ\Bundle\CommandBusLauncherBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down
43 changes: 43 additions & 0 deletions src/CommandBusLauncher/ArgumentsProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

use ClearcodeHQ\CommandBusLauncher\ValueConverter\ValueConverter;

class ArgumentsProcessor
{
/**
* @var ValueConverter[]
*/
private $valueConverters;

public function __construct(array $valueConverters = [])
{
$this->valueConverters = $valueConverters;
}

/**
* @param $arguments
*
* @return mixed
*/
public function process($arguments)
{
foreach ($arguments as $key => $argument) {
$arguments[$key] = $this->convertValue($argument);
}

return $arguments;
}

private function convertValue($value)
{
foreach ($this->valueConverters as $valueConverter) {
if (null !== $convertedValue = $valueConverter->convert($value)) {
return $convertedValue;
}
}

return $value;
}
}
32 changes: 32 additions & 0 deletions src/CommandBusLauncher/CommandCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

class CommandCollector
{
/**
* @var CommandReflection[]
*/
public $commands = [];

/**
* @param array $services
*/
public function processCommandServices(array $services)
{
foreach ($services as $service) {
$this->commands[] = CommandReflection::fromClass($service);
}
}

public function getCommandByName($commandName)
{
foreach ($this->commands as $command) {
if ($command->commandName == $commandName) {
return $command;
}
}

throw new CommandDoesNotExist(sprintf("Command '%s' does not exists", $commandName));
}
}
7 changes: 7 additions & 0 deletions src/CommandBusLauncher/CommandDoesNotExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

class CommandDoesNotExist extends CommandLauncherException
{
}
48 changes: 48 additions & 0 deletions src/CommandBusLauncher/CommandLauncher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

class CommandLauncher
{
/** @var CommandCollector */
private $commandCollector;
/** @var ArgumentsProcessor */
private $argumentsProcessor;

/**
* @param CommandCollector $commandCollector
* @param ArgumentsProcessor $argumentsProcessor
*/
public function __construct(CommandCollector $commandCollector, ArgumentsProcessor $argumentsProcessor)
{
$this->commandCollector = $commandCollector;
$this->argumentsProcessor = $argumentsProcessor;
}

/**
* @param $commandName
*
* @return CommandReflection
*
* @throws CommandDoesNotExist
*/
public function getCommandReflection($commandName)
{
return $this->commandCollector->getCommandByName($commandName);
}

/**
* @param $commandName
* @param $arguments
*
* @return object
*
* @throws CommandDoesNotExist
*/
public function getCommandToLaunch($commandName, $arguments)
{
$commandReflection = $this->commandCollector->getCommandByName($commandName);

return $commandReflection->createCommand($arguments, $this->argumentsProcessor);
}
}
7 changes: 7 additions & 0 deletions src/CommandBusLauncher/CommandLauncherException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

class CommandLauncherException extends \Exception
{
}
86 changes: 86 additions & 0 deletions src/CommandBusLauncher/CommandReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

class CommandReflection
{
/** @var string */
public $commandName;
/** @var string */
public $commandClass;

/**
* @param $commandName
* @param $commandClass
*/
public function __construct($commandName, $commandClass)
{
$this->commandName = $commandName;
$this->commandClass = $commandClass;
}

/**
* @param $className
*
* @return CommandReflection
*/
public static function fromClass($className)
{
$reflection = new \ReflectionClass($className);

return new self($reflection->getShortName(), $className);
}

/**
* @return \ReflectionParameter[]
*/
public function parameters()
{
$commandReflection = new \ReflectionClass($this->commandClass);
$commandParameters = $commandReflection->getConstructor()->getParameters();

return $commandParameters;
}

/**
* @param array $arguments
* @param ArgumentsProcessor $argumentsProcessor
*
* @return object
*
* @throws InvalidCommandArgument
* @throws MissingCommandArgument
*/
public function createCommand(array $arguments, ArgumentsProcessor $argumentsProcessor)
{
$classReflection = new \ReflectionClass($this->commandClass);

$inputArguments = $argumentsProcessor->process($arguments);

foreach ($this->parameters() as $commandArgument) {
if ($commandArgument->getClass() !== null) {
if (!array_key_exists($commandArgument->getPosition(), $inputArguments)) {
throw new MissingCommandArgument(
sprintf("Missing argument %s for '%s' command", $commandArgument->getPosition() + 1, $this->commandName)
);
}

$givenArgument = $inputArguments[$commandArgument->getPosition()];
$requiredArgumentClass = $commandArgument->getClass()->name;

if (!is_object($givenArgument) || !$givenArgument instanceof $requiredArgumentClass) {
throw new InvalidCommandArgument(
sprintf(
"Invalid argument for '%s' command. Expected parameter %s to be instance of '%s'",
$this->commandName,
$commandArgument->getPosition() + 1,
$requiredArgumentClass
)
);
}
}
}

return $classReflection->newInstanceArgs($inputArguments);
}
}
7 changes: 7 additions & 0 deletions src/CommandBusLauncher/InvalidCommandArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

class InvalidCommandArgument extends CommandLauncherException
{
}
7 changes: 7 additions & 0 deletions src/CommandBusLauncher/MissingCommandArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher;

class MissingCommandArgument extends CommandLauncherException
{
}
15 changes: 15 additions & 0 deletions src/CommandBusLauncher/ValueConverter/IntConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher\ValueConverter;

class IntConverter implements ValueConverter
{
public function convert($rawValue)
{
if (is_numeric($rawValue)) {
return (int) $rawValue;
}

return;
}
}
17 changes: 17 additions & 0 deletions src/CommandBusLauncher/ValueConverter/UuidConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher\ValueConverter;

use Ramsey\Uuid\Uuid;

class UuidConverter implements ValueConverter
{
public function convert($rawValue)
{
try {
return Uuid::fromString($rawValue);
} catch (\Exception $e) {
return;
}
}
}
8 changes: 8 additions & 0 deletions src/CommandBusLauncher/ValueConverter/ValueConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ClearcodeHQ\CommandBusLauncher\ValueConverter;

interface ValueConverter
{
public function convert($rawValue);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace tests\ClearcodeHQ\CommandBusLauncherBundle\App;
namespace tests\ClearcodeHQ\Bundle\CommandBusLauncherBundle\App;

use ClearcodeHQ\CommandBusLauncherBundle\CommandBusLauncherBundle;
use ClearcodeHQ\Bundle\CommandBusLauncherBundle\CommandBusLauncherBundle;
use SimpleBus\SymfonyBridge\SimpleBusCommandBusBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
Expand Down
Loading