Passed
Push — main ( 383cbf...40aa9a )
by Dylan
02:01
created

ObjectFactory::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Lifeboat\Factory;
4
5
use Lifeboat\Connector;
6
use Lifeboat\Models\Model;
7
use Lifeboat\Models\Order;
8
use Lifeboat\Resource\ObjectResource;
9
10
/**
11
 * Class ObjectFactory
12
 * @package Lifeboat\Services
13
 */
14
class ObjectFactory {
15
16
    const CLASS_MAP = [
17
        'order'     => Order::class
18
    ];
19
20
    /**
21
     * @param Connector $connector
22
     * @param string $model
23
     * @param array $data
24
     * @return Model|null
25
     */
26
    public static function create(Connector $connector, string $model, array $data = []): ?Model
27
    {
28
        $model = strtolower($model);
29
        if (!array_key_exists($model, self::CLASS_MAP)) return null;
30
31
        $cls = self::CLASS_MAP[$model];
32
        return new $cls($connector, $data);
33
    }
34
35
    /**
36
     * @param Connector $connector
37
     * @param array $data
38
     * @return ObjectResource
39
     */
40
    public static function make(Connector $connector, array $data): ?ObjectResource
41
    {
42
        $model = $data['model'] ?? '';
43
        if (!$model) return new ObjectResource($connector, $data);
44
        return self::create($connector, $model, $data);
45
    }
46
}
47