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

LdapManager::injectManagerSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * AppserverIo\Appserver\Ldap\LdapManager
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 AppserverIo\Appserver\Core\AbstractManager;
24
use AppserverIo\Appserver\Core\Environment;
25
use AppserverIo\Appserver\Core\Utilities\EnvironmentKeys;
26
use AppserverIo\Psr\Servlet\SessionUtils;
27
use AppserverIo\Psr\Di\ObjectManagerInterface;
28
use AppserverIo\Psr\Application\ApplicationInterface;
29
use AppserverIo\Appserver\Application\Interfaces\ManagerSettingsInterface;
30
use AppserverIo\Appserver\Application\Interfaces\ManagerSettingsAwareInterface;
31
use AppserverIo\Ldap\LdapManagerInterface;
32
use AppserverIo\Ldap\Description\EntityDescriptorInterface;
33
use AppserverIo\Ldap\Description\LdapDescriptorInterface;
34
use AppserverIo\Ldap\Description\RepositoryDescriptorInterface;
35
36
/**
37
 * The LDAP manager is necessary to load and provides information about all
38
 * LDAP objects related with the application itself.
39
 *
40
 * @author    Tim Wagner <[email protected]>
41
 * @copyright 2019 TechDivision GmbH <[email protected]>
42
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
43
 * @link      https://github.com/appserver-io/appserver
44
 * @link      http://www.appserver.io
45
 *
46
 * @property \AppserverIo\Appserver\PersistenceContainer\BeanManagerSettingsInterface $managerSettings Settings for the LDAP manager
47
 */
