Completed
Push — master ( 3a87d5...4e429b )
by Kirill
04:26
created

Service   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 6.51%

Importance

Changes 0
Metric Value
dl 0
loc 94
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7
ccs 3
cts 46
cp 0.0651
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDoctrine() 0 4 1
A executeVirtual() 0 4 1
A executeReal() 0 4 1
B execute() 0 53 6
A runAction() 0 13 3
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 4
    public function __construct(Registry $doctrine)
22
    {
23 4
        $this->doctrine = $doctrine;
24 4
    }
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
            $executor->setParameters($changeSet);
70
71
            try {
72
                $result = $executor->{$method}($action);
73
            } catch (\Exception $exception) {
74
                $result = $exception->getMessage();
75
            }
76
            $changeSet['result']=$result;
77
        }
78
79
        $state = new ActionHistory();
80
        $state->setAction($action);
81
        $state->setTime(new \DateTime());
82
        $state->setSource($source);
83
        $state->setPerformed(!$virtual);
84
        $state->setChangeSet(json_encode($changeSet));
85
86
        $device = $action->getDevice()->setState($changeSet);
87
88
        $this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state);
89
        $this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device);
90
91
        $this->getDoctrine()->getManager()->flush();
92
93
        return $this;
94
    }
95
96
    public function runAction($actionName, array $parameters)
97
    {
98
        $action = $this->doctrine->getRepository('AppBundle:Action')->findOneBy(['name'=>$actionName]);
99
        if (!$action) {
100
            throw new \Exception("Action ".$actionName." not found");
101
        }
102
103
104
        if (isset($parameter['container'])) {
0 ignored issues
show
Bug introduced by
The variable $parameter does not exist. Did you mean $parameters?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
105
            $args['container'] = $parameter['container'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
106
        }
107
        $this->executeReal($action, "internalcall", $parameters);
108
    }
109
}
110