Completed
Push — master ( 289c61...45ac24 )
by Kirill
03:32
created

Movement::analyze()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 19
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 1
crap 20
1
<?php
2
3
namespace AppBundle\Action\Executor;
4
5
use AppBundle\Entity\Action;
6
use AppBundle\Entity\Variable;
7
8
class Movement extends BaseExecutor implements ExecutorInterface
9
{
10
    public function analyze(Action $action)
11
    {
12
        $varService = $this->getContainer()->get('vars');
13
14
        $params = $action->getDevice()->getParams();
15
16
        /** @var Variable $variable */
17
        $variable = false;
18
19
        foreach ($params as $param) {
20
            list($p,$v) = explode(':', $param);
21
            if ($p=='variable') {
22
                $variable = $this->getDoctrine()->getManager()->getRepository('AppBundle:Variable')->findOneBy(['name'=>$v]);
23
                break;
24
            }
25
        }
26
27
        if (!$variable) {
28
            throw new \Exception("Sensor variable for device '".$action->getDevice()->getName()."' not found :(");
29
        }
30
31
        $lastValue = $varService->getLastValue($variable);
32
        /** @var \DateTime $lastPresence */
33
        $lastPresence = $lastValue['df'];
34
        $varService->set('room.lastPresence', time() - $lastPresence->getTimestamp());
35
    }
36
}
37