1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: chrl |
5
|
|
|
* Date: 04/03/17 |
6
|
|
|
* Time: 17:49 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace AppBundle\Action; |
10
|
|
|
|
11
|
|
|
use AppBundle\Action\Executor\ExecutorInterface; |
12
|
|
|
use AppBundle\Entity\Action; |
13
|
|
|
use AppBundle\Entity\ActionHistory; |
14
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
15
|
|
|
|
16
|
|
|
class Service |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private $doctrine; |
20
|
|
|
private $container; |
21
|
|
|
|
22
|
5 |
|
public function __construct(Registry $doctrine, $container) |
23
|
|
|
{ |
24
|
5 |
|
$this->doctrine = $doctrine; |
25
|
5 |
|
$this->container = $container; |
26
|
5 |
|
} |
27
|
|
|
|
28
|
1 |
|
private function getDoctrine() |
29
|
|
|
{ |
30
|
1 |
|
return $this->doctrine; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function executeVirtual(Action $action, $source, array $changeSet) |
35
|
|
|
{ |
36
|
|
|
return $this->execute($action, $source, $changeSet, true); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $source |
41
|
|
|
* @return Service |
42
|
|
|
*/ |
43
|
1 |
|
public function executeReal(Action $action, $source, $arguments) |
44
|
|
|
{ |
45
|
1 |
|
return $this->execute($action, $source, $arguments, false); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false) |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
//TODO: Find and run executor here |
52
|
|
|
|
53
|
1 |
|
if (!$virtual) { |
54
|
1 |
|
list($executor, $method) = explode(':', $action->getExecutor()); |
55
|
|
|
|
56
|
1 |
|
$executor = 'AppBundle\Action\Executor\\'.ucfirst($executor); |
57
|
|
|
|
58
|
1 |
|
if (!class_exists($executor)) { |
59
|
|
|
throw new \Exception('Unknown executor: '.$executor); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var ExecutorInterface $executor */ |
63
|
1 |
|
$executor = new $executor(); |
64
|
1 |
|
$executor->setDoctrine($this->getDoctrine()); |
65
|
|
|
|
66
|
1 |
|
$executor->setContainer($this->container); |
67
|
1 |
|
unset($changeSet['container']); |
68
|
|
|
|
69
|
|
|
|
70
|
1 |
|
if (!method_exists($executor, $method)) { |
71
|
|
|
throw new \Exception('Unknown executor method: '.$action->getExecutor().'()'); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$executor->setParameters($changeSet); |
75
|
|
|
|
76
|
|
|
try { |
77
|
1 |
|
$result = $executor->{$method}($action); |
78
|
|
|
} catch (\Exception $exception) { |
79
|
|
|
$result = $exception->getMessage(); |
80
|
|
|
} |
81
|
1 |
|
$changeSet['result'] = $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$state = new ActionHistory(); |
85
|
1 |
|
$state->setAction($action); |
86
|
1 |
|
$state->setTime(new \DateTime()); |
87
|
1 |
|
$state->setSource($source); |
88
|
1 |
|
$state->setPerformed(!$virtual); |
89
|
1 |
|
$state->setChangeSet(json_encode($changeSet)); |
90
|
|
|
|
91
|
1 |
|
$device = $action->getDevice()->setState($changeSet); |
92
|
|
|
|
93
|
1 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state); |
94
|
1 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device); |
95
|
|
|
|
96
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
97
|
|
|
|
98
|
1 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function runAction($actionName, array $parameters) |
102
|
|
|
{ |
103
|
|
|
$action = $this->doctrine->getRepository('AppBundle:Action')->findOneBy(['name'=>$actionName]); |
104
|
|
|
if (!$action) { |
105
|
|
|
throw new \Exception("Action ".$actionName." not found"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
if (isset($parameter['container'])) { |
|
|
|
|
110
|
|
|
$args['container'] = $parameter['container']; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
$this->executeReal($action, "internalcall", $parameters); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.