Movement::analyze()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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