|
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
|
|
|
|
|
21
|
|
|
public function __construct(Registry $doctrine) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->doctrine = $doctrine; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
private function getDoctrine() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->doctrine; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
public function executeVirtual(Action $action, $source, array $changeSet) |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->execute($action, $source, $changeSet, true); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function executeReal(Action $action, $source, $arguments) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->execute($action, $source, $arguments, false); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false) |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
//TODO: Find and run executor here |
|
46
|
|
|
|
|
47
|
|
|
if (!$virtual) { |
|
48
|
|
|
list($executor, $method) = explode(':', $action->getExecutor()); |
|
49
|
|
|
|
|
50
|
|
|
$executor = 'AppBundle\Action\Executor\\' . ucfirst($executor); |
|
51
|
|
|
|
|
52
|
|
|
if (!class_exists($executor)) { |
|
53
|
|
|
throw new \Exception('Unknown executor: ' . $executor); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @var ExecutorInterface $executor */ |
|
57
|
|
|
$executor = new $executor(); |
|
58
|
|
|
$executor->setDoctrine($this->getDoctrine()); |
|
59
|
|
|
|
|
60
|
|
|
if (isset($changeSet['container'])) { |
|
61
|
|
|
$executor->setContainer($changeSet['container']); |
|
62
|
|
|
unset($changeSet['container']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!method_exists($executor, $method)) { |
|
66
|
|
|
throw new \Exception('Unknown executor method: ' . $action->getExecutor().'()'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$result = $executor->{$method}($action); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$state = new ActionHistory(); |
|
73
|
|
|
$state->setAction($action); |
|
74
|
|
|
$state->setTime(new \DateTime()); |
|
75
|
|
|
$state->setSource($source); |
|
76
|
|
|
$state->setPerformed(!$virtual); |
|
77
|
|
|
$state->setChangeSet(json_encode($changeSet)); |
|
78
|
|
|
|
|
79
|
|
|
$device = $action->getDevice()->setState($changeSet); |
|
80
|
|
|
|
|
81
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state); |
|
82
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device); |
|
83
|
|
|
|
|
84
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.