Completed
Pull Request — develop (#542)
by Mathias
09:01
created

EmployeeFieldsetFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 5
eloc 35
dl 0
loc 71
ccs 16
cts 32
cp 0.5
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 59 5
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\Factory\FactoryInterface;
16
use Zend\Hydrator\Strategy\ClosureStrategy;
17
use Organizations\Entity\EmployeePermissionsInterface as Perms;
18
use Organizations\Form\EmployeeFieldset;
19
20
/**
21
 * Factory for an EmployeeFieldset
22
 *
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @todo extract hydrating strategies
25
 * @since 0.18
26
 */
27
class EmployeeFieldsetFactory implements FactoryInterface
28
{
29
30
    /**
31
     * Create a EmployeeFieldset fieldset
32
     *
33
     * @param  ContainerInterface $container
34
     * @param  string             $requestedName
35
     * @param  null|array         $options
36
     *
37
     * @return EmployeeFieldset
38
     */
39 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
40
    {
41 1
        $fieldset = new EmployeeFieldset();
42
43 1
        $hydrator = new \Zend\Hydrator\ClassMethods(false); //new EntityHydrator();
44 1
        $repositories = $container->get('repositories');
45 1
        $users        = $repositories->get('Auth/User'); /* @var $users \Auth\Repository\User */
46
47
        /* todo: WRITE own Hydrator strategy class */
48 1
        $strategy = new ClosureStrategy(
49
            function ($object) use ($users) {
50
                if (is_string($object)) {
51
                    return $users->find($object);
52
                }
53
                return $object;
54 1
            },
55
            function ($data) use ($users) {
56
                if (is_string($data)) {
57
                    $data = $users->find($data);
58
                }
59
                return $data;
60 1
            }
61
        );
62
63
        /* todo: write own strategy class */
64 1
        $permStrategy = new ClosureStrategy(
65
        // extract
66
            function ($object) {
67
                /* @var $object \Organizations\Entity\EmployeePermissionsInterface */
68
                $values = array();
69
                foreach (array(
70
                                Perms::JOBS_VIEW, Perms::JOBS_CHANGE, PERMS::JOBS_CREATE,
71
                                Perms::APPLICATIONS_VIEW, Perms::APPLICATIONS_CHANGE)
72
                            as $perm) {
73
                    if ($object->isAllowed($perm)) {
74
                        $values[] = $perm;
75
                    }
76
                }
77
78
                return $values;
79 1
            },
80
            function ($data) {
81
                $permissions = array_reduce(
82
                    $data,
83
                    function ($c, $i) {
84
                        return $c | $i;
85
                    },
86
                    0
87
                );
88
                return new EmployeePermissions($permissions);
89 1
            }
90
        );
91
92 1
        $hydrator->addStrategy('user', $strategy);
93 1
        $hydrator->addStrategy('permissions', $permStrategy);
94 1
        $fieldset->setHydrator($hydrator);
95 1
        $fieldset->setObject(new \Organizations\Entity\Employee());
96
97 1
        return  $fieldset;
98
    }
99
}
100