RoutingUserSource   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSource() 0 12 4
1
<?php
2
namespace Agavi\Routing;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
use Agavi\User\User;
17
18
/**
19
 * AgaviRoutingUserSource allows you to provide an user source for the routing
20
 *
21
 * @package    agavi
22
 * @subpackage routing
23
 *
24
 * @author     Dominik del Bondio <[email protected]>
25
 * @copyright  Authors
26
 * @copyright  The Agavi Project
27
 *
28
 * @since      0.11.0
29
 *
30
 * @version    $Id$
31
 */
32
class RoutingUserSource implements RoutingSourceInterface
33
{
34
    /**
35
     * @var        User An user instance.
36
     */
37
    protected $user = null;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param      User $user An user instance.
43
     *
44
     * @author     Dominik del Bondio <[email protected]>
45
     * @since      0.11.0
46
     */
47
    public function __construct(User $user)
48
    {
49
        $this->user = $user;
50
    }
51
52
    /**
53
     * Retrieves the value for a given entry from the source.
54
     *
55
     * @param      array $parts An array with the name parts for the entry.
56
     *
57
     * @return     mixed The value.
58
     *
59
     * @author     Dominik del Bondio <[email protected]>
60
     * @since      0.11.0
61
     */
62
    public function getSource(array $parts)
63
    {
64
        if ($parts[0] == 'authenticated') {
65
            return (int) $this->user->isAuthenticated();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Agavi\User\User as the method isAuthenticated() does only exist in the following sub-classes of Agavi\User\User: Agavi\User\RbacSecurityUser, Agavi\User\SecurityUser. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
66
        } elseif ($parts[0] == 'credentials' && count($parts) > 1) {
67
            // throw the 'credentials' entry away and check with the parameters left
68
            array_shift($parts);
69
            return (int) $this->user->hasCredentials($parts);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Agavi\User\User as the method hasCredentials() does only exist in the following sub-classes of Agavi\User\User: Agavi\User\RbacSecurityUser, Agavi\User\SecurityUser. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
70
        }
71
72
        return null;
73
    }
74
}
75