Completed
Push — master ( f24d71...81e9ea )
by
unknown
22s queued 10s
created

Cookiebot::getConsentFor()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 17
nop 1
1
<?php
2
3
namespace DigiFactory\CookieConsent\Providers;
4
5
use DigiFactory\CookieConsent\Contracts\ConsentProvider;
6
use DigiFactory\CookieConsent\CookieConsent;
7
use Illuminate\Support\Collection;
8
9
class Cookiebot implements ConsentProvider
10
{
11
    public function forNecessary(): bool
12
    {
13
        return $this->consentGiven()->contains(CookieConsent::CONSENT_NECESSARY);
14
    }
15
16
    public function forPreferences(): bool
17
    {
18
        return $this->consentGiven()->contains(CookieConsent::CONSENT_PREFERENCES);
19
    }
20
21
    public function forStatistics(): bool
22
    {
23
        return $this->consentGiven()->contains(CookieConsent::CONSENT_STATISTICS);
24
    }
25
26
    public function forMarketing(): bool
27
    {
28
        return $this->consentGiven()->contains(CookieConsent::CONSENT_MARKETING);
29
    }
30
31
    private function consentGiven(): Collection
32
    {
33
        // Most of this code is taken from: https://www.cookiebot.com/en/developer/
34
35
        if (isset($_COOKIE['CookieConsent'])) {
36
            // The user is not within a region that requires consent - all cookies are accepted
37
            if ($_COOKIE['CookieConsent'] === '-1') {
38
                return collect([
39
                    CookieConsent::CONSENT_NECESSARY,
40
                    CookieConsent::CONSENT_PREFERENCES,
41
                    CookieConsent::CONSENT_MARKETING,
42
                    CookieConsent::CONSENT_STATISTICS,
43
                ]);
44
            } else {
45
                //Read current user consent in encoded JavaScript format
46
                $phpJson = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"', stripslashes($_COOKIE['CookieConsent']))));
47
                $cookieConsent = json_decode($phpJson);
48
49
                return $this->getConsentFor($cookieConsent);
50
            }
51
        } else {
52
            //The user has not accepted cookies - set strictly necessary cookies only
53
            return collect(CookieConsent::CONSENT_NECESSARY);
54
        }
55
    }
56
57
    private function getConsentFor($cookieConsent): Collection
58
    {
59
        $consentFor = collect();
60
61
        if (is_object($cookieConsent)) {
62
            if (filter_var($cookieConsent->necessary, FILTER_VALIDATE_BOOLEAN)) {
63
                $consentFor->push(CookieConsent::CONSENT_NECESSARY);
64
            }
65
66
            if (filter_var($cookieConsent->preferences, FILTER_VALIDATE_BOOLEAN)) {
67
                $consentFor->push(CookieConsent::CONSENT_PREFERENCES);
68
            }
69
70
            if (filter_var($cookieConsent->statistics, FILTER_VALIDATE_BOOLEAN)) {
71
                $consentFor->push(CookieConsent::CONSENT_STATISTICS);
72
            }
73
74
            if (filter_var($cookieConsent->marketing, FILTER_VALIDATE_BOOLEAN)) {
75
                $consentFor->push(CookieConsent::CONSENT_MARKETING);
76
            }
77
        }
78
79
        return $consentFor;
80
    }
81
}
82