Completed
Push — master ( d11b01...1c170b )
by Daniel
17:01 queued 13:33
created

FieldLoader::loadByTypeAndOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType;
6
7
use Psi\Component\ContentType\Storage\TypeFactory;
8
9
class FieldLoader
10
{
11
    private $fieldRegistry;
12
    private $typeFactory;
13
    private $fields = [];
14
15
    public function __construct(TypeFactory $typeFactory, FieldRegistry $fieldRegistry)
16
    {
17
        $this->fieldRegistry = $fieldRegistry;
18
        $this->typeFactory = $typeFactory;
19
    }
20
21
    public function load(string $type, array $options = []): LoadedField
22
    {
23
        $hash = md5(serialize($options)) . $type;
24
25
        if (isset($this->fields[$hash])) {
26
            return $this->fields[$hash];
27
        }
28
29
        $field = $this->fieldRegistry->get($type);
30
        $field = new LoadedField($this->typeFactory, $field, $options);
31
32
        $this->fields[$hash] = $field;
33
34
        return $field;
35
    }
36
}
37