Completed
Push — master ( 688c46...b38950 )
by Tim
8s
created

ServletDispatcherResult::getServletContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\Results\ServletDispatcherResult
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       http://github.com/appserver-io/routlt
18
 * @link       http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Routlt\Results;
22
23
use AppserverIo\Routlt\Util\ServletContextAware;
24
use AppserverIo\Psr\Servlet\ServletContextInterface;
25
use AppserverIo\Psr\Servlet\ServletRequestInterface;
26
use AppserverIo\Psr\Servlet\ServletResponseInterface;
27
28
/**
29
 * Result implementation that dispatches another servlet.
30
 *
31
 * @author     Tim Wagner <[email protected]>
32
 * @copyright  2015 TechDivision GmbH <[email protected]>
33
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link       http://github.com/appserver-io/routlt
35
 * @link       http://www.appserver.io
36
 *
37
 * @Result(shared=false)
38
 */
39
class ServletDispatcherResult extends AbstractResult implements ServletContextAware
40
{
41
42
    /**
43
     * The servlet context instance.
44
     *
45
     * @param \AppserverIo\Psr\Servlet\ServletContextInterface
46
     */
47
    protected $servletContext;
48
49
    /**
50
     * Sets the actual servlet context instance.
51
     *
52
     * @param \AppserverIo\Psr\Servlet\ServletContextInterface $servletContext The servlet context instance
53
     *
54
     * @return void
55
     */
56 1
    public function setServletContext(ServletContextInterface $servletContext)
57
    {
58 1
        $this->servletContext = $servletContext;
59 1
    }
60
61
    /**
62
     * Returns the servlet context instance.
63
     *
64
     * @return \AppserverIo\Psr\Servlet\ServletContextInterface The servlet context instance
65
    */
66 1
    public function getServletContext()
67
    {
68 1
        return $this->servletContext;
69
    }
70
71
    /**
72
     * Processes an action result by dispatching the configured servlet.
73
     *
74
     * @param \AppserverIo\Psr\Servlet\ServletRequestInterface  $servletRequest  The request instance
75
     * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client
76
     *
77
     * @return void
78
     */
79 1
    public function process(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse)
80
    {
81
82
        // initialize the variables for the path and the query string
83 1
        $path = null;
84 1
        $query = null;
85
86
        // load result and session-ID
87 1
        extract(parse_url($this->getResult()));
0 ignored issues
show
Bug introduced by
parse_url($this->getResult()) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
88
89
        // initialize the request URI
90 1
        if (!empty($path)) {
91 1
            $servletRequest->setRequestUri($path);
0 ignored issues
show
Bug introduced by
The method setRequestUri() does not seem to exist on object<AppserverIo\Psr\S...ervletRequestInterface>.

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...
92 1
        }
93
94
        // initialize the query string
95 1
        if (!empty($query)) {
96 1
            $servletRequest->setQueryString($query);
0 ignored issues
show
Bug introduced by
The method setQueryString() does not seem to exist on object<AppserverIo\Psr\S...ervletRequestInterface>.

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...
97 1
        }
98
99
        // prepare the request with the new data
100 1
        $servletRequest->prepare();
0 ignored issues
show
Bug introduced by
The method prepare() does not seem to exist on object<AppserverIo\Psr\S...ervletRequestInterface>.

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...
101
102
        // load the servlet path and session-ID
103 1
        $servletPath = $servletRequest->getServletPath();
104 1
        $sessionId = $servletRequest->getProposedSessionId();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface AppserverIo\Psr\Servlet\ServletRequestInterface as the method getProposedSessionId() does only exist in the following implementations of said interface: AppserverIo\Psr\Servlet\...tpServletRequestWrapper.

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...
105
106
        // load and process the servlet
107 1
        $servlet = $this->getServletContext()->lookup($servletPath, $sessionId);
0 ignored issues
show
Bug introduced by
The method lookup() does not seem to exist on object<AppserverIo\Psr\S...ervletContextInterface>.

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...
108 1
        $servlet->service($servletRequest, $servletResponse);
109 1
    }
110
}
111