Completed
Push — master ( ea3c7e...ca6541 )
by Kirill
04:42
created

Service::getDoctrine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 5
    public function __construct(Registry $doctrine)
22
    {
23 5
        $this->doctrine = $doctrine;
24 5
    }
25
26 1
    private function getDoctrine()
27
    {
28 1
        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
    /**
38
     * @param string $source
39
     */
40 1
    public function executeReal(Action $action, $source, $arguments)
41
    {
42 1
        return $this->execute($action, $source, $arguments, false);
43
    }
44
45 1
    public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false)
46
    {
47
48
        //TODO: Find and run executor here
49
50 1
        if (!$virtual) {
51 1
            list($executor, $method) = explode(':', $action->getExecutor());
52
53 1
            $executor = 'AppBundle\Action\Executor\\'.ucfirst($executor);
54
55 1
            if (!class_exists($executor)) {
56
                throw new \Exception('Unknown executor: '.$executor);
57
            }
58
59
            /** @var ExecutorInterface $executor */
60 1
            $executor = new $executor();
61 1
            $executor->setDoctrine($this->getDoctrine());
62
63 1
            if (isset($changeSet['container'])) {
64
                $executor->setContainer($changeSet['container']);
65
                unset($changeSet['container']);
66
            }
67
68 1
            if (!method_exists($executor, $method)) {
69
                throw new \Exception('Unknown executor method: '.$action->getExecutor().'()');
70
            }
71
72 1
            $executor->setParameters($changeSet);
73
74
            try {
75 1
                $result = $executor->{$method}($action);
76
            } catch (\Exception $exception) {
77
                $result = $exception->getMessage();
78
            }
79 1
            $changeSet['result'] = $result;
80
        }
81
82 1
        $state = new ActionHistory();
83 1
        $state->setAction($action);
84 1
        $state->setTime(new \DateTime());
85 1
        $state->setSource($source);
86 1
        $state->setPerformed(!$virtual);
87 1
        $state->setChangeSet(json_encode($changeSet));
88
89 1
        $device = $action->getDevice()->setState($changeSet);
90
91 1
        $this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state);
92 1
        $this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device);
93
94 1
        $this->getDoctrine()->getManager()->flush();
95
96 1
        return $this;
97
    }
98
99
    public function runAction($actionName, array $parameters)
100
    {
101
        $action = $this->doctrine->getRepository('AppBundle:Action')->findOneBy(['name'=>$actionName]);
102
        if (!$action) {
103
            throw new \Exception("Action ".$actionName." not found");
104
        }
105
106
107
        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...
108
            $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...
109
        }
110
        $this->executeReal($action, "internalcall", $parameters);
111
    }
112
}
113