TelegrafJsonResponseFactory::arrangeGroups()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.9
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Lamoda\Metric\Responder\ResponseFactory;
4
5
use GuzzleHttp\Psr7\Response;
6
use Lamoda\Metric\Common\MetricInterface;
7
use Lamoda\Metric\Common\MetricSourceInterface;
8
use Lamoda\Metric\Responder\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
final class TelegrafJsonResponseFactory implements ResponseFactoryInterface
12
{
13
    private const CONTENT_TYPE = 'application/json';
14
15
    /** {@inheritdoc} */
16 2
    public function create(MetricSourceInterface $source, array $options = []): ResponseInterface
17
    {
18 2
        $normalizedGroups = [];
19 2
        $groups = $this->arrangeGroups($source, $options);
20 2
        foreach ($groups as $name => $group) {
21 2
            $normalizedGroups[$name] = $this->formatGroup($group, $options);
22
        }
23
24 2
        $normalizedGroups = array_values(array_filter($normalizedGroups));
25
26 2
        if (array_keys($normalizedGroups) === [0]) {
27 1
            $normalizedGroups = $normalizedGroups[0];
28
        }
29
30 2
        return new Response(
31 2
            200,
32 2
            ['Content-Type' => self::CONTENT_TYPE],
33 2
            json_encode($normalizedGroups)
34
        );
35
    }
36
37 2
    private function formatGroup(array $source, array $options): array
38
    {
39 2
        $result = [];
40 2
        $prefix = $options['prefix'] ?? '';
41
42 2
        foreach ($source as $metric) {
43 2
            $result[$prefix . $metric->getName()] = $metric->resolve();
44
        }
45
46 2
        if (!empty($options['propagate_tags'])) {
47 1
            $this->propagateTags($result, $source, $options['propagate_tags']);
48
        }
49
50 2
        return $result;
51
    }
52
53
    /**
54
     * @param array             $result
55
     * @param MetricInterface[] $source
56
     * @param string[]          $propagatedTags
57
     */
58 1
    private function propagateTags(array &$result, array $source, array $propagatedTags): void
59
    {
60 1
        foreach ($source as $metric) {
61 1
            foreach ($metric->getTags() as $tag => $value) {
62 1
                if (\in_array($tag, $propagatedTags, true)) {
63 1
                    $result[$tag] = (string) $value;
64
                }
65
            }
66
        }
67 1
    }
68
69
    /**
70
     * @param MetricSourceInterface $source
71
     * @param array                 $options
72
     *
73
     * @return MetricInterface[][]
74
     */
75 2
    private function arrangeGroups(MetricSourceInterface $source, array $options): array
76
    {
77 2
        $groups = [];
78 2
        if (empty($options['group_by_tags'])) {
79 1
            $groups[0] = iterator_to_array($source->getMetrics(), false);
80
81 1
            return $groups;
82
        }
83
84 1
        $tags = (array) $options['group_by_tags'];
85 1
        foreach ($source->getMetrics() as $metric) {
86 1
            $vector = [];
87 1
            foreach ($tags as $tag) {
88 1
                $vector[] = $metric->getTags()[$tag] ?? '__';
89
            }
90
91 1
            $groups[$this->createTagVector(...$vector)][] = $metric;
92
        }
93
94 1
        return array_values($groups);
95
    }
96
97 1
    private function createTagVector(string ...$values)
98
    {
99 1
        return implode(';', $values);
100
    }
101
}
102