Created
July 25, 2014 09:56
-
-
Save mkusher/bbf14719dbd276568cf7 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* This file is part of the core package. | |
* | |
* (c) Aleh Kashnikau <[email protected]> | |
* | |
* Created: 28/06/2014 2:34 PM | |
*/ | |
namespace Core\TasksBundle\Worker; | |
use Core\TasksBundle\Entity\Task; | |
use Core\TasksBundle\Worker\Task\AbstractWorker; | |
use Symfony\Component\DependencyInjection\Container; | |
class TasksWorkerFactory { | |
/** | |
* @param string|Task $task | |
* @return AbstractWorker | |
* @throws \Exception | |
*/ | |
public function getWorker($task){ | |
if($task instanceof Task){ | |
$task_type = get_class($task); | |
$stack = explode('\\', $task_type); | |
$task_type = array_pop($stack); | |
} | |
elseif(is_string($task)) | |
$task_type = $task; | |
else | |
throw new \Exception; | |
if(!array_key_exists($task_type, self::$map)) | |
throw new \Exception; | |
$worker_class = self::$map[$task_type]; | |
/** @var AbstractWorker $worker */ | |
$worker = new $worker_class($this->container); | |
if($task instanceof Task) | |
$worker->setTask($task); | |
return $worker; | |
} | |
public function __construct(Container $service_container) | |
{ | |
$this->container = $service_container; | |
} | |
/** @var \Symfony\Component\DependencyInjection\Container $container */ | |
protected $container; | |
protected static $map = [ | |
'Move' => '\Core\TasksBundle\Worker\Task\MoveWorker', | |
'Mining' => '\Core\TasksBundle\Worker\Task\MiningWorker', | |
'BuildingConstructing' => '\Core\TasksBundle\Worker\Task\BuildingConstructingWorker', | |
'Constructing' => '\Core\TasksBundle\Worker\Task\BuildingConstructingWorker', | |
'RoadConstructing' => '\Core\TasksBundle\Worker\Task\RoadConstructingWorker', | |
'Manufacturing' => '\Core\TasksBundle\Worker\Task\ManufacturingWorker' | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment