|
@@ -3,75 +3,126 @@
|
|
|
|
|
|
namespace FTTHBundle\Service;
|
|
|
|
|
|
-use Exception;
|
|
|
-use FTTHBundle\Entity\ONU;
|
|
|
+use Base\AdminBundle\Controller\TenancyService;
|
|
|
+use Doctrine\ORM\EntityManager;
|
|
|
+use FTTHBundle\Domain\OnuTransition\OnuTransitionFactory;
|
|
|
use FTTHBundle\Factory\ExceptionFactory;
|
|
|
-use FTTHBundle\Utils\ClientStateEnum;
|
|
|
use FTTHBundle\Utils\ONUStateEnum;
|
|
|
-use WebserviceBundle\Services\Webservice;
|
|
|
+use Symfony\Bundle\FrameworkBundle\Console\Application;
|
|
|
+use Symfony\Component\Console\Input\ArgvInput;
|
|
|
+use Symfony\Component\Console\Output\BufferedOutput;
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
|
|
class ClientService
|
|
|
{
|
|
|
/**
|
|
|
- * @var Webservice
|
|
|
+ * @var EntityManager
|
|
|
*/
|
|
|
- protected $webservice;
|
|
|
+ private $entityManager;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var TenancyService
|
|
|
+ */
|
|
|
+ private $tenancyService;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* @var ContainerInterface
|
|
|
*/
|
|
|
- protected $serviceContainer;
|
|
|
+ private $container;
|
|
|
|
|
|
/**
|
|
|
- * @param Webservice $webservice
|
|
|
- * @param ContainerInterface $serviceContainer
|
|
|
+ * @param EntityManager $entityManager
|
|
|
+ * @param TenancyService $tenancyService
|
|
|
+ * @param ContainerInterface $container
|
|
|
*/
|
|
|
- public function __construct(Webservice $webservice, ContainerInterface $serviceContainer)
|
|
|
+ public function __construct(EntityManager $entityManager, TenancyService $tenancyService, ContainerInterface $container)
|
|
|
{
|
|
|
- $this->webservice = $webservice;
|
|
|
- $this->serviceContainer = $serviceContainer;
|
|
|
+ $this->entityManager = $entityManager;
|
|
|
+ $this->tenancyService = $tenancyService;
|
|
|
+ $this->container = $container;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param $id int
|
|
|
- * @return Client || Exception
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public function getById($id){
|
|
|
+ public function applyTransition($clientId, $transition){
|
|
|
try{
|
|
|
- $this->checkContainerParameter();
|
|
|
- $filters = array("qb-ids" => $id, 'qb-criteria' => true);
|
|
|
- return $this->webservice->getData($this->serviceContainer->getParameter('client'), $filters)[0];
|
|
|
- }catch (Exception $ex){
|
|
|
- ExceptionFactory::make($ex->getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
+ if(!in_array($transition, ONUStateEnum::getStatesTypes())){
|
|
|
+ throw new \Exception("Don't apply this transition");
|
|
|
+ }
|
|
|
|
|
|
+ $query = $this->entityManager->createQuery("
|
|
|
+ SELECT o FROM FTTHBundle:ONU o
|
|
|
+ WHERE o.clientId = :client
|
|
|
+ ")->setParameters([
|
|
|
+ 'client' => $clientId
|
|
|
+ ]);
|
|
|
|
|
|
- /**
|
|
|
- * @param ONU $onu
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public function setStateOnu(ONU $onu){
|
|
|
- try{
|
|
|
- $clientEnums = new ClientStateEnum();
|
|
|
- $client = $this->getById($onu->getClientId());
|
|
|
- if(!is_null($client) && $clientEnums->containsStatesDisabled($client['currentState'])){
|
|
|
- ExceptionFactory::make("Not is possible use client with current state 'cancelled' or 'deleted'");
|
|
|
+ $serialNumbers = array();
|
|
|
+
|
|
|
+ $onus = $query->getResult();
|
|
|
+ if(!is_null($onus)){
|
|
|
+ $factory = (new OnuTransitionFactory($transition));
|
|
|
+
|
|
|
+ foreach ($onus as $onu){
|
|
|
+ if($onu->getCurrentState() !== strtolower($transition)){
|
|
|
+ $onuTransition = $factory->build($onu);
|
|
|
+ if(!is_null($onuTransition->getTransitions())){
|
|
|
+ foreach ($onuTransition->getTransitions() as $t){
|
|
|
+ $this->execute($onu->getId(), $t);
|
|
|
+ }
|
|
|
+ $serialNumbers[$onu->getId()] = $onu->getPonSerialNumber();
|
|
|
+ } else if($onuTransition->getTo() !== null && is_null($onuTransition->getTransitions())){
|
|
|
+ $this->execute($onu->getId(), $onuTransition->getTo());
|
|
|
+ $serialNumbers[$onu->getId()] = $onu->getPonSerialNumber();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- $onu->setCurrentState(ONUStateEnum::ACTIVE);
|
|
|
- }catch (Exception $ex){
|
|
|
+
|
|
|
+ return array(
|
|
|
+ 'transition' => $transition,
|
|
|
+ 'count' => count($onus),
|
|
|
+ 'onus' => $serialNumbers
|
|
|
+ );
|
|
|
+ } catch (\Exception $ex){
|
|
|
ExceptionFactory::make($ex->getMessage());
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function checkContainerParameter(){
|
|
|
- if(!$this->serviceContainer->hasParameter('client')){
|
|
|
- ExceptionFactory::make("Don't exists parameters 'client' to search the webservice");
|
|
|
+ private function execute($onuId, $transition){
|
|
|
+ $cmd_args = [
|
|
|
+ 'entity' => '--entity:FTTHBundle\\Entity\\ONU',
|
|
|
+ 'workflow' => '--workflow:onu_workflow',
|
|
|
+ 'transition' => "--transition:{$transition}",
|
|
|
+ 'id' => "--id:{$onuId}"
|
|
|
+ ];
|
|
|
+
|
|
|
+ $this->runCommand('workflow:apply', $cmd_args);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function runCommand($name, $cmd_args = array())
|
|
|
+ {
|
|
|
+
|
|
|
+ $kernel = $this->container->get('kernel');
|
|
|
+
|
|
|
+ $application = new Application($kernel);
|
|
|
+ $application->setAutoExit(false);
|
|
|
+
|
|
|
+ $args = [
|
|
|
+ '',
|
|
|
+ 'amqp:remote',
|
|
|
+ $name,
|
|
|
+ '--route=' . getenv("AMQP_KEY"),
|
|
|
+ ];
|
|
|
+ foreach ($cmd_args as $cmd_arg) {
|
|
|
+ $args[] = "--args={$cmd_arg}";
|
|
|
}
|
|
|
+ $input = new ArgvInput($args);
|
|
|
+
|
|
|
+ $output = new BufferedOutput();
|
|
|
+
|
|
|
+ $application->run($input, $output);
|
|
|
+
|
|
|
+ return $output->fetch();
|
|
|
}
|
|
|
}
|