Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.41% covered (warning)
72.41%
21 / 29
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExchangeAccessFilter
72.41% covered (warning)
72.41%
21 / 29
50.00% covered (danger)
50.00%
4 / 8
34.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFilteredEntity
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 getFilteredEntityByUseraccountPermissions
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getFilteredEntityByUseraccountSuperuser
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getFilteredEntityByScope
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
5.67
 getFilteredEntityByDepartment
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
5.67
 getFilteredEntityByOrganisation
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
5.67
 getOrganisationListByDepartments
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace BO\Zmsbackend\Helper;
4
5use BO\Slim\Render;
6
7/**
8 *
9 * @SuppressWarnings(CouplingBetweenObjects)
10 */
11class ExchangeAccessFilter
12{
13    protected static $filterList = [
14        'scope.id' => 'getFilteredEntityByScope',
15        'department.id' => 'getFilteredEntityByDepartment',
16        'organisation.id' => 'getFilteredEntityByOrganisation',
17        'useraccount.permissions' => 'getFilteredEntityByUseraccountPermissions',
18        'useraccount.permissions.superuser' => 'getFilteredEntityByUseraccountSuperuser',
19    ];
20
21    protected static $exchangeEntity = null;
22
23    protected static $filteredEntity = null;
24
25    protected static $workstation = null;
26
27    protected static $organisationList = null;
28
29    public function __construct($exchangeEntity, $workstation)
30    {
31        static::$exchangeEntity = $exchangeEntity;
32        static::$workstation = $workstation;
33        static::$organisationList = $this->getOrganisationListByDepartments();
34    }
35
36    /**
37     * @return \BO\Zmsentities\Exchange
38     *
39     */
40    public function getFilteredEntity()
41    {
42        static::$filteredEntity = clone static::$exchangeEntity;
43        foreach (static::$exchangeEntity->dictionary as $entry) {
44            if ($entry['reference'] && isset(static::$filterList[$entry['reference']])) {
45                $filterMethod = self::$filterList[$entry['reference']];
46                foreach (static::$filteredEntity->data as $key => $data) {
47                    static::$filterMethod($data[$entry['position']], $key);
48                }
49            }
50        }
51
52        return static::$filteredEntity;
53    }
54
55    /**
56     * @psalm-api
57     */
58    protected static function getFilteredEntityByUseraccountPermissions($permission, $filteredKey): void
59    {
60        if (! static::$workstation->getUseraccount()->hasPermissions([$permission])) {
61            unset(static::$filteredEntity->data[$filteredKey]);
62        }
63    }
64
65    /**
66     * @SuppressWarnings(UnusedFormalParameter)
67     *
68     * @psalm-api
69     */
70    protected static function getFilteredEntityByUseraccountSuperuser($unused, $filteredKey): void
71    {
72        if (! static::$workstation->getUseraccount()->isSuperUser()) {
73            unset(static::$filteredEntity->data[$filteredKey]);
74        }
75    }
76
77    /**
78     * @psalm-api
79     */
80    protected static function getFilteredEntityByScope($entityId, $filteredKey): void
81    {
82        if (static::$workstation->getUseraccount()->hasPermissions(['scope'])) {
83            if (! static::$workstation->getScopeListFromAssignedDepartments()->hasEntity($entityId)) {
84                unset(static::$filteredEntity->data[$filteredKey]);
85            }
86        }
87    }
88
89    /**
90     * @psalm-api
91     */
92    protected static function getFilteredEntityByDepartment($entityId, $filteredKey): void
93    {
94        if (static::$workstation->getUseraccount()->hasPermissions(['department'])) {
95            if (! static::$workstation->getDepartmentList()->hasEntity($entityId)) {
96                unset(static::$filteredEntity->data[$filteredKey]);
97            }
98        }
99    }
100
101    /**
102     * @psalm-api
103     */
104    protected static function getFilteredEntityByOrganisation($entityId, $filteredKey): void
105    {
106        if (static::$workstation->getUseraccount()->hasPermissions(['organisation'])) {
107            if (! static::$organisationList->hasEntity($entityId)) {
108                unset(static::$filteredEntity->data[$filteredKey]);
109            }
110        }
111    }
112
113    protected static function getOrganisationListByDepartments(): \BO\Zmsentities\Collection\OrganisationList
114    {
115        $organisationList = new \BO\Zmsentities\Collection\OrganisationList();
116        foreach (static::$workstation->getDepartmentList() as $department) {
117            $organisation = (new \BO\Zmsbackend\Organisation\Service\Organisation())->readByDepartmentId($department->id);
118            if ($organisation && $organisation instanceof \BO\Zmsentities\Organisation) {
119                $organisationList->addEntity($organisation);
120            }
121        }
122        return $organisationList;
123    }
124}