Passed
Push — main ( dd7998...bae415 )
by Dylan
03:01
created

ObjectFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 1
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 2
A make() 0 6 1
1
<?php
2
3
namespace Lifeboat\Services;
4
5
use Lifeboat\Connector;
6
use Lifeboat\Models\Address;
7
use Lifeboat\Models\Model;
8
9
/**
10
 * Class ObjectFactory
11
 * @package Lifeboat\Services
12
 */
13
class ObjectFactory {
14
15
    const CLASS_MAP = [
16
        'address'   => Address::class
17
    ];
18
19
    /**
20
     * @param Connector $connector
21
     * @param string $model
22
     * @param array $data
23
     * @return Model|null
24
     */
25
    public static function create(Connector $connector, string $model, array $data = []): ?Model
26
    {
27
        $model = strtolower($model);
28
        if (!array_key_exists($model, self::CLASS_MAP)) return null;
29
30
        $cls = self::CLASS_MAP[$model];
31
        return new $cls($connector, $data);
32
    }
33
34
    /**
35
     * @param Connector $connector
36
     * @param array $data
37
     * @return Model|null
38
     */
39
    public static function make(Connector $connector, array $data): ?Model
40
    {
41
        $model = $data['model'] ?? '';
42
        unset($data['model']);
43
44
        return self::create($connector, $model, $data);
45
    }
46
}
47