Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Stats::makeGuzzleRequest()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 3
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD;
4
5
trait Stats
6
{
7
    /**
8
     * Check if the application is running in normal conditions
9
     * (production env, not in console, not in unit tests).
10
     *
11
     * @return void
12
     */
13
    private function runningInProduction()
14
    {
15
        if ($this->app->environment('local')) {
16
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
17
        }
18
19
        if ($this->app->runningInConsole()) {
20
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
21
        }
22
23
        if ($this->app->runningUnitTests()) {
24
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
25
        }
26
27
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
28
    }
29
30
    /**
31
     * Send usage statistics to the BackpackForLaravel.com website.
32
     * Used to track unlicensed usage and general usage statistics.
33
     *
34
     * No GDPR implications, since no client info is send, only server info.
35
     *
36
     * @return void
37
     */
38
    private function sendUsageStats()
39
    {
40
        // only send usage stats in production
41
        if (! $this->runningInProduction()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->runningInProduction() targeting Backpack\CRUD\Stats::runningInProduction() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
            return;
43
        }
44
45
        // only send stats every ~100 pageloads
46
        if (rand(1, 100) != 1) {
47
            return;
48
        }
49
50
        $url = 'https://backpackforlaravel.com/api/stats';
51
        $method = 'PUT';
52
        $stats = [
53
            'URL' => url('') ?? false,
54
            'HTTP_HOST' => $_SERVER['HTTP_HOST'] ?? false,
55
            'APP_URL' => $_SERVER['APP_URL'] ?? false,
56
            'APP_ENV' => $this->app->environment() ?? false,
57
            'APP_DEBUG' => $_SERVER['APP_DEBUG'] ?? false,
58
            'SERVER_ADDR' => $_SERVER['SERVER_ADDR'] ?? false,
59
            'SERVER_ADMIN' => $_SERVER['SERVER_ADMIN'] ?? false,
60
            'SERVER_NAME' => $_SERVER['SERVER_NAME'] ?? false,
61
            'SERVER_PORT' => $_SERVER['SERVER_PORT'] ?? false,
62
            'SERVER_PROTOCOL' => $_SERVER['SERVER_PROTOCOL'] ?? false,
63
            'SERVER_SOFTWARE' => $_SERVER['SERVER_SOFTWARE'] ?? false,
64
            'DB_CONNECTION' => $_SERVER['DB_CONNECTION'] ?? false,
65
            'LARAVEL_VERSION' => $this->app->version() ?? false,
66
            'BACKPACK_CRUD_VERSION' => \Composer\InstalledVersions::getVersion('backpack/crud') ?? false,
67
            'BACKPACK_PRO_VERSION' => backpack_pro(),
68
            'BACKPACK_LICENSE' => config('backpack.base.license_code') ?? false,
69
            'BACKPACK_TOKEN_USERNAME' => config('backpack.base.token_username') ?? false,
70
            'BACKPACK_URL' => backpack_url('') ?? false,
71
        ];
72
73
        // send this info to the main website to store it in the db
74
        if (function_exists('exec') && extension_loaded('curl')) {
75
            $this->makeCurlRequest($method, $url, $stats);
76
        } else {
77
            $this->makeGuzzleRequest($method, $url, $stats);
78
        }
79
    }
80
81
    /**
82
     * Make a request using CURL.
83
     *
84
     * It spins up a separate process for this, and doesn't listen for a response,
85
     * so it has minimal to no impact on pageload.
86
     *
87
     * @param  string  $method  HTTP Method to use for the request.
88
     * @param  string  $url  URL to point the request at.
89
     * @param  array  $payload  The data you want sent to the URL.
90
     * @return void
91
     */
92
    private function makeCurlRequest($method, $url, $payload)
93
    {
94
        $cmd = 'curl -X '.$method." -H 'Content-Type: application/json'";
95
        $cmd .= " -d '".json_encode($payload)."' "."'".$url."'";
96
        $cmd .= ' > /dev/null 2>&1 &';
97
98
        exec($cmd, $output, $exit);
99
100
        return $exit == 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $exit == 0 returns the type boolean which is incompatible with the documented return type void.
Loading history...
101
    }
102
103
    /**
104
     * Make a request using Guzzle.
105
     *
106
     * This request happens in the same process as the page load,
107
     * and Guzzle listens for a response, so depending on the API latency and
108
     * geographic location this is usually slower than CURL. However,
109
     * unlike CURL, it works on most machines, so it's reliable.
110
     *
111
     * @param  string  $method  HTTP Method to use for the request.
112
     * @param  string  $url  URL to point the request at.
113
     * @param  array  $payload  The data you want sent to the URL.
114
     * @return void
115
     */
116
    private function makeGuzzleRequest($method, $url, $payload)
117
    {
118
        try {
119
            $client = new \GuzzleHttp\Client();
120
            $res = $client->request($method, $url, [
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
121
                'form_params' => $payload,
122
                'http_errors' => false,
123
                'connect_timeout' => 0.5,
124
                'timeout' => 0.5,
125
            ]);
126
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
127
            // do nothing
128
        }
129
    }
130
}
131