AverageLeadCycleTimeAnalytic::getTasks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Analytic;
13
14
use Jitamin\Foundation\Base;
15
use Jitamin\Model\TaskModel;
16
17
/**
18
 * Average Lead and Cycle Time.
19
 */
20
class AverageLeadCycleTimeAnalytic extends Base
21
{
22
    /**
23
     * Build report.
24
     *
25
     * @param int $project_id Project id
26
     *
27
     * @return array
28
     */
29
    public function build($project_id)
30
    {
31
        $stats = [
32
            'count'            => 0,
33
            'total_lead_time'  => 0,
34
            'total_cycle_time' => 0,
35
            'avg_lead_time'    => 0,
36
            'avg_cycle_time'   => 0,
37
        ];
38
39
        $tasks = $this->getTasks($project_id);
40
41
        foreach ($tasks as &$task) {
42
            $stats['count']++;
43
            $stats['total_lead_time'] += $this->calculateLeadTime($task);
44
            $stats['total_cycle_time'] += $this->calculateCycleTime($task);
45
        }
46
47
        $stats['avg_lead_time'] = $this->calculateAverage($stats, 'total_lead_time');
48
        $stats['avg_cycle_time'] = $this->calculateAverage($stats, 'total_cycle_time');
49
50
        return $stats;
51
    }
52
53
    /**
54
     * Calculate average.
55
     *
56
     * @param array  &$stats
57
     * @param string $field
58
     *
59
     * @return float
60
     */
61
    private function calculateAverage(array &$stats, $field)
62
    {
63
        if ($stats['count'] > 0) {
64
            return (int) ($stats[$field] / $stats['count']);
65
        }
66
67
        return 0;
68
    }
69
70
    /**
71
     * Calculate lead time.
72
     *
73
     * @param array &$task
74
     *
75
     * @return int
76
     */
77
    private function calculateLeadTime(array &$task)
78
    {
79
        $end = $task['date_completed'] ?: time();
80
        $start = $task['date_creation'];
81
82
        return $end - $start;
83
    }
84
85
    /**
86
     * Calculate cycle time.
87
     *
88
     * @param array &$task
89
     *
90
     * @return int
91
     */
92
    private function calculateCycleTime(array &$task)
93
    {
94
        $end = (int) $task['date_completed'] ?: time();
95
        $start = (int) $task['date_started'];
96
97
        // Start date can be in the future when defined with the Gantt chart
98
        if ($start > 0 && $end > $start) {
99
            return $end - $start;
100
        }
101
102
        return 0;
103
    }
104
105
    /**
106
     * Get the 1000 last created tasks.
107
     *
108
     * @param int $project_id
109
     *
110
     * @return array
111
     */
112 View Code Duplication
    private function getTasks($project_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        return $this->db
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Analytic\...eLeadCycleTimeAnalytic>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
            ->table(TaskModel::TABLE)
116
            ->columns('date_completed', 'date_creation', 'date_started')
117
            ->eq('project_id', $project_id)
118
            ->desc('id')
119
            ->limit(1000)
120
            ->findAll();
121
    }
122
}
123