Completed
Push — master ( 07d150...58a98c )
by Alexpts
02:24
created

ModelClosure   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.56%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 19
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 86
ccs 40
cts 41
cp 0.9756
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getToDataFn() 0 8 2
A getFillFn() 0 8 2
B createFillFn() 0 21 6
D createDataFn() 0 26 9
1
<?php
2
namespace PTS\DataTransformer;
3
4
class ModelClosure
5
{
6
    /** @var \Closure */
7
    protected $fillFn;
8
    /** @var \Closure */
9
    protected $dataFn;
10
11
    /**
12
     * @return \Closure
13
     */
14 4
    public function getToDataFn()
15
    {
16 4
        if (!$this->dataFn) {
17 4
            $this->dataFn = $this->createDataFn();
18 4
        }
19
20 4
        return $this->dataFn;
21
    }
22
23
    /**
24
     * @return \Closure
25
     */
26 3
    public function getFillFn()
27
    {
28 3
        if (!$this->fillFn) {
29 3
            $this->fillFn = $this->createFillFn();
30 3
        }
31
32 3
        return $this->fillFn;
33
    }
34
35
    /**
36
     * @return \Closure
37
     */
38 4
    protected function createDataFn() {
39
        return function (array $mapping, TypeConverter $typeConverter) {
40 2
            $props = [];
41
42 2
            foreach ($mapping as $name => $prop) {
43 2
                $getter = array_key_exists('get', $prop) ? $prop['get'] : null;
44 2
                $propVal = array_key_exists('prop', $prop) ? $prop['prop'] : null;
45
46 2
                if ($getter) {
47 2
                    $method = is_array($getter) ? $getter[0] : $getter;
48 2
                    $args = is_array($getter) ? $getter[1] : [];
49 2
                    $val = call_user_func_array([$this, $method], $args);
50 2
                } elseif ($propVal) {
51 2
                    $val = $this->{$propVal};
52 2
                } else {
53
                    throw new \InvalidArgumentException('Bad mapping config');
54
                }
55
56 2
                if ($val !== null) {
57 2
                    $props[$name] = $typeConverter->toStorage($val, $prop);
58 2
                }
59 2
            }
60
61 1
            return $props;
62 4
        };
63
    }
64
65
    /**
66
     * @return \Closure
67
     */
68
    protected function createFillFn()
69
    {
70 3
        return function(array $doc, array $mapping, TypeConverter $typeConverter) {
71 1
            foreach ($doc as $name => $val) {
72 1
                if (!array_key_exists($name, $mapping)) {
73 1
                    continue;
74
                }
75
76 1
                $prop = $mapping[$name];
77
78 1
                $modelName = array_key_exists('prop', $prop) ? $prop['prop'] : $name;
79 1
                $setter = array_key_exists('set', $prop) ? $prop['set'] : null;
80
81 1
                $val = $typeConverter->toModel($val, $prop);
82
83
                $setter
84 1
                    ? call_user_func([$this, $setter], $val)
85 1
                    : $this->{$modelName} = $val;
86 1
            }
87 3
        };
88
    }
89
}