Completed
Pull Request — master (#86)
by Renato
02:33
created

Helper::arrayDateRange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace GitScrum\Classes;
4
5
use Carbon\Carbon;
6
use Auth;
7
8
class Helper
9
{
10
    public static function arrayDateRange($between, $value = 0)
11
    {
12
        $arr = [];
13
14
        for ($i = strtotime($between[0]); $i <= strtotime($between[1]); $i = $i + 86400) {
15
            $date = date('Y-m-d', $i);
16
            $arr[$date] = $value;
17
        }
18
19
        return $arr;
20
    }
21
22
    public static function slug($value)
23
    {
24
        return str_slug(substr($value, 0, 40).'-'.uniqid());
25
    }
26
27
    public static function percentage($model, $feature)
28
    {
29
        if (isset($model->{$feature})) {
30
            $total = $model->{$feature}->count();
31
            $totalClosed = $model->{$feature}->where('closed_at', '!=', null)->count();
32
33
            return ($totalClosed) ? ceil(($totalClosed * 100) / $total) : 0;
34
        }
35
36
        return 0;
37
    }
38
39
    public static function burndown($obj, $subDays = null)
40
    {
41
        $total = $obj->issues->count();
42
        $arr = [];
43
        if ($total) {
44
            if (is_null($subDays)) {
45
                $started = $obj->date_start;
46
                $finished = (Carbon::now() > $obj->date_finish) ?
47
                    (is_null($obj->closed_at) ? Carbon::now() : $obj->closed_at) : $obj->date_finish;
48
            } else {
49
                $dt = Carbon::now();
50
                $started = $dt->subDays($subDays)->toDateString();
51
                $finished = $dt->addDays($subDays + 1)->toDateString();
52
            }
53
54
            $dates = self::arrayDateRange([$started, $finished], $total);
55
56
            $previous = $started;
57
            $arr[$previous] = $total;
58
59
            foreach ($dates as $date => $value) {
60
                $closed = $obj->issues()->whereDate('closed_at', '=', $date)->get()->count();
61
                $totalPrevious = $total - $arr[$previous];
62
                $arr[$date] = $total - ($closed + $totalPrevious);
63
                $previous = $date;
64
            }
65
        }
66
67
        return $arr;
68
    }
69
70
    public static function request($url, $auth = true, $customRequest = null, $postFields = null)
71
    {
72
        $user = Auth::user();
73
        $ch = curl_init();
74
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0');
75
        curl_setopt($ch, CURLOPT_URL, $url);
76
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
77
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
78
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
79
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
80
81
        if (env('PROXY_PORT')) {
82
            curl_setopt($ch, CURLOPT_PROXYPORT, env('PROXY_PORT'));
83
            curl_setopt($ch, CURLOPT_PROXYTYPE, env('PROXY_METHOD'));
84
            curl_setopt($ch, CURLOPT_PROXY, env('PROXY_SERVER'));
85
        }
86
87
        if (env('PROXY_USER')) {
88
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, env('PROXY_USER').':'.env('PROXY_USER'));
89
        }
90
91
        if (!is_null($postFields)) {
92
            $postFields = json_encode($postFields);
93
            curl_setopt($ch, CURLOPT_POST, true);
94
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
95
            curl_setopt($ch, CURLOPT_HTTPHEADER,  ['Content-Type: application/json',
96
                'Content-Length: '.strlen($postFields), ]);
97
        }
98
99
        //curl_setopt($ch, CURLOPT_HTTPHEADER,  ['Authorization: Bearer OAUTH-TOKEN']);
100
101
        if (!is_null($customRequest)) {
102
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $customRequest); //'PATCH'
103
        }
104
105
        if ($auth && isset($user->username)) {
106
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
107
            curl_setopt($ch, CURLOPT_USERPWD, $user->username.':'.$user->token);
108
        }
109
110
        $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Unused Code introduced by
$status_code is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
        $result = curl_exec($ch);
112
        curl_close($ch);
113
114
        return json_decode($result);
115
    }
116
}
117