Completed
Push — master ( 921459...7217ff )
by Shagiakhmetov
02:25
created

ProfileMethodListPage::sortList()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * A page with a list of all methods of the snapshot
5
 * @maintainer Timur Shagiakhmetov <[email protected]>
6
 */
7
8
namespace Badoo\LiveProfilerUI\Pages;
9
10
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodInterface;
11
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodDataInterface;
12
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodTreeInterface;
13
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SnapshotInterface;
14
use Badoo\LiveProfilerUI\FieldList;
15
use Badoo\LiveProfilerUI\Interfaces\ViewInterface;
16
17
class ProfileMethodListPage extends BasePage
18
{
19
    /** @var string */
20
    protected static $template_path = 'profile_method_list';
21
    /** @var SnapshotInterface */
22
    protected $Snapshot;
23
    /** @var MethodInterface */
24
    protected $Method;
25
    /** @var MethodTreeInterface */
26
    protected $MethodTree;
27
    /** @var MethodDataInterface */
28
    protected $MethodData;
29
    /** @var FieldList */
30
    protected $FieldList;
31
    /** @var string */
32
    protected $calls_count_field = '';
33
34 1
    public function __construct(
35
        ViewInterface $View,
36
        SnapshotInterface $Snapshot,
37
        MethodInterface $Method,
38
        MethodTreeInterface $MethodTree,
39
        MethodDataInterface $MethodData,
40
        FieldList $FieldList,
41
        string $calls_count_field
42
    ) {
43 1
        $this->View = $View;
44 1
        $this->Snapshot = $Snapshot;
45 1
        $this->Method = $Method;
46 1
        $this->MethodTree = $MethodTree;
47 1
        $this->MethodData = $MethodData;
48 1
        $this->FieldList = $FieldList;
49 1
        $this->calls_count_field = $calls_count_field;
50 1
    }
51
52 2
    protected function cleanData() : bool
53
    {
54 2
        $this->data['app'] = isset($this->data['app']) ? trim($this->data['app']) : '';
55 2
        $this->data['label'] = isset($this->data['label']) ? trim($this->data['label']) : '';
56 2
        $this->data['snapshot_id'] = isset($this->data['snapshot_id']) ? (int)$this->data['snapshot_id'] : 0;
57 2
        $this->data['all'] = isset($this->data['all']) ? (bool)$this->data['all'] : false;
58
59 2
        if (!$this->data['snapshot_id'] && (!$this->data['app'] || !$this->data['label'])) {
60 1
            throw new \InvalidArgumentException('Empty snapshot_id, app and label');
61
        }
62
63 1
        return true;
64
    }
65
66
    /**
67
     * @return array
68
     * @throws \InvalidArgumentException
69
     */
70 4
    public function getTemplateData() : array
71
    {
72 4
        $link_base = '/profiler/tree-view.phtml?';
73 4
        $Snapshot = false;
74 4
        if ($this->data['snapshot_id']) {
75 1
            $Snapshot = $this->Snapshot->getOneById($this->data['snapshot_id']);
76
            $link_base .= 'snapshot_id=' . $this->data['snapshot_id'];
77 3
        } elseif ($this->data['app'] && $this->data['label']) {
78 2
            $Snapshot = $this->Snapshot->getOneByAppAndLabel($this->data['app'], $this->data['label']);
79 1
            $link_base .= 'app=' . urlencode($this->data['app']) . '&label=' . urlencode($this->data['label']);
80
        }
81
82 2
        if (empty($Snapshot)) {
83 1
            throw new \InvalidArgumentException('Can\'t get snapshot');
84
        }
85
86 1
        $all_fields = $this->data['all'] ?
87
            $this->FieldList->getAllFieldsWithVariations() :
88 1
            $this->FieldList->getFields();
89 1
        $fields = array_diff($this->FieldList->getFields(), [$this->calls_count_field]);
90 1
        $field_descriptions = $this->FieldList->getFieldDescriptions();
91
92 1
        $parents_data = $this->MethodTree->getSnapshotParentsData([$Snapshot->getId()]);
93 1
        $records = $this->MethodData->getDataBySnapshotId($Snapshot->getId());
94 1
        $records = $this->Method->injectMethodNames($records);
95
96 1
        foreach ($records as $Row) {
97
            /** @var \Badoo\LiveProfilerUI\Entity\MethodData $Row */
98 1
            $values = $Row->getValues();
99 1
            $values = array_intersect_key($values, array_flip($all_fields));
100
101 1
            foreach ($fields as $field) {
102 1
                $all_fields[$field . '_excl'] = $field . '_excl';
103 1
                $values[$field . '_excl'] = !empty($parents_data[$Row->getSnapshotId()][$Row->getMethodId()])
104
                    ? ($values[$field] - $parents_data[$Row->getSnapshotId()][$Row->getMethodId()][$field])
105 1
                    : 0;
106
            }
107 1
            $Row->setValues($values);
108
        }
109
110 1
        $this->sortList($records);
111
112 1
        $wall = $this->View->fetchFile(
113 1
            'profiler_result_view_part',
114
            [
115 1
                'data' => $records,
116 1
                'fields' => $all_fields,
117 1
                'field_descriptions' => $field_descriptions,
118 1
                'link_base' => $link_base
119
            ],
120 1
            false
121
        );
122
123
        return [
124 1
            'snapshot' => $Snapshot,
125 1
            'wall' => $wall,
126 1
            'all' => $this->data['all'],
127
        ];
128
    }
129
130 1
    protected function sortList(array &$records)
131
    {
132 1
        $sort_field = (string)current($this->FieldList->getFields());
133 1
        usort($records, function ($Element1, $Element2) use ($sort_field) : int {
134
            /** @var \Badoo\LiveProfilerUI\Entity\MethodData $Element1 */
135
            /** @var \Badoo\LiveProfilerUI\Entity\MethodData $Element2 */
136
            return $Element2->getValue($sort_field) > $Element1->getValue($sort_field) ? 1 : -1;
137 1
        });
138
    }
139
}
140