Completed
Branch barista (e00b71)
by
unknown
20:01 queued 09:17
created

StatusChangeNotice   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dismiss() 0 9 2
A isDismissed() 0 6 1
A getCurrentUser() 0 10 3
1
<?php
2
3
namespace EventEspresso\core\admin;
4
5
use EventEspresso\core\services\database\WordPressOption;
6
use RuntimeException;
7
use WP_User;
8
9
/**
10
 * Class StatusChangeNotice
11
 *
12
 * @author  Brent Christensen
13
 * @package EventEspresso\core\admin
14
 * @since   $VID:$
15
 */
16
class StatusChangeNotice extends WordPressOption
17
{
18
    public function __construct()
19
    {
20
        parent::__construct('ee_hide_status_change_notices_for_users', [], false);
21
    }
22
23
24
    /**
25
     * @return int
26
     * @throws RuntimeException
27
     */
28
    public function dismiss(): int
29
    {
30
        $user      = $this->getCurrentUser();
31
        $dismissed = (array) $this->loadOption();
32
        if (! in_array($user, $dismissed)) {
33
            $dismissed[] = $user;
34
        }
35
        return $this->updateOption($dismissed);
36
    }
37
38
39
    /**
40
     * @return bool
41
     * @throws RuntimeException
42
     */
43
    public function isDismissed(): bool
44
    {
45
        $user      = $this->getCurrentUser();
46
        $dismissed = (array) $this->loadOption();
47
        return in_array($user, $dismissed);
48
    }
49
50
51
    /**
52
     * @return string
53
     * @throws RuntimeException
54
     */
55
    private function getCurrentUser(): string
56
    {
57
        $user = wp_get_current_user();
58
        if (! $user instanceof WP_User || ! $user->exists()) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
59
            throw new RuntimeException(
60
                esc_html__('A valid WP User could not be retrieved.', 'event_espresso')
61
            );
62
        }
63
        return $user->user_login;
64
    }
65
}
66