Completed
Push — develop ( 49270f...975a05 )
by
unknown
06:55
created

RoleSelectFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 25 7
A createService() 0 4 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license       MIT
8
 */
9
10
namespace Auth\Factory\Form;
11
12
use Interop\Container\ContainerInterface;
13
use Zend\ServiceManager\FactoryInterface;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
use Zend\Form\Element\Select;
16
use Auth\Entity\User;
17
18
/**
19
 * Class RoleSelectFactory
20
 *
21
 * Creates the select box of roles used temporary on the users profiles page. Box is removed from the
22
 * Users profiles Page now. But maybe we can reuse the code for the admin user
23
 *
24
 * @package Auth\Factory\Form
25
 */
26
class RoleSelectFactory implements FactoryInterface
27
{
28
    /**
29
     * Create a Select Element
30
     *
31
     * @param  ContainerInterface $container
32
     * @param  string             $requestedName
33
     * @param  null|array         $options
34
     *
35
     * @return Select
36
     * @throws ServiceNotFoundException if unable to resolve the service.
37
     * @throws ServiceNotCreatedException if an exception is raised when
38
     *     creating a service.
39
     * @throws ContainerException if any other error occurs
40
     */
41
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
42
    {
43
        $config     = $container->get('Config');
44
        $translator = $container->get('translator');
45
46
        $publicRoles = isset($config['acl']['public_roles'])
47
                       && is_array($config['acl']['public_roles'])
48
                       && !empty($config['acl']['public_roles'])
49
            ? $config['acl']['public_roles']
50
            : (in_array(User::ROLE_USER, $config['acl']['roles'])
51
               || array_key_exists('user', $config['acl']['roles'])
52
                ? array(User::ROLE_USER)
53
                : array('none')
54
            );
55
56
        $valueOptions = array();
57
        foreach ($publicRoles as $role) {
58
            $valueOptions[$role] = $translator->translate($role);
59
        }
60
61
        $select = new Select('role');
62
        $select->setValueOptions($valueOptions);
63
64
        return $select;
65
    }
66
    
67
    public function createService(ServiceLocatorInterface $serviceLocator)
68
    {
69
        return $this($serviceLocator->getServiceLocator(), Select::class);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\ServiceManager\ServiceLocatorInterface as the method getServiceLocator() does only exist in the following implementations of said interface: Acl\Assertion\AssertionManager, Core\Mail\MailService, Core\Paginator\PaginatorService, Zend\Barcode\ObjectPluginManager, Zend\Barcode\RendererPluginManager, Zend\Cache\PatternPluginManager, Zend\Cache\Storage\AdapterPluginManager, Zend\Cache\Storage\PluginManager, Zend\Config\ReaderPluginManager, Zend\Config\WriterPluginManager, Zend\Feed\Reader\ExtensionPluginManager, Zend\Feed\Writer\ExtensionPluginManager, Zend\File\Transfer\Adapter\FilterPluginManager, Zend\File\Transfer\Adapter\ValidatorPluginManager, Zend\Filter\FilterPluginManager, Zend\Form\FormElementMan...lementManagerV2Polyfill, Zend\Form\FormElementMan...lementManagerV3Polyfill, Zend\Hydrator\HydratorPluginManager, Zend\I18n\Translator\LoaderPluginManager, Zend\InputFilter\InputFilterPluginManager, Zend\Log\FilterPluginManager, Zend\Log\FormatterPluginManager, Zend\Log\ProcessorPluginManager, Zend\Log\WriterPluginManager, Zend\Log\Writer\FilterPluginManager, Zend\Log\Writer\FormatterPluginManager, Zend\Mail\Protocol\SmtpPluginManager, Zend\Mvc\Controller\ControllerManager, Zend\Mvc\Controller\PluginManager, Zend\Mvc\Router\RoutePluginManager, Zend\Paginator\AdapterPluginManager, Zend\Paginator\ScrollingStylePluginManager, Zend\Serializer\AdapterPluginManager, Zend\ServiceManager\AbstractPluginManager, Zend\Stdlib\Hydrator\HydratorPluginManager, Zend\Tag\Cloud\DecoratorPluginManager, Zend\Text\Table\DecoratorManager, Zend\Validator\ValidatorPluginManager, Zend\View\HelperPluginManager, Zend\View\Helper\Navigation\PluginManager.

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...
70
    }
71
}
72