Completed
Push — master ( 2322ba...25df9e )
by Kirill
04:32
created

Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: chrl
5
 * Date: 04/03/17
6
 * Time: 17:49
7
 */
8
9
namespace AppBundle\Action;
10
11
use AppBundle\Entity\Action;
12
use AppBundle\Entity\ActionHistory;
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
15
class Service
16
{
17
18
    private $doctrine;
19
20
    public function __construct(Registry $doctrine)
21
    {
22
        $this->doctrine = $doctrine;
23
    }
24
25
    private function getDoctrine()
26
    {
27
        return $this->doctrine;
28
    }
29
30
31
    public function executeVirtual(Action $action, $source, array $changeSet)
32
    {
33
        return $this->execute($action, $source, $changeSet, true);
34
    }
35
36
    public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false)
37
    {
38
39
        //TODO: Find and run executor here
40
41
        $state = new ActionHistory();
42
        $state->setAction($action);
43
        $state->setTime(new \DateTime());
44
        $state->setSource($source);
45
        $state->setPerformed(!$virtual);
46
        $state->setChangeSet(json_encode($changeSet));
47
48
        $device = $action->getDevice()->setState($changeSet);
49
50
        $this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state);
51
        $this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device);
52
53
        $this->getDoctrine()->getManager()->flush();
54
55
        return $this;
56
    }
57
}
58