Passed
Pull Request — 1.1 (#1122)
by Tim
07:39
created

EntityManagerFactory::factory()   B

Complexity

Conditions 8
Paths 35

Size

Total Lines 64
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 35
nop 0
dl 0
loc 64
rs 8.4444
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * AppserverIo\Appserver\Ldap\EntityManagerFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\Ldap;
22
23
use Symfony\Component\Ldap\Ldap;
24
use AppserverIo\Appserver\Ldap\Symfony\LdapClient;
25
use AppserverIo\Psr\Application\ApplicationInterface;
26
use AppserverIo\Appserver\Doctrine\Utils\ConnectionUtil;
27
use AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface;
28
29
/**
30
 * Factory implementation for a Symfony LDAP Adapter instance.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/appserver-io/appserver
36
 * @link      http://www.appserver.io
37
 */
38
class EntityManagerFactory
39
{
40
41
    /**
42
     * The application instance.
43
     *
44
     * @var \AppserverIo\Psr\Application\ApplicationInterface
45
     */
46
    protected $application;
47
48
    /**
49
     * The persistence unit configuration instance.
50
     *
51
     *  @var \AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface
52
     */
53
    protected $persistenceUnitNode;
54
55
    /**
56
     *
57
     * @param \AppserverIo\Psr\Application\ApplicationInterface                            $application         The application instance
58
     * @param \AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface $persistenceUnitNode The persistence unit configuration node
59
     */
60
    public function __construct(
61
        ApplicationInterface $application,
62
        PersistenceUnitConfigurationInterface $persistenceUnitNode
63
    ) {
64
        $this->application = $application;
65
        $this->persistenceUnitNode = $persistenceUnitNode;
66
    }
67
68
    /**
69
     * Return's the application instance.
70
     *
71
     * @return \AppserverIo\Psr\Application\ApplicationInterface The application instance
72
     */
73
    protected function getApplication()
74
    {
75
        return $this->application;
76
    }
77
78
    /**
79
     * Return's the persistence unit configuration instance.
80
     *
81
     * @return \AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface The configuration instance
82
     */
83
    protected function getPersistenceUnitNode()
84
    {
85
        return $this->persistenceUnitNode;
86
    }
87
88
    /**
89
     * Creates a new Symfony LDAP adapter instance based on the passed configuration.
90
     *
91
     * @return object The Symfony LDAP Adapter instance
92
     */
93
    public function factory()
94
    {
95
96
        // load the application and persistence unit configuration
97
        $application = $this->getApplication();
98
        $persistenceUnitNode = $this->getPersistenceUnitNode();
99
100
        // query whether or not an initialize EM configuration is available
101
        if ($application->hasAttribute($persistenceUnitNode->getName()) === false) {
0 ignored issues
show
Bug introduced by
The method hasAttribute() does not exist on AppserverIo\Psr\Application\ApplicationInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ApplicationInterface such as AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        if ($application->/** @scrutinizer ignore-call */ hasAttribute($persistenceUnitNode->getName()) === false) {
Loading history...
102
            // load the datasource node
103
            $datasourceNode = null;
104
            foreach ($application->getInitialContext()->getSystemConfiguration()->getDatasources() as $datasourceNode) {
0 ignored issues
show
Bug introduced by
The method getInitialContext() does not exist on AppserverIo\Psr\Application\ApplicationInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ApplicationInterface such as AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
            foreach ($application->/** @scrutinizer ignore-call */ getInitialContext()->getSystemConfiguration()->getDatasources() as $datasourceNode) {
Loading history...
105
                if ($datasourceNode->getName() === $persistenceUnitNode->getDatasource()->getName()) {
106
                    break;
107
                }
108
            }
109
110
            // throw a exception if the configured datasource is NOT available
111
            if ($datasourceNode == null) {
112
                throw new \Exception(sprintf('Can\'t find a datasource node for persistence unit %s', $persistenceUnitNode->getName()));
113
            }
114
115
            // load the database node
116
            $databaseNode = $datasourceNode->getDatabase();
117
118
            // throw an exception if the configured database is NOT available
119
            if ($databaseNode == null) {
120
                throw new \Exception(sprintf('Can\'t find database node for persistence unit %s', $persistenceUnitNode->getName()));
121
            }
122
123
            // load the driver node
124
            $driverNode = $databaseNode->getDriver();
125
126
            // throw an exception if the configured driver is NOT available
127
            if ($driverNode == null) {
128
                throw new \Exception(sprintf('Can\'t find driver node for persistence unit %s', $persistenceUnitNode->getName()));
129
            }
130
131
            // load the connection parameters
132
            $connectionParameters = ConnectionUtil::get($application)->fromDatabaseNode($databaseNode);
133
134
            // append the initialized EM configuration to the application
135
            $application->setAttribute($persistenceUnitNode->getName(), array($connectionParameters));
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on AppserverIo\Psr\Application\ApplicationInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ApplicationInterface such as AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
            $application->/** @scrutinizer ignore-call */ 
136
                          setAttribute($persistenceUnitNode->getName(), array($connectionParameters));
Loading history...
136
        }
137
138
        try {
139
            // load the initialized EM configuration from the application
140
            list ($connectionParameters) = $application->getAttribute($persistenceUnitNode->getName());
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on AppserverIo\Psr\Application\ApplicationInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ApplicationInterface such as AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
            /** @scrutinizer ignore-call */ 
141
            list ($connectionParameters) = $application->getAttribute($persistenceUnitNode->getName());
Loading history...
141
142
            // initialize the LDAP client
143
            $ldapClient = new LdapClient(
144
                Ldap::create($connectionParameters['driver'], [
145
                    'host' => $connectionParameters['host'],
146
                    'port' => $connectionParameters['port']
147
                ])
148
            );
149
150
            // bind the LDAP adapter to the given credentials
151
            $ldapClient->bind($connectionParameters['user'], $connectionParameters['password']);
152
153
            // initialize and return the bound LDAP entity manager
154
            return new EntityManager($ldapClient, $application, $persistenceUnitNode);
155
        } catch (\Exception $e) {
156
            error_log($e->__toString());
157
        }
158
    }
159
}
160