48
class LdapManager extends AbstractManager implements LdapManagerInterface, ManagerSettingsAwareInterface
49
{
50
51
    /**
52
     * Injects the LDAP manager settings.
53
     *
54
     * @param \AppserverIo\Appserver\PersistenceContainer\BeanManagerSettingsInterface $managerSettings The LDAP manager settings
55
     *
56
     * @return void
57
     */
58
    public function injectManagerSettings(ManagerSettingsInterface $managerSettings)
59
    {
60
        $this->managerSettings = $managerSettings;
0 ignored issues
show
Documentation Bug introduced by
$managerSettings is of type AppserverIo\Appserver\Ap...anagerSettingsInterface, but the property $managerSettings was declared to be of type AppserverIo\Appserver\Pe...anagerSettingsInterface. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
61
    }
62
63
    /**
64
     * Return's the LDAP manager settings.
65
     *
66
     * @return \AppserverIo\Appserver\PersistenceContainer\BeanManagerSettingsInterface The LDAP manager settings
67
     */
68
    public function getManagerSettings()
69
    {
70
        return $this->managerSettings;
71
    }
72
73
    /**
74
     * Has been automatically invoked by the container after the application
75
     * instance has been created.
76
     *
77
     * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
78
     *
79
     * @return void
80
     * @see \AppserverIo\Psr\Application\ManagerInterface::initialize()
81
     */
82
    public function initialize(ApplicationInterface $application)
83
    {
84
85
        // add the application instance to the environment
86
        Environment::singleton()->setAttribute(EnvironmentKeys::APPLICATION, $application);
87
88
        // create s simulated request/session ID whereas session equals request ID
89
        Environment::singleton()->setAttribute(EnvironmentKeys::SESSION_ID, $sessionId = SessionUtils::generateRandomString());
90
        Environment::singleton()->setAttribute(EnvironmentKeys::REQUEST_ID, $sessionId);
91
92
        // finally register the entiies
93
        $this->registerEntities($application);
94
    }
95
96
    /**
97
     * Registers the LDAP entities at startup.
98
     *
99
     * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
100
     *
101
     * @return void
102
     */
103
    public function registerEntities(ApplicationInterface $application)
104
    {
105
106
        // register the annotation registries
107
        $application->registerAnnotationRegistries();
0 ignored issues
show
Bug introduced by
The method registerAnnotationRegistries() 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

107
        $application->/** @scrutinizer ignore-call */ 
108
                      registerAnnotationRegistries();
Loading history...
108
109
        // parse the object descriptors
110
        $this->parseObjectDescriptors();
111
112
        // load the object manager
113
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
114
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
115
116
        // register the entities found by annotations and the XML configuration
117
        /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
118
        foreach ($objectManager->getObjectDescriptors() as $descriptor) {
119
            // check if we've found a entity descriptor and register the entity
120
            if ($descriptor instanceof EntityDescriptorInterface) {
121
                $this->registerEntity($descriptor);
122
            }
123
        }
124
    }
125
126
    /**
127
     * Register the entity described by the passed descriptor.
128
     *
129
     * @param \AppserverIo\Ldap\Description\EntityDescriptorInterface $descriptor The entity descriptor
130
     *
131
     * @return void
132
     */
133
    public function registerEntity(EntityDescriptorInterface $descriptor)
134
    {
135
136
        try {
137
            // load the application instance
138
            $application = $this->getApplication();
139
140
            // register the bean with the default name/short class name
141
            $application->getNamingDirectory()
0 ignored issues
show
Bug introduced by
The method getNamingDirectory() 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

141
            $application->/** @scrutinizer ignore-call */ 
142
                          getNamingDirectory()
Loading history...
142
                ->bind(
143
                    sprintf('php:global/%s/%s', $application->getUniqueName(), $descriptor->getName()),
0 ignored issues
show
Bug introduced by
The method getUniqueName() 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

143
                    sprintf('php:global/%s/%s', $application->/** @scrutinizer ignore-call */ getUniqueName(), $descriptor->getName()),
Loading history...
144
                    array(&$this, 'lookup'),
145
                    array($descriptor->getName())
146
                );
147
148
            // register the entity => repository mapping and vice versa
149
            $this->setAttribute($descriptor->getName(), $descriptor->getRepository());
0 ignored issues
show
Bug introduced by
$descriptor->getRepository() of type string is incompatible with the type object expected by parameter $value of AppserverIo\Appserver\Co...Manager::setAttribute(). ( Ignorable by Annotation )

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

149
            $this->setAttribute($descriptor->getName(), /** @scrutinizer ignore-type */ $descriptor->getRepository());
Loading history...
150
            $this->setAttribute($descriptor->getRepository(), $descriptor->getName());
151
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
152
        } catch (\Exception $e) {
153
            // log the exception
154
            $this->getApplication()->getInitialContext()->getSystemLogger()->critical($e->__toString());
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

154
            $this->getApplication()->/** @scrutinizer ignore-call */ getInitialContext()->getSystemLogger()->critical($e->__toString());
Loading history...
155
        }
156
    }
157
158
    /**
159
     * Runs a lookup for the entity with the passed lookup name.
160
     *
161
     * @param string $lookupName The lookpu name of the entity class
162
     *
163
     * @return object The requested entity instance
164
     */
165
    public function lookup($lookupName)
166
    {
167
        return $this->get($lookupName);
168
    }
169
170
    /**
171
     * Lookup the LDAP repository for the entity with the passed lookup name.
172
     *
173
     * @param string $lookupName The lookup name of the entity to return the repository for
174
     * @param array  $args       The arguments passed to the repository's constructor
175
     *
176
     * @return object The repository instance
177
     * @throws \Exception Is thrown, if the requested instance is NO LDAP repository
178
     */
179
    public function lookupRepositoryByEntityName($lookupName, array $args = array())
180
    {
181
182
        // load the object manager instance
183
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
184
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
185
186
        // query whether or not the passed lookup name relates to a LDAP repository
187
        if ($objectManager->hasObjectDescriptor($repositoryLookupName = $this->getAttribute($lookupName))) {
188
            if ($objectManager->getObjectDescriptor($repositoryLookupName) instanceof RepositoryDescriptorInterface) {
189
                return $this->getApplication()->search($repositoryLookupName, $args);
190
            }
191
        }
192
193
        // throw an exception if the requested instance is NOT a LDAP repository
194
        throw new \Exception(sprintf('Requested instance with lookup name "%s" is not available or is not a LDAP repository', $repositoryLookupName));
195
    }
196
197
    /**
198
     * Lookup the LDAP entity for the entity with the passed lookup name.
199
     *
200
     * @param string $lookupName The lookup name of the repository to return the entity for
201
     * @param array  $args       The arguments passed to the entity's constructor
202
     *
203
     * @return object The entity instance
204
     * @throws \Exception Is thrown, if the requested instance is NO LDAP entity
205
     */
206
    public function lookupEntityByRepositoryName($lookupName, array $args = array())
207
    {
208
209
        // load the object manager instance
210
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
211
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
212
213
        // query whether or not the passed lookup name relates to a LDAP entity
214
        if ($objectManager->hasObjectDescriptor($entityLookupName = $this->getAttribute($lookupName))) {
215
            // load the object descriptor
216
            $objectDescriptor = $objectManager->getObjectDescriptor($entityLookupName);
217
            // make sure that we've an LDAP entity descriptor here
218
            if ($objectDescriptor instanceof EntityDescriptorInterface) {
219
                return $this->newInstance($objectDescriptor->getClassName(), $args);
220
            }
221
        }
222
223
        // throw an exception if the requested instance is NOT a LDAP entity
224
        throw new \Exception(sprintf('Requested instance with lookup name "%s" is not available or is not a LDAP entity', $entityLookupName));
225
    }
226
227
    /**
228
     * Returns the object descriptor for the repository with the passed class name.
229
     *
230
     * @param string $className The class name to return the object descriptor for
231
     *
232
     * @return \AppserverIo\Ldap\Description\RepositoryDescriptorInterface|null The requested repository descriptor instance
233
     */
234
    public function lookupDescriptorByClassName($className)
235
    {
236
237
        // load the object manager instance
238
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
239
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
240
241
        // load the object descriptors
242
        $objectDescriptors = $objectManager->getObjectDescriptors();
243
244
        // iterate over the object descriptors and compare the class names
245
        foreach ($objectDescriptors as $objectDescriptor) {
246
            if ($objectDescriptor instanceof LdapDescriptorInterface) {
247
                if ($objectDescriptor->getClassName() === $className) {
248
                    return $objectDescriptor;
249
                }
250
            }
251
        }
252
    }
253
254
    /**
255
     * Returns the identifier for the object manager instance.
256
     *
257
     * @return string
258
     * @see \AppserverIo\Psr\Application\ManagerInterface::getIdentifier()
259
     */
260
    public function getIdentifier()
261
    {
262
        return LdapManagerInterface::IDENTIFIER;
263
    }
264
}
265