1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GitScrum\Classes; |
4
|
|
|
|
5
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Auth; |
8
|
|
|
|
9
|
|
|
class Helper |
10
|
|
|
{ |
11
|
|
|
public static function arrayDateRange($between, $value = 0) |
12
|
|
|
{ |
13
|
|
|
$arr = []; |
14
|
|
|
|
15
|
|
|
for ($i = strtotime($between[0]); $i <= strtotime($between[1]); $i = $i + 86400) { |
16
|
|
|
$date = date('Y-m-d', $i); |
17
|
|
|
$arr[$date] = $value; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return $arr; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function slug($value) |
24
|
|
|
{ |
25
|
|
|
return str_slug(substr($value, 0, 40).'-'.uniqid()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function percentage($model, $feature) |
29
|
|
|
{ |
30
|
|
|
if (isset($model->{$feature})) { |
31
|
|
|
$total = $model->{$feature}->count(); |
32
|
|
|
$totalClosed = $model->{$feature}->where('closed_at', '!=', null)->count(); |
33
|
|
|
|
34
|
|
|
return ($totalClosed) ? ceil(($totalClosed * 100) / $total) : 0; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return 0; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function burndown($obj, $subDays = null) |
41
|
|
|
{ |
42
|
|
|
$total = $obj->issues->count(); |
43
|
|
|
$arr = []; |
44
|
|
|
if ($total) { |
45
|
|
|
if (is_null($subDays)) { |
46
|
|
|
$started = $obj->date_start; |
47
|
|
|
$finished = (Carbon::now() > $obj->date_finish) ? |
48
|
|
|
(is_null($obj->closed_at) ? Carbon::now() : $obj->closed_at) : $obj->date_finish; |
49
|
|
|
} else { |
50
|
|
|
$dt = Carbon::now(); |
51
|
|
|
$started = $dt->subDays($subDays)->toDateString(); |
52
|
|
|
$finished = $dt->addDays($subDays + 1)->toDateString(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$dates = self::arrayDateRange([$started, $finished], $total); |
56
|
|
|
|
57
|
|
|
$previous = $started; |
58
|
|
|
$arr[$previous] = $total; |
59
|
|
|
|
60
|
|
|
foreach ($dates as $date => $value) { |
61
|
|
|
$closed = $obj->issues()->whereDate('closed_at', '=', $date)->get()->count(); |
62
|
|
|
$totalPrevious = $total - $arr[$previous]; |
63
|
|
|
$arr[$date] = $total - ($closed + $totalPrevious); |
64
|
|
|
$previous = $date; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $arr; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function request($url, $auth = true, $customRequest = null, $postFields = null) |
72
|
|
|
{ |
73
|
|
|
$user = Auth::user(); |
74
|
|
|
$ch = curl_init(); |
75
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'); |
76
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
77
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
78
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
79
|
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true); |
80
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
81
|
|
|
|
82
|
|
|
if (env('PROXY_PORT')) { |
83
|
|
|
curl_setopt($ch, CURLOPT_PROXYPORT, env('PROXY_PORT')); |
84
|
|
|
curl_setopt($ch, CURLOPT_PROXYTYPE, env('PROXY_METHOD')); |
85
|
|
|
curl_setopt($ch, CURLOPT_PROXY, env('PROXY_SERVER')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (env('PROXY_USER')) { |
89
|
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, env('PROXY_USER').':'.env('PROXY_USER')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!is_null($postFields)) { |
93
|
|
|
$postFields = json_encode($postFields); |
94
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
95
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
96
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', |
97
|
|
|
'Content-Length: '.strlen($postFields), ]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
//curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer OAUTH-TOKEN']); |
101
|
|
|
|
102
|
|
|
if (!is_null($customRequest)) { |
103
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $customRequest); //'PATCH' |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($auth && isset($user->username)) { |
107
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
108
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $user->username.':'.$user->token); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
|
|
|
112
|
|
|
$result = curl_exec($ch); |
113
|
|
|
curl_close($ch); |
114
|
|
|
|
115
|
|
|
return json_decode($result); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function lengthAwarePaginator($collection, $page = 1) |
119
|
|
|
{ |
120
|
|
|
$page = intval($page)?intval($page):1; |
121
|
|
|
return new LengthAwarePaginator($collection->forPage($page, env('APP_PAGINATE')), |
122
|
|
|
$collection->count(), env('APP_PAGINATE')); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.