1 | <?php |
||
13 | class Service |
||
14 | { |
||
15 | |||
16 | private $doctrine; |
||
17 | private $needSync = false; |
||
18 | private $syncHost; |
||
19 | private $actionService; |
||
20 | |||
21 | 5 | public function __construct(Registry $doctrine, \AppBundle\Action\Service $actionService, $needSync, $syncHost) |
|
28 | |||
29 | 5 | private function getDoctrine() |
|
33 | |||
34 | 5 | public function get($varName) |
|
45 | |||
46 | 3 | public function set($varName, $value) |
|
47 | { |
||
48 | 3 | $vars = $this->getDoctrine()->getRepository('AppBundle:Variable'); |
|
49 | |||
50 | /** @var Variable $var */ |
||
51 | 3 | $var = $vars->findOneBy(['name'=>$varName]); |
|
52 | 3 | if (!$var) { |
|
53 | throw new Exception('Variable '.$varName.' not found'); |
||
54 | } |
||
55 | |||
56 | 3 | $parser = 'AppBundle\Variable\Parser\\'.ucfirst($var->getParser()); |
|
57 | |||
58 | 3 | if (!class_exists($parser)) { |
|
59 | throw new Exception('Unknown parser: '.$parser); |
||
60 | } |
||
61 | |||
62 | /** @var ParserInterface $parser */ |
||
63 | 3 | $parser = new $parser(); |
|
64 | |||
65 | 3 | $value = $parser->parse($value); |
|
66 | |||
67 | 3 | if (!$value) { |
|
68 | 2 | return false; |
|
69 | } |
||
70 | |||
71 | 3 | if ($this->needSync) { |
|
72 | if ($var->needSync) { |
||
73 | @file_get_contents($this->syncHost.'set/'.$varName.'?value='.$value); |
||
|
|||
74 | } |
||
75 | } |
||
76 | |||
77 | 3 | $var->setValue($value); |
|
78 | 3 | $var->setLaststatus(200); |
|
79 | 3 | $var->setLastupdate(new \DateTime()); |
|
80 | |||
81 | 3 | $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->persist($var); |
|
82 | |||
83 | 3 | if ($var->needHistory) { |
|
84 | 3 | $state = new VariableHistory(); |
|
85 | 3 | $state->setVar($var); |
|
86 | 3 | $state->setTime(new \DateTime()); |
|
87 | 3 | $state->setValue($value); |
|
88 | |||
89 | 3 | $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->persist($state); |
|
90 | 3 | $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->flush(); |
|
91 | } |
||
92 | |||
93 | 3 | $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->flush(); |
|
94 | |||
95 | // hooks run |
||
96 | |||
97 | 3 | $hooks = $this->getDoctrine()->getManager()->getRepository('AppBundle:VarHook')->findBy(['variable'=>$var]); |
|
98 | |||
99 | 3 | foreach ($hooks as $varHook) { |
|
100 | 1 | list($executor, $method) = explode(':', $varHook->getExecutor()); |
|
101 | |||
102 | 1 | $executor = 'AppBundle\Action\Executor\\'.ucfirst($executor); |
|
103 | |||
104 | 1 | if (!class_exists($executor)) { |
|
105 | throw new \Exception('Unknown executor: '.$executor); |
||
106 | } |
||
107 | |||
108 | /** @var ExecutorInterface $executor */ |
||
109 | 1 | $executor = new $executor(); |
|
110 | 1 | $executor->setDoctrine($this->getDoctrine()); |
|
111 | |||
112 | |||
113 | 1 | if (!method_exists($executor, $method)) { |
|
114 | throw new \Exception('Unknown executor method: '.$varHook->getExecutor().'()'); |
||
115 | } |
||
116 | |||
117 | 1 | $executor->setParameters(json_decode($varHook->getArguments(), true)); |
|
118 | |||
119 | try { |
||
120 | 1 | $result = $executor->{$method}($varHook); |
|
121 | } catch (\Exception $exception) { |
||
122 | $result = $exception->getMessage(); |
||
123 | } |
||
124 | |||
125 | 1 | if ($varHook->getType() == 'decider') { |
|
126 | 1 | if ($result === true) { |
|
127 | 1 | $this->actionService->executeReal( |
|
128 | 1 | $varHook->getAction(), |
|
129 | 1 | 'hook', |
|
130 | 1 | json_decode($varHook->getArguments(), true) |
|
131 | ); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | 3 | return $value; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param Variable $variable |
||
141 | * @return array |
||
142 | */ |
||
143 | public function getDayHistory(Variable $variable) |
||
163 | |||
164 | /** |
||
165 | * @param Variable $variable |
||
166 | * @return array |
||
167 | */ |
||
168 | public function getLastValue(Variable $variable) |
||
186 | } |
||
187 |
If you suppress an error, we recommend checking for the error condition explicitly: