Passed
Push — master ( 6acaa5...4ef58e )
by Simon
06:26
created

DataObjectExtension::getViewList()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 4
nc 6
nop 0
1
<?php
2
3
4
namespace Firesphere\SolrPermissions\Extensions;
5
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\Member;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * Class \Firesphere\SolrPermissions\Extensions\DataObjectExtension
14
 *
15
 * Add the ability to get the Member View statusses for Solr.
16
 *
17
 * @package Firesphere\SolrPermissions\Extensions
18
 * @property DataObject|DataObjectExtension $owner
19
 */
20
class DataObjectExtension extends DataExtension
21
{
22
    /**
23
     * @var ArrayList Cached list of the members to reduce looping impact
24
     */
25
    protected static $memberList;
26
27
    /**
28
     * Get the member permissions for each unique user in the system
29
     * This is additional to the GroupView permissions
30
     *
31
     * @return array
32
     */
33
    public function getMemberView()
34
    {
35
        /** @var Member|null $currentUser */
36
        $currentUser = Security::getCurrentUser();
37
        Security::setCurrentUser(null);
38
39
        if ($this->owner->canView(null)) {
0 ignored issues
show
Bug introduced by
The method canView() does not exist on Firesphere\SolrPermissio...ons\DataObjectExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        if ($this->owner->/** @scrutinizer ignore-call */ canView(null)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
            Security::setCurrentUser($currentUser);
41
42
            return ['null'];
43
        }
44
45
        $return = $this->getViewList();
46
47
        Security::setCurrentUser($currentUser);
48
49
        return $return;
50
    }
51
52
    /**
53
     * Get the list of members who can view this owner
54
     *
55
     * @return array
56
     */
57
    private function getViewList(): array
58
    {
59
        if (!static::$memberList) {
60
            static::$memberList = ArrayList::create(Member::get()->toArray());
61
        }
62
63
        $return = ['false'];
64
        foreach (static::$memberList as $member) {
65
            if ($this->owner->canView($member)) {
66
                $return[] = sprintf('1-%s', $member->ID);
67
            }
68
        }
69
70
        return $return;
71
    }
72
}
73