Completed
Push — master ( 4011c2...5e557d )
by Kirill
03:45
created

Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 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 Symfony\Component\Config\Definition\Exception\Exception;
10
11
class Service
12
{
13
14
    private $doctrine;
15
    private $needSync = false;
16
    private $syncHost;
17
18 1
    public function __construct(Registry $doctrine, $needSync, $syncHost)
19
    {
20 1
        $this->doctrine = $doctrine;
21 1
        $this->needSync = $needSync;
22 1
        $this->syncHost = $syncHost;
23 1
    }
24
25 1
    public function getDoctrine()
26
    {
27 1
        return $this->doctrine;
28
    }
29
30 1
    public function get($varName)
31
    {
32 1
        $vars = $this->getDoctrine()->getRepository('AppBundle:Variable');
33
34
        /** @var Variable $var */
35 1
        $var = $vars->findOneBy(['name'=>$varName]);
36 1
        if (!$var) {
37
            throw new Exception('Variable '.$varName.' not found');
38
        }
39 1
        return $var;
40
    }
41
42 1
    public function set($varName, $value)
43
    {
44 1
        $vars = $this->getDoctrine()->getRepository('AppBundle:Variable');
45
46
        /** @var Variable $var */
47 1
        $var = $vars->findOneBy(['name'=>$varName]);
48 1
        if (!$var) {
49
            throw new Exception('Variable '.$varName.' not found');
50
        }
51
52 1
        $parser = 'AppBundle\Variable\Parser\\'.ucfirst($var->getParser());
53
54 1
        if (!class_exists($parser)) {
55
            throw new Exception('Unknown parser: '.$parser);
56
        }
57
58
        /** @var ParserInterface $parser */
59 1
        $parser = new $parser();
60
61 1
        $value = $parser->parse($value);
62
63 1
        if (!$value) {
64 1
            return false;
65
        }
66
67 1
        if ($this->needSync) {
68
        	@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...
69
		}
70
71 1
        $var->setValue($value);
72 1
        $var->setLaststatus(200);
73 1
        $var->setLastupdate(new \DateTime());
74
75 1
        $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->persist($var);
76
77 1
        $state = new VariableHistory();
78 1
        $state->setVar($var);
79 1
        $state->setTime(new \DateTime());
80 1
        $state->setValue($value);
81
82 1
        $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->persist($state);
83
84 1
        $this->getDoctrine()->getManagerForClass('AppBundle:Variable')->flush();
85 1
        $this->getDoctrine()->getManagerForClass('AppBundle:VariableHistory')->flush();
86
87 1
        return $value;
88
    }
89
}
90