Completed
Push — develop ( b974e0...970378 )
by
unknown
05:31
created

ModelResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setModel() 0 4 1
A setTable() 0 4 1
A getModel() 0 7 2
A getTable() 0 7 2
A make() 0 8 2
1
<?php
2
namespace Fenos\Notifynder\Resolvers;
3
4
use Illuminate\Support\Str;
5
6
class ModelResolver
7
{
8
    protected $models = [];
9
    protected $tables = [];
10
11
    public function setModel($class, $model)
12
    {
13
        $this->models[$class] = $model;
14
    }
15
16
    public function setTable($class, $table)
17
    {
18
        $this->tables[$class] = $table;
19
    }
20
21
    public function getModel($class)
22
    {
23
        if (isset($this->models[$class])) {
24
            return $this->models[$class];
25
        }
26
        return $class;
27
    }
28
29
    public function getTable($class)
30
    {
31
        if (isset($this->tables[$class])) {
32
            return $this->tables[$class];
33
        }
34
        return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getModel($class)))));
35
    }
36
37
    public function make($class, array $attributes = [])
38
    {
39
        $model = $this->getModel($class);
40
        if(!class_exists($model)) {
41
            throw new \ReflectionException("Class {$model} does not exist");
42
        }
43
        return new $model($attributes);
44
    }
45
}