Completed
Push — master ( e657da...2322ba )
by Kirill
04:30
created

Service::set()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6.1169

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 49
ccs 23
cts 27
cp 0.8519
rs 8.5906
cc 6
eloc 27
nc 6
nop 2
crap 6.1169
1
<?php
2
3
namespace AppBundle\Variable;
4
5
use AppBundle\Entity\Variable;
6
use AppBundle\Entity\VariableHistory;
7
use AppBundle\Variable\Parser\ParserInterface;
8
use Doctrine\Bundle\DoctrineBundle\Registry;
9
use Doctrine\ORM\EntityManager;
10
use Symfony\Component\Config\Definition\Exception\Exception;
11
12
class Service
13
{
14
15
    private $doctrine;
16
    private $needSync = false;
17
    private $syncHost;
18
19 4
    public function __construct(Registry $doctrine, $needSync, $syncHost)
20
    {
21 4
        $this->doctrine = $doctrine;
22 4
        $this->needSync = $needSync;
23 4
        $this->syncHost = $syncHost;
24 4
    }
25
26 4
    private function getDoctrine()
27
    {
28 4
        return $this->doctrine;
29
    }
30
31 4
    public function get($varName)
32
    {
33 4
        $vars = $this->getDoctrine()->getRepository('AppBundle:Variable');
34
35
        /** @var Variable $var */
36 4
        $var = $vars->findOneBy(['name'=>$varName]);
37 4
        if (!$var) {
38 1
            throw new Exception('Variable '.$varName.' not found');
39
        }
40 3
        return $var;
41
    }
42
43 2
    public function set($varName, $value)
44
    {
45 2
        $vars = $this->getDoctrine()->getRepository('AppBundle:Variable');
46
47
        /** @var Variable $var */
48 2
        $var = $vars->findOneBy(['name'=>$varName]);
49 2
        if (!$var) {
50
            throw new Exception('Variable '.$varName.' not found');
51
        }
52
53 2
        $parser = 'AppBundle\Variable\Parser\\'.ucfirst($var->getParser());
54
55 2
        if (!class_exists($parser)) {
56
            throw new Exception('Unknown parser: '.$parser);
57
        }
58
59
        /** @var ParserInterface $parser */
60 2
        $parser = new $parser();
61
62 2
        $value = $parser->parse($value);
63
64 2
        if (!$value) {
65 1
            return false;
66
        }
67
68 2
        if ($this->needSync) {
69
            if ($var->needSync) {
70
                @file_get_contents($this->syncHost . 'set/' . $varName . '?value=' . $value);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
71
            }
72
        }
73
74 2
        $var->setValue($value);
75 2
        $var->setLaststatus(200);
76 2
        $var->setLastupdate(new \DateTime());
77
78 2
        $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->persist($var);
79
80 2
        $state = new VariableHistory();
81 2
        $state->setVar($var);
82 2
        $state->setTime(new \DateTime());
83 2
        $state->setValue($value);
84
85 2
        $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->persist($state);
86
87 2
        $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->flush();
88 2
        $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->flush();
89
90 2
        return $value;
91
    }
92
93
    /**
94
     * @param Variable $variable
95
     * @return array
96
     */
97
    public function getDayHistory(Variable $variable)
98
    {
99
        /** @var EntityManager $em */
100
        $em = $this->getDoctrine()->getManager();
101
102
        $q = $em->createQueryBuilder();
103
        $res = $q->
104
            select('AVG(vh.value) as av')->
105
            addSelect('DATE_FORMAT(vh.time,\'%Y-%m-%d %H:00\') as df')->
106
            from('AppBundle:VariableHistory', 'vh')->
107
            where('vh.time >= :date')->
108
            setParameter('date', new \DateTime('-48 hour'))->
109
            andWhere('vh.var = :var_id')->
110
            setParameter('var_id', $variable->getId())->
111
            groupBy('df')->
112
            orderBy('df', 'asc')->
113
            getQuery();
114
115
        return $res->getArrayResult();
116
    }
117
}
118