OptionalBinaryTransformation   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 17
eloc 22
c 3
b 2
f 0
dl 0
loc 68
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hasGetMutator() 0 7 4
A setMutatedAttributeValue() 0 10 2
A mutateAttribute() 0 7 2
A shouldProcessBinaryAttribute() 0 3 2
A hasSetMutator() 0 7 3
A disableBinaryMutators() 0 3 1
A enableBinaryMutators() 0 3 1
A hexBinaryColumns() 0 3 1
A isAllowedToMutateBinaryAttributes() 0 3 1
1
<?php
2
3
namespace MaksimM\CompositePrimaryKeys\Http\Traits;
4
5
trait OptionalBinaryTransformation
6
{
7
    protected $hexBinaryColumns = false;
8
9
    private $shouldMutate = true;
10
11
    public function hexBinaryColumns()
12
    {
13
        return $this->hexBinaryColumns;
14
    }
15
16
    public function disableBinaryMutators()
17
    {
18
        $this->shouldMutate = false;
19
    }
20
21
    public function enableBinaryMutators()
22
    {
23
        $this->shouldMutate = true;
24
    }
25
26
    protected function isAllowedToMutateBinaryAttributes()
27
    {
28
        return $this->shouldMutate;
29
    }
30
31
    private function shouldProcessBinaryAttribute($key)
32
    {
33
        return $this->hexBinaryColumns() && in_array($key, $this->getBinaryColumns());
0 ignored issues
show
Bug introduced by
The method getBinaryColumns() does not exist on MaksimM\CompositePrimary...nalBinaryTransformation. Did you maybe mean hexBinaryColumns()? ( Ignorable by Annotation )

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

33
        return $this->hexBinaryColumns() && in_array($key, $this->/** @scrutinizer ignore-call */ getBinaryColumns());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
    }
35
36
    public function hasGetMutator($key)
37
    {
38
        if ($this->shouldProcessBinaryAttribute($key) && isset($this->{$key}) && $this->isAllowedToMutateBinaryAttributes()) {
39
            return true;
40
        }
41
42
        return parent::hasGetMutator($key);
43
    }
44
45
    public function mutateAttribute($key, $value)
46
    {
47
        if ($this->shouldProcessBinaryAttribute($key)) {
48
            return strtoupper(bin2hex($value));
49
        }
50
51
        return parent::mutateAttribute($key, $value);
52
    }
53
54
    public function hasSetMutator($key)
55
    {
56
        if ($this->shouldProcessBinaryAttribute($key) && $this->isAllowedToMutateBinaryAttributes()) {
57
            return true;
58
        }
59
60
        return parent::hasSetMutator($key);
61
    }
62
63
    public function setMutatedAttributeValue($key, $value)
64
    {
65
        if ($this->shouldProcessBinaryAttribute($key)) {
66
            $value = hex2bin($value);
67
            $this->attributes[$key] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
68
69
            return $value;
70
        }
71
72
        return parent::setMutatedAttributeValue($key, $value);
73
    }
74
}
75