Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
ExchangeAccessFilter
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
21
100.00% covered (success)
100.00%
1 / 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
 getFilteredEntityByUseraccountRights
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getFilteredEntityByScope
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getFilteredEntityByDepartment
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getFilteredEntityByOrganisation
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getOrganisationListByDepartments
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace BO\Zmsapi\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.rights' => 'getFilteredEntityByUseraccountRights'
18    ];
19
20    protected static $exchangeEntity = null;
21
22    protected static $filteredEntity = null;
23
24    protected static $workstation = null;
25
26    protected static $organisationList = null;
27
28    public function __construct($exchangeEntity, $workstation)
29    {
30        static::$exchangeEntity = $exchangeEntity;
31        static::$workstation = $workstation;
32        static::$organisationList = $this->getOrganisationListByDepartments();
33    }
34
35    /**
36     * @return \BO\Zmsentities\Exchange
37     *
38     */
39    public function getFilteredEntity()
40    {
41        static::$filteredEntity = clone static::$exchangeEntity;
42        foreach (static::$exchangeEntity->dictionary as $entry) {
43            if ($entry['reference'] && isset(static::$filterList[$entry['reference']])) {
44                $filterMethod = self::$filterList[$entry['reference']];
45                foreach (static::$filteredEntity->data as $key => $data) {
46                    static::$filterMethod($data[$entry['position']], $key);
47                }
48            }
49        }
50
51        return static::$filteredEntity;
52    }
53
54    protected static function getFilteredEntityByUseraccountRights($right, $filteredKey)
55    {
56        if (! static::$workstation->getUseraccount()->hasRights([$right])) {
57            unset(static::$filteredEntity->data[$filteredKey]);
58        }
59    }
60
61    protected static function getFilteredEntityByScope($entityId, $filteredKey)
62    {
63        if (static::$workstation->getUseraccount()->hasRights(['scope'])) {
64            if (! static::$workstation->getScopeListFromAssignedDepartments()->hasEntity($entityId)) {
65                unset(static::$filteredEntity->data[$filteredKey]);
66            }
67        }
68    }
69
70    protected static function getFilteredEntityByDepartment($entityId, $filteredKey)
71    {
72        if (static::$workstation->getUseraccount()->hasRights(['department'])) {
73            if (! static::$workstation->getDepartmentList()->hasEntity($entityId)) {
74                unset(static::$filteredEntity->data[$filteredKey]);
75            }
76        }
77    }
78
79    protected static function getFilteredEntityByOrganisation($entityId, $filteredKey)
80    {
81        if (static::$workstation->getUseraccount()->hasRights(['organisation'])) {
82            if (! static::$organisationList->hasEntity($entityId)) {
83                unset(static::$filteredEntity->data[$filteredKey]);
84            }
85        }
86    }
87
88    protected static function getOrganisationListByDepartments()
89    {
90        $organisationList = new \BO\Zmsentities\Collection\OrganisationList();
91        foreach (static::$workstation->getDepartmentList() as $department) {
92            $organisation = (new \BO\Zmsdb\Organisation())->readByDepartmentId($department->id);
93            if ($organisation && $organisation instanceof \BO\Zmsentities\Organisation) {
94                $organisationList->addEntity($organisation);
95            }
96        }
97        return $organisationList;
98    }
99}