Completed
Pull Request — master (#73)
by Renato
02:37
created

Helper::slug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace GitScrum\Classes;
4
5
use Carbon\Carbon;
6
7
class Helper
8
{
9
    public static function arrayDateRange($between, $value = 0)
10
    {
11
        $arr = [];
12
13
        for ($i = strtotime($between[0]); $i <= strtotime($between[1]); $i = $i + 86400) {
14
            $date = date('Y-m-d', $i);
15
            $arr[$date] = $value;
16
        }
17
18
        return $arr;
19
    }
20
21
    public static function slug($value)
22
    {
23
        return str_slug(substr($value, 0, 40).'-'.uniqid());
24
    }
25
26
    public static function percentage($model, $feature)
27
    {
28
        if (isset($model->{$feature})) {
29
            $total = $model->{$feature}->count();
30
            $totalClosed = $model->{$feature}->where('closed_at', '!=', null)->count();
31
32
            return ($totalClosed) ? ceil(($totalClosed * 100) / $total) : 0;
33
        }
34
35
        return 0;
36
    }
37
38
    public static function burndown($obj, $subDays = null)
39
    {
40
        $total = $obj->issues->count();
41
        $arr = [];
42
        if ($total) {
43
            if (is_null($subDays)) {
44
                $started = $obj->date_start;
45
                $finished = (Carbon::now() > $obj->date_finish) ?
46
                    (is_null($obj->closed_at) ? Carbon::now() : $obj->closed_at) : $obj->date_finish;
47
            } else {
48
                $dt = Carbon::now();
49
                $started = $dt->subDays($subDays)->toDateString();
50
                $finished = $dt->addDays($subDays + 1)->toDateString();
51
            }
52
53
            $dates = self::arrayDateRange([$started, $finished], $total);
54
55
            $previous = $started;
56
            $arr[$previous] = $total;
57
58
            foreach ($dates as $date => $value) {
59
                $closed = $obj->issues()->whereDate('closed_at', '=', $date)->get()->count();
60
                $totalPrevious = $total - $arr[$previous];
61
                $arr[$date] = $total - ($closed + $totalPrevious);
62
                $previous = $date;
63
            }
64
        }
65
66
        return $arr;
67
    }
68
}
69