Completed
Push — master ( f77784...60840d )
by Mahmoud
03:05
created

Repository::model()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
c 1
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace App\Port\Repository\Abstracts;
4
5
use Prettus\Repository\Contracts\CacheableInterface as PrettusCacheableInterface;
6
use Prettus\Repository\Criteria\RequestCriteria as PrettusRequestCriteria;
7
use Prettus\Repository\Eloquent\BaseRepository as PrettusRepository;
8
use Prettus\Repository\Traits\CacheableRepository as PrettusCacheableRepository;
9
10
/**
11
 * Class Repository.
12
 *
13
 * @author  Mahmoud Zalt <[email protected]>
14
 */
15
abstract class Repository extends PrettusRepository implements PrettusCacheableInterface
16
{
17
18
    use PrettusCacheableRepository;
19
20
    /**
21
     * Boot up the repository, pushing criteria.
22
     */
23
    public function boot()
24
    {
25
        $this->pushCriteria(app(PrettusRequestCriteria::class));
26
    }
27
28
    /**
29
     * This function relies on the convention.
30
     * Conventions:
31
     *    - Repository name should be same like it's model name (model: Foo -> repository: FooRepository).
32
     *    - If the container contains Models with names different than the container name, the repository class must
33
     *          set `$container='ContainerName'` property for this function to work properly
34
     * Specify Model class name.
35
     *
36
     * @return string
37
     */
38
    public function model()
39
    {
40
        // 1_ get the full namespace of the child class who's extending this class.
41
        // 2_ remove the namespace and keep the class name
42
        // 3_ remove the word Repository from the class name
43
        // 4_ check if the container name is set on the repository to indicate that the
44
        //    model has different name than the container holding it
45
        // 5_ build the namespace of the Model based on the conventions
46
47
        $fullName = get_called_class();
48
        $className = substr($fullName, strrpos($fullName, '\\') + 1);
49
        $classOnly = str_replace('Repository', '', $className);
50
        $container = isset($this->container) ? $this->container : $classOnly;
51
        $modelNamespace = 'App\Containers\\' . $container . '\\Models\\' . $classOnly;
52
53
        return $modelNamespace;
54
    }
55
}
56