LaravelFacebookPixel::createEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace WebLAgence\LaravelFacebookPixel;
4
5
use Illuminate\Support\Traits\Macroable;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Traits\Macroable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * Class LaravelFacebookPixel
9
 * @package WebLAgence\LaravelFacebookPixel
10
 */
11
class LaravelFacebookPixel
12
{
13
    use Macroable;
14
    
15
    /**
16
     * @var string
17
     */
18
    protected $id;
19
    /**
20
     * @var string
21
     */
22
    protected $cspNonceCallback;
23
24
    /**
25
     * LaravelFacebookPixel constructor.
26
     * @param $id
27
     */
28
    public function __construct($id)
29
    {
30
        $this->id = $id;
31
        $this->enabled = true;
0 ignored issues
show
Bug Best Practice introduced by
The property enabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        $this->cspNonceCallback = '';
33
    }
34
    
35
    /**
36
     * Return the Facebook Pixel id.
37
     *
38
     * @return string
39
     */
40
    public function id()
41
    {
42
        return $this->id;
43
    }
44
    
45
    /**
46
     * @param $id
47
     * @return $this
48
     */
49
    public function setId($id)
50
    {
51
        $this->id = $id;
52
        
53
        return $this;
54
    }
55
    
56
    /**
57
     * Enable Facebook Pixel scripts rendering.
58
     */
59
    public function enable()
60
    {
61
        $this->enabled = true;
0 ignored issues
show
Bug Best Practice introduced by
The property enabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
    }
63
    
64
    /**
65
     * Disable Facebook Pixel scripts rendering.
66
     */
67
    public function disable()
68
    {
69
        $this->enabled = false;
0 ignored issues
show
Bug Best Practice introduced by
The property enabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
    }
71
    
72
    /**
73
     * @return bool
74
     */
75
    public function isEnabled()
76
    {
77
        return $this->enabled;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getCspNonceCallback()
84
    {
85
        return $this->cspNonceCallback;
86
    }
87
88
    /**
89
     * @param string $callback
90
     */
91
    public function addCspNonce($callback)
92
    {
93
        if (!function_exists($callback)) {
94
            return;
95
        }
96
        $this->cspNonceCallback = $callback;
97
    }
98
    
99
    /**
100
     * @return string
101
     */
102
    public function bodyContent()
103
    {
104
        $facebookPixelSession = session()->pull('facebookPixelSession', []);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        $facebookPixelSession = /** @scrutinizer ignore-call */ session()->pull('facebookPixelSession', []);
Loading history...
105
        $pixelCode = "";
106
        if (count($facebookPixelSession) > 0) {
107
            foreach ($facebookPixelSession as $key => $facebookPixel) {
108
                $pixelCode .= "fbq('track', '" . $facebookPixel["name"] . "', " . json_encode($facebookPixel["parameters"]) . ");";
109
            };
110
            session()->forget('facebookPixelSession');
111
            if($this->cspNonceCallback) {
112
                return "<script nonce='" . call_user_func($this->cspNonceCallback) . "'>" . $pixelCode . "</script>";
113
            }
114
            return "<script>" . $pixelCode . "</script>";
115
        }
116
        
117
        return "";
118
    }
119
    
120
    /**
121
     * @param $eventName
122
     * @param array $parameters
123
     */
124
    public function createEvent($eventName, $parameters = [])
125
    {
126
        $facebookPixelSession = session('facebookPixelSession');
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        $facebookPixelSession = /** @scrutinizer ignore-call */ session('facebookPixelSession');
Loading history...
127
        $facebookPixelSession = !$facebookPixelSession ? [] : $facebookPixelSession;
128
        $facebookPixel = [
129
            "name"       => $eventName,
130
            "parameters" => $parameters,
131
        ];
132
        array_push($facebookPixelSession, $facebookPixel);
133
        session(['facebookPixelSession' => $facebookPixelSession]);
134
    }
135
}