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
|
|
|
*/ |
42
|
1 |
|
public function executeReal(Action $action, $source, $arguments) |
43
|
|
|
{ |
44
|
1 |
|
return $this->execute($action, $source, $arguments, false); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false) |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
//TODO: Find and run executor here |
51
|
|
|
|
52
|
1 |
|
if (!$virtual) { |
53
|
1 |
|
list($executor, $method) = explode(':', $action->getExecutor()); |
54
|
|
|
|
55
|
1 |
|
$executor = 'AppBundle\Action\Executor\\'.ucfirst($executor); |
56
|
|
|
|
57
|
1 |
|
if (!class_exists($executor)) { |
58
|
|
|
throw new \Exception('Unknown executor: '.$executor); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @var ExecutorInterface $executor */ |
62
|
1 |
|
$executor = new $executor(); |
63
|
1 |
|
$executor->setDoctrine($this->getDoctrine()); |
64
|
|
|
|
65
|
1 |
|
$executor->setContainer($this->container); |
66
|
1 |
|
unset($changeSet['container']); |
67
|
|
|
|
68
|
|
|
|
69
|
1 |
|
if (!method_exists($executor, $method)) { |
70
|
|
|
throw new \Exception('Unknown executor method: '.$action->getExecutor().'()'); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$executor->setParameters($changeSet); |
74
|
|
|
|
75
|
|
|
try { |
76
|
1 |
|
$result = $executor->{$method}($action); |
77
|
|
|
} catch (\Exception $exception) { |
78
|
|
|
$result = $exception->getMessage(); |
79
|
|
|
} |
80
|
1 |
|
$changeSet['result'] = $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$state = new ActionHistory(); |
84
|
1 |
|
$state->setAction($action); |
85
|
1 |
|
$state->setTime(new \DateTime()); |
86
|
1 |
|
$state->setSource($source); |
87
|
1 |
|
$state->setPerformed(!$virtual); |
88
|
1 |
|
$state->setChangeSet(json_encode($changeSet)); |
89
|
|
|
|
90
|
1 |
|
$device = $action->getDevice()->setState($changeSet); |
91
|
|
|
|
92
|
1 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state); |
93
|
1 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device); |
94
|
|
|
|
95
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
96
|
|
|
|
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function runAction($actionName, array $parameters) |
101
|
|
|
{ |
102
|
|
|
$action = $this->doctrine->getRepository('AppBundle:Action')->findOneBy(['name'=>$actionName]); |
103
|
|
|
if (!$action) { |
104
|
|
|
throw new \Exception("Action ".$actionName." not found"); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
if (isset($parameter['container'])) { |
|
|
|
|
109
|
|
|
$args['container'] = $parameter['container']; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
$this->executeReal($action, "internalcall", $parameters); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.