BeanLocator::lookup()   C
last analyzed

Complexity

Conditions 15
Paths 15

Size

Total Lines 112
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
cc 15
eloc 39
nc 15
nop 3
dl 0
loc 112
ccs 0
cts 55
cp 0
crap 240
rs 5.9166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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\PersistenceContainer\BeanLocator
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 2015 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\PersistenceContainer;
22
23
use AppserverIo\Psr\Di\ProviderInterface;
24
use AppserverIo\Psr\Di\ObjectManagerInterface;
25
use AppserverIo\Psr\EnterpriseBeans\BeanContextInterface;
26
use AppserverIo\Psr\EnterpriseBeans\ResourceLocatorInterface;
27
use AppserverIo\Psr\EnterpriseBeans\EnterpriseBeansException;
28
use AppserverIo\Psr\EnterpriseBeans\Description\MessageDrivenBeanDescriptorInterface;
29
use AppserverIo\Psr\EnterpriseBeans\Description\StatefulSessionBeanDescriptorInterface;
30
use AppserverIo\Psr\EnterpriseBeans\Description\SingletonSessionBeanDescriptorInterface;
31
use AppserverIo\Psr\EnterpriseBeans\Description\StatelessSessionBeanDescriptorInterface;
32
use AppserverIo\Psr\EnterpriseBeans\Description\BeanDescriptorInterface;
33
use AppserverIo\Appserver\Core\Environment;
34
use AppserverIo\Appserver\Core\Utilities\EnvironmentKeys;
35
36
/**
37
 * The bean resource locator implementation.
38
 *
39
 * @author    Tim Wagner <[email protected]>
40
 * @copyright 2015 TechDivision GmbH <[email protected]>
41
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
42
 * @link      https://github.com/appserver-io/appserver
43
 * @link      http://www.appserver.io
44
 */
