Collection::support()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Record;
4
5
use Knp\FriendlyContexts\Reflection\ObjectReflector;
6
use Symfony\Component\PropertyAccess\PropertyAccess;
7
8
class Collection
9
{
10
11
    protected $reflector;
12
    protected $referencial;
13
    protected $headers;
14
    protected $records;
15
16
    public function __construct(ObjectReflector $reflector)
17
    {
18
        $this->reflector = $reflector;
19
        $this->headers   = [];
20
        $this->records   = [];
21
    }
22
23
    public function support($entity)
24
    {
25
        if (null !== $this->referencial) {
26
            $name = $this->referencial;
27
28
            return $this->reflector->isInstanceOf($entity, $name);
29
        }
30
        $this->setReferencial($entity);
31
32
        return true;
33
    }
34
35
    public function getReferencial()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
36
    {
37
        return $this->referencial;
38
    }
39
40
    public function setReferencial($entity)
41
    {
42
        $this->referencial = is_object($entity) ? $this->reflector->getClassLongName($entity) : $entity;
43
44
        return $this;
45
    }
46
47
    public function attach($entity, array $values = null)
48
    {
49
        $values = $values ?: $this->buildValues($entity);
50
        $this->mergeHeaders(array_keys($values));
51
52
        $record = new Record($this->reflector, $this);
53
        $record->attach($entity, $values);
54
55
        $this->records[] = $record;
56
57
        return $record;
58
    }
59
60
    public function search($value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
61
    {
62
        foreach ($this->records as $record) {
63
            $entity = $record->getEntity();
64
            if (method_exists($entity, '__toString') && (string) $entity === $value) {
65
                return $record;
66
            }
67
        }
68
69
        foreach ($this->headers as $header) {
70
            foreach ($this->records as $record) {
71
                if (null !== $record->get($header) && $value === $record->get($header)) {
72
                    return $record;
73
                }
74
            }
75
        }
76
    }
77
78
    public function all()
79
    {
80
        return array_values($this->records);
81
    }
82
83
    public function count()
84
    {
85
        return count($this->records);
86
    }
87
88
    protected function mergeHeaders($headers)
89
    {
90
        foreach ($headers as $header) {
91
            if (!in_array($header, $this->headers)) {
92
                $this->headers[] = $header;
93
            }
94
        }
95
    }
96
97
    protected function buildValues($entity)
98
    {
99
        $result = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
100
        $accessor = PropertyAccess::createPropertyAccessor();
101
102
        foreach ($this->headers as $header) {
103
            try {
104
                $value = $accessor->getValue($entity, $header);
105
                if (is_scalar($value)) {
106
                    $result[$header] = $value;
107
                }
108
            } catch (\Exception $ex) {
109
                unset($ex);
110
            }
111
        }
112
113
        return $result;
114
    }
115
}
116