Repository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 10
Bugs 3 Features 0
Metric Value
c 10
b 3
f 0
dl 0
loc 128
rs 10
wmc 13
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A firstMatching() 0 4 1
A allMatching() 0 4 1
A paginate() 0 4 1
A __construct() 0 10 4
A all() 0 4 1
A find() 0 4 1
A delete() 0 4 1
A store() 0 4 1
A __call() 0 8 2
1
<?php
2
3
namespace Analogue\ORM;
4
5
use Analogue\ORM\Exceptions\MappingException;
6
use Exception;
7
use InvalidArgumentException;
8
use Analogue\ORM\System\Mapper;
9
use Analogue\ORM\System\Manager;
10
11
/**
12
 * Class Repository
13
 *
14
 * @mixin Mapper
15
 */
16
class Repository
17
{
18
    /**
19
     * The mapper object for the corresponding entity
20
     *
21
     * @var \Analogue\ORM\System\Mapper
22
     */
23
    protected $mapper;
24
25
    /**
26
     * To build a repository, either provide :
27
     *
28
     * - Mappable object's class name as a string
29
     * - Mappable object instance
30
     * - Instance of mapper
31
     *
32
     * @param  Mapper         $mapper
33
     * @param  EntityMap|null $entityMap (optional)
34
     * @throws \InvalidArgumentException
35
     * @throws MappingException
36
     */
37
    public function __construct($mapper, EntityMap $entityMap = null)
38
    {
39
        if ($mapper instanceof Mappable || is_string($mapper)) {
40
            $this->mapper = Manager::getMapper($mapper, $entityMap);
41
        } elseif ($mapper instanceof Mapper) {
42
            $this->mapper = $mapper;
43
        } else {
44
            new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.');
45
        }
46
    }
47
48
    /**
49
     * Return all Entities from database
50
     *
51
     * @return \Analogue\ORM\EntityCollection
52
     */
53
    public function all()
54
    {
55
        return $this->mapper->get();
0 ignored issues
show
Documentation Bug introduced by
The method get does not exist on object<Analogue\ORM\System\Mapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
    }
57
    
58
    /**
59
     * Fetch a record from the database
60
     * @param  integer $id
61
     * @return \Analogue\ORM\Mappable
62
     */
63
    public function find($id)
64
    {
65
        return $this->mapper->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Analogue\ORM\System\Mapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
    }
67
68
    /**
69
     * Get the first entity matching the given attributes.
70
     *
71
     * @param  array  $attributes
72
     * @return \Analogue\ORM\Mappable|null
73
     */
74
    public function firstMatching(array $attributes)
75
    {
76
        return $this->mapper->where($attributes)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Analogue\ORM\System\Mapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
    }
78
79
    /**
80
     * Return all the entities matching the given attributes
81
     *
82
     * @param array $attributes
83
     * @return \Analogue\ORM\EntityCollection
84
     */
85
    public function allMatching(array $attributes)
86
    {
87
        return $this->mapper->where($attributes)->get();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Analogue\ORM\System\Mapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
88
    }
89
90
    /**
91
     * Return a paginator instance on the EntityCollection
92
     *
93
     * @param int|null $perPage number of item per page (fallback on default setup in entity map)
94
     * @return \Illuminate\Pagination\LengthAwarePaginator
95
     */
96
    public function paginate($perPage = null)
97
    {
98
        return $this->mapper->paginate($perPage);
0 ignored issues
show
Documentation Bug introduced by
The method paginate does not exist on object<Analogue\ORM\System\Mapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
    }
100
101
    /**
102
     * Delete an entity or an entity collection from the database
103
     *
104
     * @param  Mappable|EntityCollection $entity
105
     * @throws MappingException
106
     * @throws \InvalidArgumentException
107
     * @return \Illuminate\Support\Collection|null
108
     */
109
    public function delete($entity)
110
    {
111
        return $this->mapper->delete($entity);
112
    }
113
114
    /**
115
     * Persist an entity or an entity collection in the database.
116
     *
117
     * @param  Mappable|EntityCollection|array $entity
118
     * @throws MappingException
119
     * @throws \InvalidArgumentException
120
     * @return Mappable|EntityCollection|array
121
     */
122
    public function store($entity)
123
    {
124
        return $this->mapper->store($entity);
125
    }
126
127
    /**
128
     * Make custom mapper custom commands available in repository
129
     *
130
     * @param  string $method
131
     * @param  array  $parameters
132
     * @throws Exception
133
     * @return mixed
134
     */
135
    public function __call($method, $parameters)
136
    {
137
        if ($this->mapper->hasCustomCommand($method)) {
138
            call_user_func_array([$this->mapper, $method], $parameters);
139
        } else {
140
            throw new Exception("No method $method on " . get_class($this));
141
        }
142
    }
143
}
144