ModelManager::getRepository()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 8.5906
cc 5
eloc 13
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Ablunier\Laravel\Database\Manager\Eloquent;
4
5
use Ablunier\Laravel\Database\Contracts\Manager\ModelManager as ModelManagerContract;
6
use Ablunier\Laravel\Database\Contracts\Repository\HasCache;
7
use Ablunier\Laravel\Database\Contracts\Repository\HasCustomRepository;
8
use Ablunier\Laravel\Database\Dbal\Eloquent\AbstractionLayer;
9
use Ablunier\Laravel\Database\Repository\Eloquent\Cache;
10
use Ablunier\Laravel\Database\Repository\Eloquent\Repository;
11
use Illuminate\Contracts\Foundation\Application;
12
use Illuminate\Database\Eloquent\Model as EloquentModel;
13
use Manager\Exceptions\InvalidModelException;
14
use Manager\Exceptions\InvalidRepositoryException;
15
16
class ModelManager implements ModelManagerContract
17
{
18
    /**
19
     * @var Application
20
     */
21 9
    private $app;
22
23 9
    /**
24 9
     * @param Application $app
25
     */
26
    public function __construct(Application $app)
27
    {
28
        $this->app = $app;
29
    }
30
31
    /**
32
     * Get Eloquent Model instance.
33 7
     *
34
     * @param string $modelName
35 7
     *
36
     * @throws \Exception
37 7
     *
38 1
     * @return \Illuminate\Database\Eloquent\Model
39
     */
40 1
    public function getModelInstance($modelName)
41
    {
42
        $modelInstance = new $modelName();
43 6
44
        if (!$modelInstance instanceof EloquentModel) {
45
            $message = "Target [$modelName] is not an Illuminate\Database\Eloquent\Model instance.";
46
47
            throw new InvalidModelException($message);
48
        }
49 4
50
        return $modelInstance;
51 4
    }
52
53 4
    /**
54
     * {@inheritdoc}
55 4
     */
56 2
    public function getRepository($modelName)
57
    {
58 2
        $modelInstance = $this->getModelInstance($modelName);
59 1
60
        if ($modelInstance instanceof HasCustomRepository) {
61 1
            $repositoryClass = $modelInstance->repository();
62
            $repository = new $repositoryClass($modelInstance);
63 1
64 2
            if (!$repository instanceof Repository) {
65
                $message = "The [$modelName] custom repository must extend Ablunier\Laravel\Database\Repository\Eloquent\Repository.";
66
67 3
                throw new InvalidRepositoryException($message);
68 1
            }
69 1
        } else {
70 1
            $repository = new Repository($modelInstance);
71 1
        }
72
73
        if ($modelInstance instanceof HasCache && $modelInstance->cache() === true) {
74 2
            return new Cache($repository, $this->app['cache.store']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Ablunier\Lar...s->app['cache.store']); (Ablunier\Laravel\Databas...pository\Eloquent\Cache) is incompatible with the return type declared by the interface Ablunier\Laravel\Databas...lManager::getRepository of type Ablunier\Laravel\Databas...s\Repository\Repository.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
75
        }
76
77
        return $repository;
78
    }
79
80 1
    /**
81
     * {@inheritdoc}
82 1
     */
83
    public function getAbstractionLayer($modelName)
84 1
    {
85
        $modelInstance = $this->getModelInstance($modelName);
86 1
87
        $dbal = new AbstractionLayer($modelInstance);
88 1
89
        return $dbal;
90
    }
91
}
92