ProjectDailyColumnStatsModel::countDays()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 8
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\Model;
13
14
use Jitamin\Foundation\Database\Model;
15
16
/**
17
 * Project Daily Column Stats.
18
 */
19
class ProjectDailyColumnStatsModel extends Model
20
{
21
    /**
22
     * SQL table name.
23
     *
24
     * @var string
25
     */
26
    const TABLE = 'project_daily_column_stats';
27
28
    /**
29
     * Update daily totals for the project and for each column.
30
     *
31
     * "total" is the number open of tasks in the column
32
     * "score" is the sum of tasks score in the column
33
     *
34
     * @param int    $project_id Project id
35
     * @param string $date       Record date (YYYY-MM-DD)
36
     *
37
     * @return bool
38
     */
39 View Code Duplication
    public function updateTotals($project_id, $date)
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...
40
    {
41
        $this->db->startTransaction();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
42
        $this->db->table(self::TABLE)->eq('project_id', $project_id)->eq('day', $date)->remove();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
43
44
        foreach ($this->getStatsByColumns($project_id) as $column_id => $column) {
45
            $this->db->table(self::TABLE)->insert([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
46
                'day'        => $date,
47
                'project_id' => $project_id,
48
                'column_id'  => $column_id,
49
                'total'      => $column['total'],
50
                'score'      => $column['score'],
51
            ]);
52
        }
53
54
        $this->db->closeTransaction();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
55
56
        return true;
57
    }
58
59
    /**
60
     * Count the number of recorded days for the data range.
61
     *
62
     * @param int    $project_id Project id
63
     * @param string $from       Start date (ISO format YYYY-MM-DD)
64
     * @param string $to         End date
65
     *
66
     * @return int
67
     */
68
    public function countDays($project_id, $from, $to)
69
    {
70
        return $this->db->table(self::TABLE)
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
71
            ->eq('project_id', $project_id)
72
            ->gte('day', $from)
73
            ->lte('day', $to)
74
            ->findOneColumn('COUNT(DISTINCT day)');
75
    }
76
77
    /**
78
     * Get aggregated metrics for the project within a data range.
79
     *
80
     * [
81
     *    ['Date', 'Column1', 'Column2'],
82
     *    ['2014-11-16', 2, 5],
83
     *    ['2014-11-17', 20, 15],
84
     * ]
85
     *
86
     * @param int    $project_id Project id
87
     * @param string $from       Start date (ISO format YYYY-MM-DD)
88
     * @param string $to         End date
89
     * @param string $field      Column to aggregate
90
     *
91
     * @return array
92
     */
93
    public function getAggregatedMetrics($project_id, $from, $to, $field = 'total')
94
    {
95
        $columns = $this->columnModel->getList($project_id);
0 ignored issues
show
Documentation introduced by
The property columnModel does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
96
        $metrics = $this->getMetrics($project_id, $from, $to);
97
98
        return $this->buildAggregate($metrics, $columns, $field);
99
    }
100
101
    /**
102
     * Fetch metrics.
103
     *
104
     * @param int    $project_id Project id
105
     * @param string $from       Start date (ISO format YYYY-MM-DD)
106
     * @param string $to         End date
107
     *
108
     * @return array
109
     */
110
    public function getMetrics($project_id, $from, $to)
111
    {
112
        return $this->db->table(self::TABLE)
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
113
            ->eq('project_id', $project_id)
114
            ->gte('day', $from)
115
            ->lte('day', $to)
116
            ->asc(self::TABLE.'.day')
117
            ->findAll();
118
    }
119
120
    /**
121
     * Build aggregate.
122
     *
123
     * @param array  $metrics
124
     * @param array  $columns
125
     * @param string $field
126
     *
127
     * @return array
128
     */
129
    private function buildAggregate(array &$metrics, array &$columns, $field)
130
    {
131
        $column_ids = array_keys($columns);
132
        $days = array_unique(array_column($metrics, 'day'));
133
        $rows = [array_merge([l('Date')], array_values($columns))];
134
135
        foreach ($days as $day) {
136
            $rows[] = $this->buildRowAggregate($metrics, $column_ids, $day, $field);
137
        }
138
139
        return $rows;
140
    }
141
142
    /**
143
     * Build one row of the aggregate.
144
     *
145
     * @param array  $metrics
146
     * @param array  $column_ids
147
     * @param string $day
148
     * @param string $field
149
     *
150
     * @return array
151
     */
152
    private function buildRowAggregate(array &$metrics, array &$column_ids, $day, $field)
153
    {
154
        $row = [$day];
155
156
        foreach ($column_ids as $column_id) {
157
            $row[] = $this->findValueInMetrics($metrics, $day, $column_id, $field);
158
        }
159
160
        return $row;
161
    }
162
163
    /**
164
     * Find the value in the metrics.
165
     *
166
     * @param array  $metrics
167
     * @param string $day
168
     * @param string $column_id
169
     * @param string $field
170
     *
171
     * @return int
172
     */
173
    private function findValueInMetrics(array &$metrics, $day, $column_id, $field)
174
    {
175
        foreach ($metrics as $metric) {
176
            if ($metric['day'] === $day && $metric['column_id'] == $column_id) {
177
                return (int) $metric[$field];
178
            }
179
        }
180
181
        return 0;
182
    }
183
184
    /**
185
     * Get number of tasks and score by columns.
186
     *
187
     * @param int $project_id
188
     *
189
     * @return array
190
     */
191
    private function getStatsByColumns($project_id)
192
    {
193
        $totals = $this->getTotalByColumns($project_id);
194
        $scores = $this->getScoreByColumns($project_id);
195
        $columns = [];
196
197
        foreach ($totals as $column_id => $total) {
198
            $columns[$column_id] = ['total' => $total, 'score' => 0];
199
        }
200
201
        foreach ($scores as $column_id => $score) {
202
            $columns[$column_id]['score'] = (int) $score;
203
        }
204
205
        return $columns;
206
    }
207
208
    /**
209
     * Get number of tasks and score by columns.
210
     *
211
     * @param int $project_id
212
     *
213
     * @return array
214
     */
215 View Code Duplication
    private function getScoreByColumns($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...
216
    {
217
        $stats = $this->db->table(TaskModel::TABLE)
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
218
            ->columns('column_id', 'SUM(score) AS score')
219
            ->eq('project_id', $project_id)
220
            ->eq('is_active', TaskModel::STATUS_OPEN)
221
            ->notNull('score')
222
            ->groupBy('column_id')
223
            ->findAll();
224
225
        return array_column($stats, 'score', 'column_id');
226
    }
227
228
    /**
229
     * Get number of tasks and score by columns.
230
     *
231
     * @param int $project_id
232
     *
233
     * @return array
234
     */
235 View Code Duplication
    private function getTotalByColumns($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...
236
    {
237
        $stats = $this->db->table(TaskModel::TABLE)
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
238
            ->columns('column_id', 'COUNT(*) AS total')
239
            ->eq('project_id', $project_id)
240
            ->in('is_active', $this->getTaskStatusConfig())
241
            ->groupBy('column_id')
242
            ->findAll();
243
244
        return array_column($stats, 'total', 'column_id');
245
    }
246
247
    /**
248
     * Get task status to use for total calculation.
249
     *
250
     * @return array
251
     */
252
    private function getTaskStatusConfig()
253
    {
254
        if ($this->settingModel->get('cfd_include_closed_tasks') == 1) {
0 ignored issues
show
Documentation introduced by
The property settingModel does not exist on object<Jitamin\Model\Pro...tDailyColumnStatsModel>. 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...
255
            return [TaskModel::STATUS_OPEN, TaskModel::STATUS_CLOSED];
256
        }
257
258
        return [TaskModel::STATUS_OPEN];
259
    }
260
}
261