Completed
Push — master ( 0f4a7b...cb1ba7 )
by Kirill
12:53
created

Service::set()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0384

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 47
ccs 23
cts 26
cp 0.8846
rs 8.5125
cc 5
eloc 26
nc 5
nop 2
crap 5.0384
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
            @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...
70
        }
71
72 2
        $var->setValue($value);
73 2
        $var->setLaststatus(200);
74 2
        $var->setLastupdate(new \DateTime());
75
76 2
        $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->persist($var);
77
78 2
        $state = new VariableHistory();
79 2
        $state->setVar($var);
80 2
        $state->setTime(new \DateTime());
81 2
        $state->setValue($value);
82
83 2
        $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->persist($state);
84
85 2
        $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->flush();
86 2
        $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->flush();
87
88 2
        return $value;
89
    }
90
91
    /**
92
     * @param Variable $variable
93
     * @return array
94
     */
95
    public function getDayHistory(Variable $variable)
96
    {
97
        /** @var EntityManager $em */
98
        $em = $this->getDoctrine()->getManager();
99
100
        $q = $em->createQueryBuilder();
101
        $res = $q->
102
            select('AVG(vh.value) as av')->
103
            addSelect('DATE_FORMAT(vh.time,\'%Y-%m-%d %H:%i\') as df')->
104
            from('AppBundle:VariableHistory', 'vh')->
105
            where('vh.time >= :date')->
106
            setParameter('date', new \DateTime('-24 hour'))->
107
            andWhere('vh.var = :var_id')->
108
            setParameter('var_id', $variable->getId())->
109
            groupBy('df')->
110
            orderBy('df', 'asc')->
111
            getQuery();
112
113
        return $res->getArrayResult();
114
    }
115
}
116