Service   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 69.57%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 99
ccs 32
cts 46
cp 0.6957
rs 10

6 Methods

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