VisibilityHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A visibilitySort() 0 13 3
A getAllByFieldId() 0 10 2
A getVisibleFields() 0 14 3
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
4
5
/**
6
 * Extended User Profile
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
16
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @since               2.3.0
18
 * @author              Jan Pedersen
19
 * @author              Taiwen Jiang <[email protected]>
20
 */
21
22
/**
23
 * Class ProfileVisibilityHandler
24
 */
25
class VisibilityHandler extends \XoopsPersistableObjectHandler
26
{
27
    /**
28
     * @param \XoopsDatabase $db
29
     */
30
    public function __construct(\XoopsDatabase $db)
31
    {
32
        parent::__construct($db, 'suico_profile_visibility', Visibility::class, 'field_id');
33
    }
34
35
    /**
36
     * Get fields visible to the $user_groups on a $profile_groups profile
37
     *
38
     * @param array      $profile_groups groups of the user to be accessed
39
     * @param array|null $user_groups    groups of the visitor, default as $GLOBALS['xoopsUser']
40
     *
41
     * @return array
42
     */
43
    public function getVisibleFields($profile_groups, $user_groups = null)
44
    {
45
        $user_groups[]    = 0;
46
        $profile_groups[] = 0;
47
        $sql              = "SELECT field_id FROM {$this->table} WHERE profile_group IN (" . \implode(',', $profile_groups) . ')';
48
        $sql              .= ' AND user_group IN (' . \implode(',', $user_groups) . ')';
0 ignored issues
show
Bug introduced by
It seems like $user_groups can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        $sql              .= ' AND user_group IN (' . \implode(',', /** @scrutinizer ignore-type */ $user_groups) . ')';
Loading history...
49
        $field_ids        = [];
50
        if (false !== ($result = $this->db->query($sql))) {
51
            while (false !== ([$field_id] = $this->db->fetchRow($result))) {
52
                $field_ids[] = $field_id;
53
            }
54
        }
55
56
        return $field_ids;
57
    }
58
59
    /**
60
     * get all rows matching a condition
61
     *
62
     * @param \CriteriaElement|null $criteria {@link \CriteriaElement} to match
63
     *
64
     * @return array of row arrays, indexed by field_id
65
     */
66
    public function getAllByFieldId(\CriteriaElement $criteria = null)
67
    {
68
        $rawRows = parent::getAll($criteria, null, false, false);
69
        \usort($rawRows, [$this, 'visibilitySort']);
70
        $rows = [];
71
        foreach ($rawRows as $rawRow) {
72
            $rows[$rawRow['field_id']][] = $rawRow;
73
        }
74
75
        return $rows;
76
    }
77
78
    /**
79
     * compare two arrays, each a row from profile_visibility
80
     * The comparison is on three columns, 'field_id', 'user_group', 'profile_group' considered in that
81
     * order for comparison
82
     *
83
     * @param array $a associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
84
     * @param array $b associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
85
     *
86
     * @return int integer less that zero if $a is less than $b
87
     *              integer zero if $a and $b are equal
88
     *              integer greater than zero if $a is greater than $b
89
     */
90
    protected function visibilitySort($a, $b)
91
    {
92
        $fieldDiff = $a['field_id'] - $b['field_id'];
93
        $userDiff  = $a['user_group'] - $b['user_group'];
94
        $profDiff  = $a['profile_group'] - $b['profile_group'];
95
        if (0 != $fieldDiff) {
96
            return $fieldDiff;
97
        }
98
        if (0 !== $userDiff) {
99
            return $userDiff;
100
        }
101
102
        return $profDiff;
103
    }
104
}
105