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

Service   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 46.27%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 131
ccs 31
cts 67
cp 0.4627
rs 10
c 5
b 1
f 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getDoctrine() 0 4 1
A get() 0 11 2
C set() 0 51 7
A getDayHistory() 0 20 1
A getLastValue() 0 18 1
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
        if ($var->needHistory) {
81
            $state = new VariableHistory();
82
            $state->setVar($var);
83
            $state->setTime(new \DateTime());
84
            $state->setValue($value);
85
86
            $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->persist($state);
87
            $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->flush();
88
        }
89
90 2
        $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->flush();
91
92 2
        return $value;
93
    }
94
95
    /**
96
     * @param Variable $variable
97
     * @return array
98
     */
99
    public function getDayHistory(Variable $variable)
100
    {
101
        /** @var EntityManager $em */
102
        $em = $this->getDoctrine()->getManager();
103
104
        $q = $em->createQueryBuilder();
105
        $res = $q->
106
            select('AVG(vh.value) as av')->
107
            addSelect('DATE_FORMAT(vh.time,\'%Y-%m-%d %H:00\') as df')->
108
            from('AppBundle:VariableHistory', 'vh')->
109
            where('vh.time >= :date')->
110
            setParameter('date', new \DateTime('-48 hour'))->
111
            andWhere('vh.var = :var_id')->
112
            setParameter('var_id', $variable->getId())->
113
            groupBy('df')->
114
            orderBy('df', 'asc')->
115
            getQuery();
116
117
        return $res->getArrayResult();
118
    }
119
120
    /**
121
     * @param Variable $variable
122
     * @return array
123
     */
124
    public function getLastValue(Variable $variable)
125
    {
126
        /** @var EntityManager $em */
127
        $em = $this->getDoctrine()->getManager();
128
129
        $q = $em->createQueryBuilder();
130
        $res = $q->
131
        select('vh.value as av')->
132
        addSelect('vh.time as df')->
133
        from('AppBundle:VariableHistory', 'vh')->
134
        where('vh.var = :var_id')->
135
        setParameter('var_id', $variable->getId())->
136
        orderBy('df', 'desc')->
137
        setMaxResults(1)->
138
        getQuery();
139
140
        return $res->getSingleResult();
141
    }
142
}
143