Completed
Push — develop ( 975a05...8c983c )
by
unknown
07:04
created

EmployeeFieldsetFactory::__invoke()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 64
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 8.6346
c 0
b 0
f 0
cc 5
eloc 36
nc 1
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Organizations\Factory\Form;
12
13
use Interop\Container\ContainerInterface;
14
use Organizations\Entity\EmployeePermissions;
15
use Zend\ServiceManager\FactoryInterface;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
use Zend\Hydrator\Strategy\ClosureStrategy;
18
use Organizations\Entity\EmployeePermissionsInterface as Perms;
19
use Organizations\Form\EmployeeFieldset;
20
21
/**
22
 * Factory for an EmployeeFieldset
23
 *
24
 * @author Mathias Gelhausen <[email protected]>
25
 * @todo extract hydrating strategies
26
 * @since 0.18
27
 */
28
class EmployeeFieldsetFactory implements FactoryInterface
29
{
30
31
    /**
32
     * Create a EmployeeFieldset fieldset
33
     *
34
     * @param  ContainerInterface $container
35
     * @param  string             $requestedName
36
     * @param  null|array         $options
37
     *
38
     * @return EmployeeFieldset
39
     */
40
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
41
    {
42
43
        $fieldset = new EmployeeFieldset();
44
45
        $hydrator = new \Zend\Hydrator\ClassMethods(false); //new EntityHydrator();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
        $repositories = $container->get('repositories');
47
        $users        = $repositories->get('Auth/User'); /* @var $users \Auth\Repository\User */
48
49
        /* todo: WRITE own Hydrator strategy class */
50
        $strategy = new ClosureStrategy(
51
            function ($object) use ($users) {
52
53
                if (is_string($object)) {
54
                    return $users->find($object);
55
                }
56
                return $object;
57
            },
58
            function ($data) use ($users) {
59
60
                if (is_string($data)) {
61
                    $data = $users->find($data);
62
                }
63
                return $data;
64
            }
65
        );
66
67
        /* todo: write own strategy class */
68
        $permStrategy = new ClosureStrategy(
69
        // extract
70
            function ($object) {
71
                /* @var $object \Organizations\Entity\EmployeePermissionsInterface */
72
                $values = array();
73
                foreach (array(
74
                             Perms::JOBS_VIEW, Perms::JOBS_CHANGE, PERMS::JOBS_CREATE,
75
                             Perms::APPLICATIONS_VIEW, Perms::APPLICATIONS_CHANGE)
76
                         as $perm) {
77
                    if ($object->isAllowed($perm)) {
78
                        $values[] = $perm;
79
                    }
80
81
                }
82
83
                return $values;
84
            },
85
            function ($data) {
86
                $permissions = array_reduce(
87
                    $data,
88
                    function ($c, $i) {
89
                        return $c | $i;
90
                    },
91
                    0
92
                );
93
                return new EmployeePermissions($permissions);
94
            }
95
        );
96
97
        $hydrator->addStrategy('user', $strategy);
98
        $hydrator->addStrategy('permissions', $permStrategy);
99
        $fieldset->setHydrator($hydrator);
100
        $fieldset->setObject(new \Organizations\Entity\Employee());
101
102
        return  $fieldset;
103
    }
104
105
    /**
106
     * Create the fieldset.
107
     *
108
     * {@inheritDoc}
109
     *
110
     * @return EmployeeFieldset
111
     */
112
    public function createService(ServiceLocatorInterface $serviceLocator)
113
    {
114
        /* @var $serviceLocator \Zend\ServiceManager\AbstractPluginManager */
115
        return $this($serviceLocator->getServiceLocator(), EmployeeFieldset::class);
116
    }
117
}