45
class BeanLocator implements ResourceLocatorInterface
46
{
47
48
    /**
49
     * Runs a lookup for the session bean with the passed class name
50
     *
51
     * If the passed class name is a session bean an instance
52
     * will be returned.
53
     *
54
     * @param \AppserverIo\Psr\EnterpriseBeans\BeanContextInterface $beanManager The bean manager instance
55
     * @param string                                                $className   The name of the session bean's class
56
     * @param array                                                 $args        The arguments passed to the session beans constructor
57
     *
58
     * @return object The requested session bean
59
     */
60
    public function lookup(BeanContextInterface $beanManager, $className, array $args = array())
61
    {
62
63
        // load the object manager
64
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
65
        $objectManager = $beanManager->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
0 ignored issues
show
Bug introduced by
The method getApplication() does not exist on AppserverIo\Psr\Enterpri...ns\BeanContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Enterpri...ns\BeanContextInterface such as AppserverIo\Appserver\Pe...ceContainer\BeanManager. ( Ignorable by Annotation )

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

65
        $objectManager = $beanManager->/** @scrutinizer ignore-call */ getApplication()->search(ObjectManagerInterface::IDENTIFIER);
Loading history...
66
67
        // load the session ID from the environment
68
        $sessionId = Environment::singleton()->getAttribute(EnvironmentKeys::SESSION_ID);
69
70
        // query whether or not an object descriptor is available
71
        if ($objectManager->hasObjectDescriptor($className)) {
72
            // load the bean descriptor
73
            $descriptor = $objectManager->getObjectDescriptor($className);
74
75
            // query whether we've a SFSB
76
            if ($descriptor instanceof StatefulSessionBeanDescriptorInterface) {
77
                // try to load the stateful session bean from the bean manager
78
                if ($instance = $beanManager->lookupStatefulSessionBean($sessionId, $className)) {
0 ignored issues
show
Bug introduced by
The method lookupStatefulSessionBean() does not exist on AppserverIo\Psr\Enterpri...ns\BeanContextInterface. Did you maybe mean lookup()? ( Ignorable by Annotation )

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

78
                if ($instance = $beanManager->/** @scrutinizer ignore-call */ lookupStatefulSessionBean($sessionId, $className)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
                    // load the object manager and re-inject the dependencies
80
                    /** @var \AppserverIo\Psr\Di\ProviderInterface $provider */
81
                    $provider = $beanManager->getApplication()->search(ProviderInterface::IDENTIFIER);
82
                    $provider->injectDependencies($descriptor, $instance);
83
84
                    // we've to check for post-detach callbacks
85
                    foreach ($descriptor->getPostDetachCallbacks() as $postDetachCallback) {
86
                        $instance->$postDetachCallback();
87
                    }
88
89
                    // return the instance
90
                    return $instance;
91
                }
92
93
                // if not create a new instance and return it
94
                $instance = $beanManager->get($className);
0 ignored issues
show
Bug introduced by
The method get() does not exist on AppserverIo\Psr\Enterpri...ns\BeanContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Enterpri...ns\BeanContextInterface such as AppserverIo\Appserver\Pe...ceContainer\BeanManager. ( Ignorable by Annotation )

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

94
                /** @scrutinizer ignore-call */ 
95
                $instance = $beanManager->get($className);
Loading history...
95
96
                // we've to check for post-construct callbacks
97
                foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) {
98
                    $instance->$postConstructCallback();
99
                }
100
101
                // return the instance
102
                return $instance;
103
            }
104
105
            // query whether we've a SSB
106
            if ($descriptor instanceof SingletonSessionBeanDescriptorInterface) {
107
                // try to load the singleton session bean from the bean manager
108
                if ($instance = $beanManager->lookupSingletonSessionBean($className)) {
0 ignored issues
show
Bug introduced by
The method lookupSingletonSessionBean() does not exist on AppserverIo\Psr\Enterpri...ns\BeanContextInterface. Did you maybe mean lookup()? ( Ignorable by Annotation )

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

108
                if ($instance = $beanManager->/** @scrutinizer ignore-call */ lookupSingletonSessionBean($className)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
                    // load the object manager and re-inject the dependencies
110
                    /** @var \AppserverIo\Psr\Di\ProviderInterface $provider */
111
                    $provider = $beanManager->getApplication()->search(ProviderInterface::IDENTIFIER);
112
                    $provider->injectDependencies($descriptor, $instance);
113
114
                    // we've to check for post-detach callbacks
115
                    foreach ($descriptor->getPostDetachCallbacks() as $postDetachCallback) {
116
                        $instance->$postDetachCallback();
117
                    }
118
119
                    // return the instance
120
                    return $instance;
121
                }
122
123
                // singleton session beans MUST extends \Stackable
124
                if (is_subclass_of($descriptor->getClassName(), '\Stackable') === false) {
125
                    throw new EnterpriseBeansException(sprintf('Singleton session bean %s MUST extend \Stackable', $descriptor->getClassName()));
126
                }
127
128
                // if not create a new instance and return it
129
                $instance = $beanManager->newSingletonSessionBeanInstance($className);
0 ignored issues
show
Bug introduced by
The method newSingletonSessionBeanInstance() does not exist on AppserverIo\Psr\Enterpri...ns\BeanContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Enterpri...ns\BeanContextInterface such as AppserverIo\Appserver\Pe...ceContainer\BeanManager. ( Ignorable by Annotation )

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

129
                /** @scrutinizer ignore-call */ 
130
                $instance = $beanManager->newSingletonSessionBeanInstance($className);
Loading history...
130
131
                // add the singleton session bean to the container
132
                $beanManager->getSingletonSessionBeans()->set($className, $instance);
0 ignored issues
show
Bug introduced by
The method getSingletonSessionBeans() does not exist on AppserverIo\Psr\Enterpri...ns\BeanContextInterface. It seems like you code against a sub-type of AppserverIo\Psr\Enterpri...ns\BeanContextInterface such as AppserverIo\Appserver\Pe...ceContainer\BeanManager. ( Ignorable by Annotation )

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

132
                $beanManager->/** @scrutinizer ignore-call */ 
133
                              getSingletonSessionBeans()->set($className, $instance);
Loading history...
133
134
                // we've to check for post-construct callback
135
                foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) {
136
                    $instance->$postConstructCallback();
137
                }
138
139
                // return the instance
140
                return $instance;
141
            }
142
143
            // query whether we've a SLSB
144
            if ($descriptor instanceof StatelessSessionBeanDescriptorInterface) {
145
                // if not create a new instance and return it
146
                $instance = $beanManager->get($className);
147
148
                // we've to check for post-construct callback
149
                foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) {
150
                    $instance->$postConstructCallback();
151
                }
152
153
                // return the instance
154
                return $instance;
155
            }
156
157
            //  query whether we've a MDB
158
            if ($descriptor instanceof MessageDrivenBeanDescriptorInterface) {
159
                // create a new instance and return it
160
                return $beanManager->get($className);
161
            }
162
163
            // query whether we simply have a bean
164
            if ($descriptor instanceof BeanDescriptorInterface) {
165
                // create the bean instance without the factory
166
                return $beanManager->get($className);
167
            }
168
        }
169
170
        // finally try to let the container create a new instance of the bean
171
        return $beanManager->newInstance($className);
172
    }
173
}
174