Passed
Pull Request — master (#2)
by
unknown
06:28
created

LaravelFacebookPixel::addCspNonce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
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 headContent()
103
    {
104
        if ($this->cspNonceCallback) {
105
            $row = "<script nonce='" . call_user_func($this->cspNonceCallback) . "'>";
106
        } else {
107
            $row = "<script>";
108
        }
109
        return "<!-- Facebook Pixel Code -->".
110
            $row.
111
            "!function(f,b,e,v,n,t,s)
112
              {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
113
                  n.callMethod.apply(n,arguments):n.queue.push(arguments)};
114
                  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
115
                  n.queue=[];t=b.createElement(e);t.async=!0;
116
                  t.src=v;s=b.getElementsByTagName(e)[0];
117
                  s.parentNode.insertBefore(t,s)}(window, document,'script',
118
              'https://connect.facebook.net/en_US/fbevents.js');
119
              fbq('init', '" . $this->id . "');
120
              fbq('track', 'PageView');
121
            </script>
122
            <noscript><img height='1' width='1' style='display:none'
123
            src='https://www.facebook.com/tr?id=" . $this->id . "&ev=PageView&noscript=1'
124
            /></noscript>
125
            <!-- End Facebook Pixel Code -->";
126
    }
127
    
128
    /**
129
     * @return string
130
     */
131
    public function bodyContent()
132
    {
133
        $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

133
        $facebookPixelSession = /** @scrutinizer ignore-call */ session()->pull('facebookPixelSession', []);
Loading history...
134
        $pixelCode = "";
135
        if (count($facebookPixelSession) > 0) {
136
            foreach ($facebookPixelSession as $key => $facebookPixel) {
137
                $pixelCode .= "fbq('track', '" . $facebookPixel["name"] . "', " . json_encode($facebookPixel["parameters"]) . ");";
138
            };
139
            session()->forget('facebookPixelSession');
140
            if($this->cspNonceCallback) {
141
                return "<script nonce='" . call_user_func($this->cspNonceCallback) . "'>" . $pixelCode . "</script>";
142
            }
143
            return "<script>" . $pixelCode . "</script>";
144
        }
145
        
146
        return "";
147
    }
148
    
149
    /**
150
     * @param $eventName
151
     * @param array $parameters
152
     */
153
    public function createEvent($eventName, $parameters = [])
154
    {
155
        $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

155
        $facebookPixelSession = /** @scrutinizer ignore-call */ session('facebookPixelSession');
Loading history...
156
        $facebookPixelSession = !$facebookPixelSession ? [] : $facebookPixelSession;
157
        $facebookPixel = [
158
            "name"       => $eventName,
159
            "parameters" => $parameters,
160
        ];
161
        array_push($facebookPixelSession, $facebookPixel);
162
        session(['facebookPixelSession' => $facebookPixelSession]);
163
    }
164
}