Completed
Push — master ( 685a37...c037a4 )
by Nil
04:54
created

UserRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Example\Service\UserAdapter;
15
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;
16
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter;
17
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity;
18
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable;
19
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort;
20
use NilPortugues\Foundation\Domain\Model\Repository\Page;
21
use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository;
22
23
/**
24
 * Class UserRepository.
25
 */
26
class UserRepository extends EloquentRepository
27
{
28
    /**
29
     * @var UserAdapter
30
     */
31
    protected $userAdapter;
32
33
    /**
34
     * @param $userAdapter
35
     */
36
    public function __construct($userAdapter)
37
    {
38
        $this->userAdapter = $userAdapter;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function modelClassName()
45
    {
46
        return User::class;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function find(Identity $id, Fields $fields = null)
53
    {
54
        /** @var Model $eloquentModel */
55
        $eloquentModel = parent::find($id, $fields);
56
57
        return $this->userAdapter->fromEloquent($eloquentModel);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->userAdapte...oquent($eloquentModel); (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...
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
64
    {
65
        $eloquentModelArray = parent::findBy($filter, $sort, $fields);
66
67
        return $this->fromEloquentArray($eloquentModelArray);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function findAll(Pageable $pageable = null)
74
    {
75
        $page = parent::findAll($pageable);
76
77
        return new Page(
78
            $this->fromEloquentArray($page->content()),
79
            $page->totalElements(),
80
            $page->pageNumber(),
81
            $page->totalPages(),
82
            $page->sortings(),
83
            $page->filters(),
84
            $page->fields()
85
        );
86
    }
87
88
    /**
89
     * @param array $eloquentModelArray
90
     *
91
     * @return array
92
     */
93
    protected function fromEloquentArray(array $eloquentModelArray)
94
    {
95
        $results = [];
96
        foreach ($eloquentModelArray as $eloquentModel) {
97
            //This is required to handle findAll returning array, not objects.
98
            $eloquentModel = (object) $eloquentModel;
99
100
            $results[] = $this->userAdapter->fromEloquent($eloquentModel);
101
        }
102
103
        return $results;
104
    }
105
}
106