Completed
Push — master ( 083b48...45ae2e )
by Mark
08:45
created

Cookiebot   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A forNecessary() 0 4 1
A forPreferences() 0 4 1
A forStatistics() 0 4 1
A forMarketing() 0 4 1
B consentGiven() 0 43 7
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
                $consentFor = collect();
50
51
                if (filter_var($cookieConsent->necessary, FILTER_VALIDATE_BOOLEAN)) {
52
                    $consentFor->push(CookieConsent::CONSENT_NECESSARY);
53
                }
54
55
                if (filter_var($cookieConsent->preferences, FILTER_VALIDATE_BOOLEAN)) {
56
                    $consentFor->push(CookieConsent::CONSENT_PREFERENCES);
57
                }
58
59
                if (filter_var($cookieConsent->statistics, FILTER_VALIDATE_BOOLEAN)) {
60
                    $consentFor->push(CookieConsent::CONSENT_STATISTICS);
61
                }
62
63
                if (filter_var($cookieConsent->marketing, FILTER_VALIDATE_BOOLEAN)) {
64
                    $consentFor->push(CookieConsent::CONSENT_MARKETING);
65
                }
66
67
                return $consentFor;
68
            }
69
        } else {
70
            //The user has not accepted cookies - set strictly necessary cookies only
71
            return collect(CookieConsent::CONSENT_NECESSARY);
72
        }
73
    }
74
}
75