Passed
Push — master ( 94702c...0299cb )
by Oleg
04:57
created

Procedure::setConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrs\StoredProcedureBundle\Utils;
4
5
use Doctrine\Common\Collections\ArrayCollection;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Collections\ArrayCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrs\StoredProcedureBundle\Utils\ArrayCollection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Doctrs\StoredProcedureBundle\Event\ChangeConnectionEvent;
7
use Doctrs\StoredProcedureBundle\Event\ChangeConnectionNameEvent;
8
use Doctrs\StoredProcedureBundle\Event\ChangeResultEvent;
9
use Doctrs\StoredProcedureBundle\Event\StoredProcedureEvents;
10
use PgFunc\Configuration;
11
use PgFunc\Connection;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
14
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\EventDispatcher\EventDispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class Procedure implements ProcedureInterface
0 ignored issues
show
Bug introduced by
The type Doctrs\StoredProcedureBu...tils\ProcedureInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
{
19
    /**
20
     * @var ArrayCollection
21
     */
22
    private $configurations;
23
24
    /**
25
     * @var ArrayCollection
26
     */
27
    private $connections;
28
29
    /**
30
     * @var EventDispatcher
31
     */
32
    private $dispatcher;
33
34
    /**
35
     * Procedure constructor.
36
     *
37
     * @param EventDispatcher $dispatcher
38
     */
39
    public function __construct(ContainerInterface $container)
40
    {
41
        $this->dispatcher = $container->get('event_dispatcher');
42
        $this->configurations = new ArrayCollection();
43
        $this->connections = new ArrayCollection();
44
    }
45
46
    /**
47
     * @param string            $connection
48
     * @param \PgFunc\Procedure $procedure
49
     *
50
     * @return mixed
51
     *
52
     * @throws \PgFunc\Exception
53
     * @throws ParameterNotFoundException
54
     */
55
    public function execute($connection, \PgFunc\Procedure $procedure)
56
    {
57
        $event = new ChangeConnectionNameEvent($connection);
58
        $this->dispatcher->dispatch(StoredProcedureEvents::CONNECTION_NAME, $event);
59
60
        $connection = $event->getName();
61
        if (!$this->configurations->get($connection)) {
62
            throw new ParameterNotFoundException(
63
                sprintf('Configuration "%s" not found in configuration pool', $connection)
64
            );
65
        }
66
67
        if (!$this->connections->get($connection)) {
68
            $this->connections->set($connection, new Connection($this->configurations->get($connection)));
69
        }
70
71
        /** @var Connection $connection */
72
        $connection = $this->connections->get($connection);
73
74
        // Connection event
75
        $event = new ChangeConnectionEvent($connection, $procedure);
76
        $this->dispatcher->dispatch(StoredProcedureEvents::CONNECTION, $event);
77
78
        $connection = $event->getConnection();
79
        $procedure = $event->getProcedure();
80
81
        $result = $connection->queryProcedure($procedure);
82
83
        // Result Event
84
        $event = new ChangeResultEvent($result);
85
        $this->dispatcher->dispatch(StoredProcedureEvents::RESULT, $event);
86
87
        return $event->getResult();
88
    }
89
90
    /**
91
     * @param string $key
92
     * @param array  $connectionData
93
     *
94
     * @return ProcedureInterface
95
     *
96
     * @throws BadMethodCallException
97
     */
98
    public function setConfiguration($key, array $connectionData): ProcedureInterface
99
    {
100
        $configuration = new \PgFunc\Configuration();
101
102
        foreach ($connectionData as $method => $value) {
103
            $method = 'set'.str_replace('_', '', ucwords($method, '_'));
104
105
            if (!method_exists($configuration, $method)) {
106
                throw new BadMethodCallException(
107
                    sprintf('Method "%s" not found in class "%s"', $method, get_class($configuration))
108
                );
109
            }
110
111
            $configuration->$method($value);
112
        }
113
114
        $this->configurations->set($key, $configuration);
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param Configuration|string $configuration
121
     *
122
     * @return Procedure
123
     */
124
    public function removeConfiguration($configuration): ProcedureInterface
125
    {
126
        if ($configuration instanceof Configuration) {
127
            $this->configurations->removeElement($configuration);
128
        } else {
129
            $this->configurations->remove($configuration);
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * @param Connection $connection
137
     *
138
     * @return Procedure
139
     */
140
    public function setConnection($key, Connection $connection): ProcedureInterface
141
    {
142
        $this->connections->set($key, $connection);
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param Connection|string $connection
149
     *
150
     * @return Procedure
151
     */
152
    public function removeConnection($connection): ProcedureInterface
153
    {
154
        if ($connection instanceof Connection) {
155
            $this->connections->removeElement($connection);
156
        } else {
157
            $this->connections->remove($connection);
158
        }
159
160
        return $this;
161
    }
162
}
163