CommentsReport::getReportAllProjectsAndDateRange()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace AlfredNutileInc\HPClient\Reports;
3
4
use Facades\AlfredNutileInc\HPClient\ProjectsQuery;
5
use Facades\AlfredNutileInc\HPClient\TimeSheets;
6
use Carbon\Carbon;
7
use AlfredNutileInc\HPClient\UserFromResource;
8
use Illuminate\Support\Collection;
9
10
class CommentsReport extends BaseReports
11
{
12
13
    use UserFromResource;
14
15
    /**
16
     * @var Collection $report
17
     */
18
    protected $report = [];
19
20
21
    protected $range = [];
22
23
    public function getReportAllProjectsAndDateRange($range = [])
24
    {
25
        $this->getResourcesFromApi();
26
27
        $this->range = $range;
28
        if (empty($this->range)) {
29
            $this->range['$gte'] = Carbon::now()->subDays(61)->toDateString();
30
        }
31
32
        $projects = ProjectsQuery::getAllProjects();
33
34
        $this->iterateOverProjects($projects);
35
36
        return collect($this->report)->sortByDesc('created_date')->toArray();
37
    }
38
39
    protected function iterateOverProjects($projects)
40
    {
41
        collect($projects)->map(
42
            function ($project) {
43
                $this->sleepToNotOverDoApiLimits();
44
                $project_trimmed =
45
                    $this->getTimeEntriesCommentsForProjectAndDateRange(
46
                        $project['_id'],
47
                        $this->range
48
                    );
49
                $results = collect($project_trimmed)->transform(
50
                    function ($item) {
51
                        $date = Carbon::parse(array_get($item, 'createdDate'))->format("m-d-Y");
52
                        $transformed = [];
53
                        $transformed['user_name'] = array_get($item, 'user_name');
54
                        $transformed['created_date'] = $date;
55
                        $transformed['project_name'] = array_get($item, 'projectName');
56
                        $transformed['note'] = array_get($item, 'note');
57
                        return $transformed;
58
                    }
59
                )->toArray();
60
                $this->report = array_merge($this->report, $results);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->report, $results) of type array is incompatible with the declared type object<Illuminate\Support\Collection> of property $report.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
            }
62
        );
63
    }
64
65
    public function getTimeEntriesCommentsForProjectAndDateRange(
66
        $project,
67
        array $range = []
68
    ) {
69
        $query = [];
70
71
        if ($project) {
72
            $query['project'] = $project;
73
        }
74
75
        if (!empty($range)) {
76
            if ($lte = array_get($range, '$lte')) {
77
                $range['$lte'] = $lte;
78
            }
79
            if ($gte = array_get($range, '$gte')) {
80
                $range['$gte'] = $gte;
81
            }
82
            $query['date'] = $range;
83
        }
84
85
        $results = TimeSheets::timeEntrySearch($query);
86
87
        $results = $this->transformResouceToResourceName($results, $this->resources);
88
89
        return $results;
90
    }
91
}
92