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

Service   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 89.74%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 9
c 2
b 1
f 1
lcom 1
cbo 6
dl 0
loc 79
ccs 35
cts 39
cp 0.8974
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getDoctrine() 0 4 1
A get() 0 11 2
B set() 0 47 5
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