Completed
Branch develop-3.0 (132dbd)
by Mohamed
03:28
created

ModelAbstract::getCountAttribute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 5
nc 4
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
use Tinyissue\Contracts\Model\UserInterface;
16
use Tinyissue\Repository\RepositoryUpdater;
17
18
abstract class ModelAbstract extends Model
19
{
20
    protected $updater;
21
    protected $counter;
22
    protected $fetcher;
23
24
    protected function factoryRepository($type)
25
    {
26
        $class = str_replace('Tinyissue\\Model\\', 'Tinyissue\\Repository\\', static::class) . '\\' . ucfirst($type);
27
28
        if (class_exists($class)) {
29
            return new $class($this);
30
        }
31
32
        return null;
33
    }
34
35
    public function updater(UserInterface $user = null)
36
    {
37
        if (is_null($this->updater)) {
38
            $this->updater = $this->factoryRepository('updater');
39
        }
40
41
        if ($this->updater instanceof RepositoryUpdater) {
42
            $this->updater->setUser($user);
43
        }
44
45
        return $this->updater;
46
    }
47
48
    public function counter()
49
    {
50
        if (is_null($this->counter)) {
51
            $this->counter = $this->factoryRepository('counter');
52
        }
53
54
        return $this->counter;
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
     * Returns the aggregate value of a field.
104
     *
105
     * @param string $field
106
     *
107
     * @return int
108
     */
109
    protected function getCountAttribute($field)
110
    {
111
        // if relation is not loaded already, let's do it first
112
        if (!array_key_exists($field, $this->relations)) {
113
            $this->load($field);
114
        }
115
116
        $related = $this->getRelation($field);
117
118
        // then return the count directly
119
        return ($related) ? (int) $related->aggregate : 0;
120
    }
121
}
122