| Conditions | 8 |
| Paths | 35 |
| Total Lines | 64 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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) { |
||
|
|
|||
| 102 | // load the datasource node |
||
| 103 | $datasourceNode = null; |
||
| 104 | foreach ($application->getInitialContext()->getSystemConfiguration()->getDatasources() as $datasourceNode) { |
||
| 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)); |
||
| 136 | } |
||
| 137 | |||
| 138 | try { |
||
| 139 | // load the initialized EM configuration from the application |
||
| 140 | list ($connectionParameters) = $application->getAttribute($persistenceUnitNode->getName()); |
||
| 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 | } |
||
| 160 |