Passed
Push — master ( d8e957...a3e243 )
by Maksim
03:45
created

NormalizedKeysParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 17
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizedKey() 0 10 3
A getNormalizedKeyName() 0 3 1
A recoverBinaryKey() 0 6 2
A parseNormalizedKey() 0 9 4
1
<?php
2
3
namespace MaksimM\CompositePrimaryKeys\Http\Traits;
4
5
6
use Illuminate\Database\Eloquent\Model;
7
use MaksimM\CompositePrimaryKeys\Exceptions\WrongKeyException;
8
9
trait NormalizedKeysParser
10
{
11
12
    protected $magicKeyDelimiter = '___';
13
14
    /**
15
     * Get key-value array from normalized key.
16
     *
17
     * @param $normalizedKey
18
     *
19
     * @throws WrongKeyException
20
     *
21
     * @return array
22
     */
23
    public function parseNormalizedKey($normalizedKey)
24
    {
25
        $parsedKeys = explode($this->magicKeyDelimiter, $normalizedKey);
26
        $keys = $this instanceof Model ? $this->getRawKeyName() : $this->key;
27
        foreach ($keys as $index => $key) {
28
            $keys[$key] = in_array($key, $this->getBinaryColumns()) ? $this->recoverBinaryKey($key, $parsedKeys[$index]) : $parsedKeys[$index];
0 ignored issues
show
Bug introduced by
It seems like getBinaryColumns() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            $keys[$key] = in_array($key, $this->/** @scrutinizer ignore-call */ getBinaryColumns()) ? $this->recoverBinaryKey($key, $parsedKeys[$index]) : $parsedKeys[$index];
Loading history...
29
        }
30
31
        return $keys;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $keys also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type array.
Loading history...
32
    }
33
34
    /**
35
     * Get normalized key.
36
     *
37
     * @return string
38
     */
39
    private function getNormalizedKeyName()
40
    {
41
        return implode($this->magicKeyDelimiter, array_merge($this->getRawKeyName()));
0 ignored issues
show
Bug introduced by
It seems like getRawKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        return implode($this->magicKeyDelimiter, array_merge($this->/** @scrutinizer ignore-call */ getRawKeyName()));
Loading history...
42
    }
43
44
    /**
45
     * Get normalized key.
46
     *
47
     * @return string
48
     */
49
    private function getNormalizedKey()
50
    {
51
        $rawKeys = $this->getRawKey();
0 ignored issues
show
Bug introduced by
It seems like getRawKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        /** @scrutinizer ignore-call */ 
52
        $rawKeys = $this->getRawKey();
Loading history...
52
        foreach ($rawKeys as $key => $value) {
53
            if (in_array($key, $this->getBinaryColumns())) {
54
                $rawKeys[$key] = strtoupper(bin2hex($value));
55
            }
56
        }
57
58
        return implode($this->magicKeyDelimiter, $rawKeys);
59
    }
60
61
    /**
62
     * @param $key
63
     * @param $hexValue
64
     *
65
     * @throws WrongKeyException
66
     *
67
     * @return bool|string
68
     */
69
    private function recoverBinaryKey($key, $hexValue)
70
    {
71
        try {
72
            return hex2bin($hexValue);
73
        } catch (\Exception $exception) {
74
            throw new WrongKeyException("$key has invalid hex value");
75
        }
76
    }
77
}