BackTo::setSessionContainer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace AtAdmin\Controller\Plugin;
4
5
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
6
use Zend\Session\Container;
7
8
class BackTo extends AbstractPlugin
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $paramName = 'backto';
14
15
    /**
16
     * @var Container
17
     */
18
    protected $sessionContainer;
19
20
    /**
21
     * @param $name
22
     */
23
    public function setParamName($name)
24
    {
25
        $this->paramName = (string) $name;
26
    }
27
28
    /**
29
     * @param Container $container
30
     * @return $this
31
     */
32
    public function setSessionContainer(Container $container)
33
    {
34
        $this->sessionContainer = $container;
35
        return $this;
36
    }
37
38
    /**
39
     * @return Container
40
     */
41
    public function getSessionContainer()
42
    {
43
        if (!$this->sessionContainer) {
44
            $this->sessionContainer = new Container('at_base');
45
        }
46
47
        return $this->sessionContainer;
48
    }
49
50
    /**
51
     * @param null $url
52
     */
53
    public function setBackUrl($url = null)
54
    {
55
        if ($url) {
56
            $url = (string) $url;
57
        } else {
58
            $url = $this->getController()->getRequest()->getRequestUri();
59
        }
60
61
        $session = $this->getSessionContainer();
62
        $session->{$this->paramName} = $url;
63
    }
64
65
    /**
66
     * @param bool $flush
67
     * @return string
68
     */
69
    public function getBackUrl($flush = true)
70
    {
71
        $backUrl = '/';
72
73
        $session = $this->getSessionContainer();
74
        if (isset($session->{$this->paramName})) {
75
            $backUrl = $session->{$this->paramName};
76
        }
77
78
        if ($flush) {
79
            $this->flush();
80
        }
81
82
        return $backUrl;
83
    }
84
85
    /**
86
     * @return $this
87
     */
88
    public function flush()
89
    {
90
        $session = $this->getSessionContainer();
91
        unset($session->{$this->paramName});
92
        return $this;
93
    }
94
95
    /**
96
     * @param null $message
97
     */
98
    public function previous($message = null)
99
    {
100
        // Return if it is ajax request
101
        if ($this->getController()->getRequest()->isXmlHttpRequest()) {
102
            return;
103
        }
104
105
        if ($message) {
106
            $this->getController()->flashMessenger()->addMessage($message);
0 ignored issues
show
Bug introduced by
The method flashMessenger() does not seem to exist on object<Zend\Stdlib\DispatchableInterface>.

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...
107
        }
108
109
        $this->getController()->redirect()->toUrl($this->getBackUrl());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\DispatchableInterface as the method redirect() does only exist in the following implementations of said interface: AtAdmin\Controller\AbstractAdminController, AtAdmin\Controller\DashboardController, AtAdmin\Controller\GridController, Zend\Mvc\Controller\AbstractActionController, Zend\Mvc\Controller\AbstractController, Zend\Mvc\Controller\AbstractRestfulController.

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...
110
    }
111
}