Completed
Push — develop-3.0 ( 4fe777...24fc5d )
by Mohamed
09:15
created

ModelAbstract::instance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model;
13
14
use Illuminate\Database\Eloquent\Model;
15
16
abstract class ModelAbstract extends Model
17
{
18
    protected $updater;
19
    protected $counter;
20
    protected $fetcher;
21
22
    protected function factoryRepository($type)
23
    {
24
        // new Tinyissue\Model\Repository\User\Counter();
25
        // new Tinyissue\Model\Repository\User\Updater();
26
        // new Tinyissue\Model\Repository\User\Fetcher();
27
28
        $class = str_replace('Tinyissue\\Model\\', 'Tinyissue\\Repository\\', static::class) . '\\' . ucfirst($type);
29
30
        if (class_exists($class)) {
31
            return new $class($this);
32
        }
33
34
        return null;
35
    }
36
37
    public function updater()
38
    {
39
        if (is_null($this->updater)) {
40
            $this->updater = $this->factoryRepository('updater');
41
        }
42
43
        return $this->updater;
44
        // return class that manages updates
45
    }
46
47
    public function counter()
48
    {
49
        if (is_null($this->counter)) {
50
            $this->counter = $this->factoryRepository('counter');
51
        }
52
53
        return $this->counter;
54
//        return class that cound
55
    }
56
57
    public function fetcher()
58
    {
59
        if (is_null($this->fetcher)) {
60
            $this->fetcher = $this->factoryRepository('fetcher');
61
        }
62
63
        return $this->fetcher;
64
    }
65
66
    public function __call($name, $arguments)
67
    {
68
        $caller = false;
69
70
        if (strpos($name, 'count') === 0) {
71
            $caller = $this->counter();
72
        }
73
74
        if (strpos($name, 'get') === 0) {
75
            $caller = $this->fetcher();
76
        }
77
78
        if (!is_null($this->updater()) && method_exists($this->updater, $name)) {
79
            $caller = $this->updater();
80
        }
81
82
        if (false != $caller && method_exists($caller, $name)) {
83
            return $caller->{$name}(...$arguments);
84
        }
85
86
        return parent::__call($name, $arguments);
87
    }
88
89
    /**
90
     * @param array $attributes
91
     *
92
     * @return static
93
     */
94
    public static function instance(array $attributes = [])
95
    {
96
        $model = new static($attributes);
97
        $model->exists = false;
98
99
        return $model;
100
    }
101
}
102
103
//
104
105
/*
106
 * Model/User
107
 * Model/User/UserRelations
108
 * Model/User/UserScopes
109
 * Model/User/Updater
110
 * Model/User/Counter
111
 * Model/User/Fetcher
112
 *
113
 * $some = new Tiny\Model\User();
114
 * $some = new Tiny\Model\User\Helpers\Counter();
115
 * $some = new Tiny\Model\User\Helpers\Updater();
116
 * $some = new Tiny\Model\User\Helpers\Fetcher();
117
 *
118
 * $some = new Tiny\Model\Helpers\User\Counter();
119
 * $some = new Tiny\Model\Helpers\User\Updater();
120
 * $some = new Tiny\Model\Helpers\User\Fetcher();
121
 *
122
 *
123
 * $some = new Tiny\Repository\User\UserCounter();
124
 * $some = new Tiny\Repository\User\UserUpdater();
125
 * $some = new Tiny\Repository\User\UserFetcher();
126
 */
127