Completed
Push — master ( 4011c2...5e557d )
by Kirill
03:45
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 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
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 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