Test Failed
Push — main ( 6435b1...db043e )
by Paul
16:23 queued 07:02
created

DirectoryController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
eloc 55
c 1
b 0
f 1
dl 0
loc 123
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A modifyTmpl() 0 7 2
A filterAjaxMembersData() 0 14 3
A modifyQuerySortby() 0 14 4
A filterDirectoryProfileSortOptions() 0 5 1
A filterDirectorySortBy() 0 29 4
A filterDirectoryProfileOptions() 0 9 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\UltimateMember\Controllers;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database\CountManager;
7
use GeminiLabs\SiteReviews\Database\Query;
8
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Helpers\Cast;
10
use GeminiLabs\SiteReviews\Modules\Html\Builder;
11
12
class DirectoryController extends AbstractController
13
{
14
    /**
15
     * @param int $userId
16
     *
17
     * @filter um_ajax_get_members_data
18
     */
19
    public function filterAjaxMembersData(array $data, $userId): array
20
    {
21
        $html = '';
22
        $userId = Cast::toInt($userId);
23
        $total = glsr(CountManager::class)->usersReviews($userId);
24
        if (0 < $total || glsr_get_option('integrations.ultimatemember.display_empty', false, 'bool')) {
25
            $rating = glsr(CountManager::class)->usersAverage($userId);
26
            $html = glsr(Builder::class)->div([
27
                'class' => 'um-member-rating',
28
                'text' => glsr_star_rating($rating, $total),
29
            ]);
30
        }
31
        $data['glsr_rating_html'] = $html;
32
        return $data;
33
    }
34
35
    /**
36
     * @filter um_admin_extend_directory_options_profile
37
     */
38
    public function filterDirectoryProfileOptions(array $fields): array
39
    {
40
        $fields[] = [
41
            'id' => '_um_glsr_display_user_rating',
42
            'type' => 'checkbox',
43
            'label' => _x('Site Reviews: Display User Rating', 'admin-text', 'site-reviews'),
44
            'value' => UM()->query()->get_meta_value('_um_glsr_display_user_rating', null, 'na'),
0 ignored issues
show
Bug introduced by
The function UM was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

44
            'value' => /** @scrutinizer ignore-call */ UM()->query()->get_meta_value('_um_glsr_display_user_rating', null, 'na'),
Loading history...
45
        ];
46
        return $fields;
47
    }
48
49
    /**
50
     * @filter um_members_directory_sort_fields
51
     */
52
    public function filterDirectoryProfileSortOptions(array $options): array
53
    {
54
        $options['glsr_highest_rated'] = _x('Site Reviews: Highest rated first', 'admin-text', 'site-reviews');
55
        $options['glsr_lowest_rated'] = _x('Site Reviews: Lowest rated first', 'admin-text', 'site-reviews');
56
        return $options;
57
    }
58
59
    /**
60
     * Used when the UM "Custom usermeta table" setting is disabled.
61
     *
62
     * @param string $sortby
63
     *
64
     * @filter um_modify_sortby_parameter
65
     */
66
    public function filterDirectorySortBy(array $queryArgs, $sortby): array
67
    {
68
        if (!in_array($sortby, ['glsr_highest_rated', 'glsr_lowest_rated'])) {
69
            return $queryArgs;
70
        }
71
        $order = 'glsr_highest_rated' === $sortby ? 'DESC' : 'ASC';
72
        $sortKey = 'bayesian' === glsr_get_option('integrations.ultimatemember.sorting')
73
            ? CountManager::META_RANKING
74
            : CountManager::META_AVERAGE;
75
        $queryArgs['meta_query'] ??= [];
76
        $queryArgs['meta_query'][] = [
77
            'relation' => 'OR',
78
            [
79
                'compare' => 'NOT EXISTS',
80
                'key' => $sortKey,
81
                'type' => 'NUMERIC',
82
            ],
83
            '_rating_key' => [
84
                'compare' => 'EXISTS',
85
                'key' => $sortKey,
86
                'type' => 'NUMERIC',
87
            ],
88
        ];
89
        $queryArgs['orderby'] = [
90
            '_rating_key' => $order,
91
            'user_registered' => 'DESC',
92
        ];
93
        unset($queryArgs['order']);
94
        return $queryArgs;
95
    }
96
97
    /**
98
     * Used when the UM "Custom usermeta table" setting is enabled.
99
     *
100
     * @param \um\core\Member_Directory_Meta $query
0 ignored issues
show
Bug introduced by
The type um\core\Member_Directory_Meta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
101
     * @param array                          $directoryData
102
     * @param string                         $sortby
103
     *
104
     * @action um_pre_users_query
105
     */
106
    public function modifyQuerySortby($query, $directoryData, $sortby): void
1 ignored issue
show
Unused Code introduced by
The parameter $directoryData is not used and could be removed. ( Ignorable by Annotation )

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

106
    public function modifyQuerySortby($query, /** @scrutinizer ignore-unused */ $directoryData, $sortby): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        if (!in_array($sortby, ['glsr_highest_rated', 'glsr_lowest_rated'])) {
109
            return;
110
        }
111
        $order = esc_sql('glsr_highest_rated' === $sortby ? 'DESC' : 'ASC');
112
        $sortKey = 'bayesian' === glsr_get_option('integrations.ultimatemember.sorting')
113
            ? CountManager::META_RANKING
114
            : CountManager::META_AVERAGE;
115
        $query->joins[] = glsr(Query::class)->sql(
116
            "LEFT JOIN table|usermeta AS glsr_usermeta ON (glsr_usermeta.user_id = u.ID AND glsr_usermeta.meta_key = %s)",
117
            $sortKey
118
        );
119
        $query->sql_order = " ORDER BY CAST(glsr_usermeta.meta_value AS SIGNED) {$order}, u.user_registered DESC";
120
    }
121
122
    /**
123
     * @param array $args
124
     *
125
     * @action um_members_just_after_name_tmpl
126
     * @action um_members_list_after_user_name_tmpl
127
     */
128
    public function modifyTmpl($args): void
129
    {
130
        $displayRating = Arr::get($args, 'glsr_display_user_rating');
131
        if (empty($displayRating)) {
132
            return;
133
        }
134
        echo "<# if ('undefined' !== typeof user.glsr_rating_html) { #>{{{user.glsr_rating_html}}}<# } #>";
135
    }
136
}
137