Passed
Push — main ( 3cfdb8...a45320 )
by PRATIK
10:34
created

PostPreference::getCookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Adminetic\Website\Services;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use Illuminate\Support\Facades\Cookie;
8
9
class PostPreference
10
{
11
    public static function trackVisitedPost(Request $request, $post_id)
12
    {
13
        $visited_posts = array();
14
        if (isset($post_id)) {
15
            if (self::checkCookieConsent()) {
16
                if (self::getCookie($request) !== null) {
17
                    $visited_posts = self::getCookie($request);
18
                    array_push($visited_posts, $post_id);
19
                } else {
20
                    array_push($visited_posts, $post_id);
21
                    self::setCookie(json_encode($visited_posts));
22
                }
23
            }
24
        }
25
    }
26
27
28
    // Get Visited Posts ID
29
    public static function getVisitedPostsID(Request $request)
30
    {
31
        return self::getCookie($request);
32
    }
33
34
    // Check Cookie Consent
35
    protected static function checkCookieConsent(): bool
36
    {
37
        return (bool) Cookie::get('laravel_cookie_consent');
38
    }
39
40
    protected static function setCookie($value)
41
    {
42
        $response = new Response('Set Cookie');
43
        $response->withCookie(cookie()->forever('visited_posts', $value));
44
        return $response;
45
    }
46
47
    protected static function getCookie(Request $request)
48
    {
49
        return json_decode($request->cookie('visited_posts'));
0 ignored issues
show
Bug introduced by
It seems like $request->cookie('visited_posts') can also be of type array and null; however, parameter $json of json_decode() does only seem to accept string, 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

49
        return json_decode(/** @scrutinizer ignore-type */ $request->cookie('visited_posts'));
Loading history...
50
    }
51
}
52