Completed
Push — master ( 94eff3...0c88b2 )
by Tim
7s
created

LocalContextConnection::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * AppserverIo\PersistenceContainerClient\Context\LocalContextConnection
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/rmi
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RemoteMethodInvocation;
22
23
use AppserverIo\Collections\CollectionInterface;
24
use AppserverIo\Psr\Application\ApplicationInterface;
25
use AppserverIo\Psr\EnterpriseBeans\BeanContextInterface;
26
27
/**
28
 * Connection implementation to invoke a local method call.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2015 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/appserver-io/rmi
34
 * @link      http://www.appserver.io
35
 */
36
class LocalContextConnection implements ConnectionInterface
37
{
38
39
    /**
40
     * The storage for the sessions.
41
     *
42
     * @var \AppserverIo\Collections\CollectionInterface
43
     */
44
    protected $sessions = null;
45
46
    /**
47
     * The application instance.
48
     *
49
     * @var \AppserverIo\Psr\Application\ApplicationInterface
50
     */
51
    protected $application;
52
53
    /**
54
     * The bean manager instance to load the local bean instance with.
55
     *
56
     * @var \AppserverIo\Psr\EnterpriseBeans\BeanContextInterface
57
     */
58
    protected $beanManager;
59
60
    /**
61
     * The local bean instance we're the proxy for.
62
     *
63
     * @var object
64
     */
65
    protected $instance;
66
67
    /**
68
     * Injects the application instance for the local connection.
69
     *
70
     * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
71
     *
72
     * @return void
73
     */
74
    public function injectApplication(ApplicationInterface $application)
75
    {
76
        $this->application = $application;
77
    }
78
79
    /**
80
     * Returns the application instance.
81
     *
82
     * @return \AppserverIo\Psr\Application\ApplicationInterface The application instance
83
     */
84
    public function getApplication()
85
    {
86
        return $this->application;
87
    }
88
89
    /**
90
     * Injects the collection for the sessions.
91
     *
92
     * @param \AppserverIo\Collections\CollectionInterface $sessions The collection for the sessions
93
     *
94
     * @return void
95
     */
96
    public function injectSessions(CollectionInterface $sessions)
97
    {
98
        $this->sessions = $sessions;
99
    }
100
101
    /**
102
     * Returns the collection with the sessions.
103
     *
104
     * @return \AppserverIo\Collections\CollectionInterface The collection with the sessions
105
     */
106
    public function getSessions()
107
    {
108
        return $this->sessions;
109
    }
110
111
    /**
112
     * Sends the remote method call to the container instance.
113
     *
114
     * @param \AppserverIo\RemoteMethodInvocation\RemoteMethodInterface $remoteMethod The remote method instance
115
     *
116
     * @return mixed The response from the container
117
     * @see AppserverIo\RemoteMethodInvocation\ConnectionInterface::send()
118
     */
119
    public function send(RemoteMethodInterface $remoteMethod)
120
    {
121
        return $this->getApplication()->search(BeanContextInterface::IDENTIFIER)->invoke($remoteMethod, $this->getSessions());
0 ignored issues
show
Bug introduced by
The method search() does not seem to exist on object<AppserverIo\Psr\A...n\ApplicationInterface>.

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...
122
    }
123
124
    /**
125
     * Initializes a new session instance.
126
     *
127
     * @return \AppserverIo\RemoteMethodInvocation\SessionInterface The session instance
128
     * @see \AppserverIo\RemoteMethodInvocation\ConnectionInterface::createContextSession()
129
     */
130
    public function createContextSession()
131
    {
132
        $this->sessions->add($session = new ContextSession($this));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface AppserverIo\Collections\CollectionInterface as the method add() does only exist in the following implementations of said interface: AppserverIo\Collections\ArrayList, AppserverIo\Collections\Dictionary, AppserverIo\Collections\HashMap, AppserverIo\Collections\IdentityDictionary, AppserverIo\Collections\TreeMap, AppserverIo\RemoteMethodInvocation\ContextSession.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
133
        return $session;
134
    }
135
}
136