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

Service::execute()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 31
cp 0
rs 8.4751
c 0
b 0
f 0
cc 5
eloc 25
nc 6
nop 4
crap 30
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\Action\Executor\ExecutorInterface;
12
use AppBundle\Entity\Action;
13
use AppBundle\Entity\ActionHistory;
14
use Doctrine\Bundle\DoctrineBundle\Registry;
15
16
class Service
17
{
18
19
    private $doctrine;
20
21
    public function __construct(Registry $doctrine)
22
    {
23
        $this->doctrine = $doctrine;
24
    }
25
26
    private function getDoctrine()
27
    {
28
        return $this->doctrine;
29
    }
30
31
32
    public function executeVirtual(Action $action, $source, array $changeSet)
33
    {
34
        return $this->execute($action, $source, $changeSet, true);
35
    }
36
37
    public function executeReal(Action $action, $source, $arguments)
38
    {
39
        return $this->execute($action, $source, $arguments, false);
40
    }
41
42
    public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false)
43
    {
44
45
        //TODO: Find and run executor here
46
47
        if (!$virtual) {
48
            list($executor, $method) = explode(':', $action->getExecutor());
49
50
            $executor = 'AppBundle\Action\Executor\\' . ucfirst($executor);
51
52
            if (!class_exists($executor)) {
53
                throw new \Exception('Unknown executor: ' . $executor);
54
            }
55
56
            /** @var ExecutorInterface $executor */
57
            $executor = new $executor();
58
            $executor->setDoctrine($this->getDoctrine());
59
60
            if (isset($changeSet['container'])) {
61
                $executor->setContainer($changeSet['container']);
62
                unset($changeSet['container']);
63
            }
64
65
            if (!method_exists($executor, $method)) {
66
                throw new \Exception('Unknown executor method: ' . $action->getExecutor().'()');
67
            }
68
69
            $result = $executor->{$method}($action);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
        }
71
72
        $state = new ActionHistory();
73
        $state->setAction($action);
74
        $state->setTime(new \DateTime());
75
        $state->setSource($source);
76
        $state->setPerformed(!$virtual);
77
        $state->setChangeSet(json_encode($changeSet));
78
79
        $device = $action->getDevice()->setState($changeSet);
80
81
        $this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state);
82
        $this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device);
83
84
        $this->getDoctrine()->getManager()->flush();
85
86
        return $this;
87
    }
88
}
89