Passed
Branch master (2fcc75)
by Paul
11:46
created

TogglePinned::handle()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 5
eloc 15
c 3
b 0
f 2
nc 5
nop 1
dl 0
loc 20
ccs 0
cts 20
cp 0
crap 30
rs 9.4555
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Handlers;
4
5
use GeminiLabs\SiteReviews\Commands\TogglePinned as Command;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Modules\Notice;
8
9
class TogglePinned
10
{
11
    /**
12
     * @return bool
13
     */
14
    public function handle(Command $command)
15
    {
16
        if (!get_post($command->id)) {
17
            return false;
18
        }
19
        if (!glsr()->can('edit_others_posts')) {
20
            $meta = (Database::class)->get($command->id, 'pinned');
21
            return wp_validate_boolean($meta);
22
        }
23
        if (is_null($command->pinned)) {
24
            $meta = glsr(Database::class)->get($command->id, 'pinned');
25
            $command->pinned = !wp_validate_boolean($meta);
26
        } else {
27
            $notice = $command->pinned
28
                ? __('Review pinned.', 'site-reviews')
29
                : __('Review unpinned.', 'site-reviews');
30
            glsr(Notice::class)->addSuccess($notice);
31
        }
32
        glsr(Database::class)->update($command->id, 'pinned', $command->pinned);
33
        return $command->pinned;
34
    }
35
}
36