UserRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 106
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A modelClassName() 0 4 1
A find() 0 7 1
A findBy() 0 6 1
A findAll() 0 14 1
A add() 0 7 1
A addAll() 0 10 2
A fromEloquentArray() 0 13 4
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/02/16
6
 * Time: 17:59.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Example\Persistence\Eloquent;
12
13
use Illuminate\Database\Eloquent\Model;
14
use NilPortugues\Assert\Assert;
15
use NilPortugues\Example\Service\UserAdapter;
16
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;
17
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter;
18
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity;
19
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable;
20
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort;
21
use NilPortugues\Foundation\Domain\Model\Repository\Page;
22
use NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB\EloquentRepository;
23
24
/**
25
 * Class UserRepository.
26
 */
27
class UserRepository extends EloquentRepository
28
{
29
    /**
30
     * @var UserAdapter
31
     */
32
    protected $userAdapter;
33
34
    /**
35
     * @param $userAdapter
36
     */
37
    public function __construct($userAdapter)
38
    {
39
        $this->userAdapter = $userAdapter;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function modelClassName()
46
    {
47
        return User::class;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function find(Identity $id, Fields $fields = null)
54
    {
55
        /** @var Model $eloquentModel */
56
        $eloquentModel = parent::find($id, $fields);
57
58
        return $this->userAdapter->fromEloquent($eloquentModel->toArray());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->userAdapte...quentModel->toArray()); (NilPortugues\Example\Domain\User) is incompatible with the return type declared by the interface NilPortugues\Foundation\...ts\ReadRepository::find of type array.

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...
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
65
    {
66
        $eloquentModelArray = parent::findBy($filter, $sort, $fields);
67
68
        return $this->fromEloquentArray($eloquentModelArray);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function findAll(Pageable $pageable = null)
75
    {
76
        $page = parent::findAll($pageable);
77
78
        return new Page(
79
            $this->fromEloquentArray($page->content()),
80
            $page->totalElements(),
81
            $page->pageNumber(),
82
            $page->totalPages(),
83
            $page->sortings(),
84
            $page->filters(),
85
            $page->fields()
86
        );
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function add(Identity $value)
93
    {
94
        Assert::isInstanceOf($value, User::class);
95
        $value = $this->userAdapter->toEloquent($value);
0 ignored issues
show
Compatibility introduced by
$value of type object<NilPortugues\Foun...ory\Contracts\Identity> is not a sub-type of object<NilPortugues\Example\Domain\User>. It seems like you assume a concrete implementation of the interface NilPortugues\Foundation\...tory\Contracts\Identity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
96
97
        return parent::add($value);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function addAll(array $values)
104
    {
105
        $eloquent = [];
106
        foreach ($values as $value) {
107
            Assert::isInstanceOf($value, User::class);
108
            $eloquent[] = $this->userAdapter->toEloquent($value);
109
        }
110
111
        parent::addAll($eloquent);
112
    }
113
114
    /**
115
     * @param array $eloquentModelArray
116
     *
117
     * @return array
118
     */
119
    protected function fromEloquentArray(array $eloquentModelArray)
120
    {
121
        $results = [];
122
        foreach ($eloquentModelArray as $eloquentModel) {
123
            if (is_object($eloquentModel) && method_exists($eloquentModel, 'toArray')) {
124
                $eloquentModel = $eloquentModel->toArray();
125
            }
126
127
            $results[] = $this->userAdapter->fromEloquent($eloquentModel);
128
        }
129
130
        return $results;
131
    }
132
}
133