Completed
Branch master (fe6485)
by Nil
02:01
created

EloquentReadRepository::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB;
4
5
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;
6
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter;
7
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity;
8
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository;
9
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort;
10
11
class EloquentReadRepository extends BaseEloquentRepository implements ReadRepository
12
{
13
    /**
14
     * Retrieves an entity by its id.
15
     *
16
     * @param Identity    $id
17
     * @param Fields|null $fields
18
     *
19
     * @return array
20
     */
21
    public function find(Identity $id, Fields $fields = null)
22
    {
23
        $model = self::$instance;
24
        $columns = ($fields) ? $fields->get() : ['*'];
25
26
        return $model->query()->where($model->getKeyName(), '=', $id->id())->get($columns)->first();
27
    }
28
29
    /**
30
     * Returns all instances of the type.
31
     *
32
     * @param Filter|null $filter
33
     * @param Sort|null   $sort
34
     * @param Fields|null $fields
35
     *
36
     * @return array
37
     */
38 View Code Duplication
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $model = self::$instance;
41
        $query = $model->query();
42
        $columns = ($fields) ? $fields->get() : ['*'];
43
44
        if ($filter) {
45
            EloquentFilter::filter($query, $filter);
0 ignored issues
show
Compatibility introduced by
$query of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<Jenssegers\Mongodb\Eloquent\Builder>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder 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...
46
        }
47
48
        if ($sort) {
49
            EloquentSorter::sort($query, $sort);
0 ignored issues
show
Compatibility introduced by
$query of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<Jenssegers\Mongodb\Eloquent\Builder>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder 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...
50
        }
51
52
        return $query->get($columns)->toArray();
53
    }
54
55
    /**
56
     * Returns all instances of the type meeting $distinctFields values.
57
     *
58
     * @param Fields      $distinctFields
59
     * @param Filter|null $filter
60
     * @param Sort|null   $sort
61
     *
62
     * @return array
63
     */
64 View Code Duplication
    public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $model = self::$instance;
67
        $query = $model->query();
68
69
        $columns = (count($fields = $distinctFields->get()) > 0) ? $fields : ['*'];
70
71
        if ($filter) {
72
            EloquentFilter::filter($query, $filter);
0 ignored issues
show
Compatibility introduced by
$query of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<Jenssegers\Mongodb\Eloquent\Builder>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder 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...
73
        }
74
75
        if ($sort) {
76
            EloquentSorter::sort($query, $sort);
0 ignored issues
show
Compatibility introduced by
$query of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<Jenssegers\Mongodb\Eloquent\Builder>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder 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...
77
        }
78
79
        return $query->getQuery()->distinct()->get($columns);
80
    }
81
82
    /**
83
     * Returns the total amount of elements in the repository given the restrictions provided by the Filter object.
84
     *
85
     * @param Filter|null $filter
86
     *
87
     * @return int
88
     */
89
    public function count(Filter $filter = null)
90
    {
91
        // TODO: Implement count() method.
92
    }
93
94
    /**
95
     * Returns whether an entity with the given id exists.
96
     *
97
     * @param $id
98
     *
99
     * @return bool
100
     */
101
    public function exists(Identity $id)
102
    {
103
        // TODO: Implement exists() method.
104
    }
105
}
106