retrieveSecondaryIndexes()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 5
nop 3
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Monolog Extensions
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/MonologExtensions/blob/master/LICENSE
11
 * @link http://github.com/graze/MonologExtensions
12
 */
13
14
namespace Graze\Monolog\Processor;
15
16
class DynamoDbSecondaryIndexProcessor
17
{
18
    /**
19
     * @var array
20
     */
21
    private $secondaryIndexes;
22
23
    /**
24
     * @param array $secondaryIndexes
25
     */
26 3
    public function __construct(array $secondaryIndexes = [])
27
    {
28 3
        $this->secondaryIndexes = $secondaryIndexes;
29 3
    }
30
31
    /**
32
     * Sets up secondary indexes for dynamodb table
33
     *
34
     * @param array $record
35
     *
36
     * @return array
37
     */
38 2
    public function __invoke(array $record)
39
    {
40 2
        $foundIndexes = $this->retrieveSecondaryIndexes($record, $this->secondaryIndexes);
41 2
        return array_merge($foundIndexes, $record);
42
    }
43
44
    /**
45
     * returns an array of secondary indexes as key-value pairs that exist in $record
46
     *
47
     * @param array $record
48
     * @param array $keys
49
     * @param array $foundKeys
50
     *
51
     * @return array $foundKeys
52
     */
53 2
    private function retrieveSecondaryIndexes(array $record, array $keys, array &$foundKeys = [])
54
    {
55 2
        foreach ($record as $key => $value) {
56 2
            if (in_array($key, $keys)) {
57 2
                $foundKeys[$key] = $value;
58
            }
59 2
            if (is_array($value)) {
60 2
                $this->retrieveSecondaryIndexes($value, $keys, $foundKeys);
61
            }
62
        }
63 2
        return $foundKeys;
64
    }
65
}
66