Hooks::queryHook()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
cc 5
nc 1
nop 0
1
<?php
2
3
namespace Sofa\Eloquence\Metable;
4
5
use BadMethodCallException;
6
7
/**
8
 * This class provides instance scope for the closures
9
 * so they can be rebound later onto the actual model.
10
 */
11
class Hooks
12
{
13
    /**
14
     * Register hook on getAttribute method.
15
     *
16
     * @return \Closure
17
     */
18
    public function getAttribute()
19
    {
20
        return function ($next, $value, $args) {
21
            $key = $args->get('key');
22
23
            if (is_null($value)) {
24
                $value = $this->getMeta($key);
0 ignored issues
show
Documentation Bug introduced by
The method getMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
25
            }
26
27
            return $next($value, $args);
28
        };
29
    }
30
31
    /**
32
     * Register hook on setAttribute method.
33
     *
34
     * @return \Closure
35
     */
36
    public function setAttribute()
37
    {
38
        return function ($next, $value, $args) {
39
            $key = $args->get('key');
40
41
            if (!$this->hasColumn($key) && $this->allowsMeta($key) && !$this->hasSetMutator($key)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasColumn does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
Documentation Bug introduced by
The method allowsMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
Documentation Bug introduced by
The method hasSetMutator does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
42
                return $this->setMeta($key, $value);
0 ignored issues
show
Documentation Bug introduced by
The method setMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
43
            }
44
45
            return $next($value, $args);
46
        };
47
    }
48
49
    /**
50
     * Register hook on toArray method.
51
     *
52
     * @return \Closure
53
     */
54
    public function toArray()
55
    {
56
        return function ($next, $attributes) {
57
            unset($attributes['meta_attributes'], $attributes['metaAttributes']);
58
59
            $attributes = array_merge($attributes, $this->getMetaAttributesArray());
0 ignored issues
show
Documentation Bug introduced by
The method getMetaAttributesArray does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
60
61
            return $next($attributes);
62
        };
63
    }
64
65
    /**
66
     * Register hook on replicate method.
67
     *
68
     * @return \Closure
69
     */
70
    public function replicate()
71
    {
72
        return function ($next, $copy, $args) {
73
            $metaAttributes = $args->get('original')
74
                                    ->getMetaAttributes()
75
                                    ->replicate($args->get('except'));
76
77
            $copy->setRelation('metaAttributes', $metaAttributes);
78
79
            return $next($copy, $args);
80
        };
81
    }
82
83
    /**
84
     * Register hook on save method.
85
     *
86
     * @return \Closure
87
     */
88
    public function save()
89
    {
90
        return function ($next, $value, $args) {
91
            $this->saveMeta();
0 ignored issues
show
Documentation Bug introduced by
The method saveMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
92
93
            return $next($value, $args);
94
        };
95
    }
96
97
    /**
98
     * Register hook on isset call.
99
     *
100
     * @return \Closure
101
     */
102
    public function __issetHook()
103
    {
104
        return function ($next, $isset, $args) {
105
            $key = $args->get('key');
106
107
            if (!$isset) {
108
                $isset = (bool) $this->hasMeta($key);
0 ignored issues
show
Documentation Bug introduced by
The method hasMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
109
            }
110
111
            return $next($isset, $args);
112
        };
113
    }
114
115
    /**
116
     * Register hook on unset call.
117
     *
118
     * @return \Closure
119
     */
120
    public function __unsetHook()
121
    {
122
        return function ($next, $value, $args) {
123
            $key = $args->get('key');
124
125
            if ($this->hasMeta($key)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
126
                return $this->setMeta($key, null);
0 ignored issues
show
Documentation Bug introduced by
The method setMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
127
            }
128
129
            return $next($value, $args);
130
        };
131
    }
132
133
    /**
134
     * Register hook on queryHook method.
135
     *
136
     * @return \Closure
137
     */
138
    public function queryHook()
139
    {
140
        return function ($next, $query, $bag) {
141
            $method = $bag->get('method');
142
            $args = $bag->get('args');
143
            $column = $args->get('column');
144
145
            if (!$this->hasColumn($column) && $this->allowsMeta($column) && $this->isMetaQueryable($method)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasColumn does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
Documentation Bug introduced by
The method allowsMeta does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
Documentation Bug introduced by
The method isMetaQueryable does not exist on object<Sofa\Eloquence\Metable\Hooks>? 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...
146
                return call_user_func_array([$this, 'metaQuery'], [$query, $method, $args]);
147
            }
148
149
            if (in_array($method, ['select', 'addSelect'])) {
150
                call_user_func_array([$this, 'metaSelect'], [$query, $args]);
151
            }
152
153
            return $next($query, $bag);
154
        };
155
    }
156
157
    public function __call($method, $params)
158
    {
159
        if (strpos($method, '__') === 0 && method_exists($this, $method . 'Hook')) {
160
            return call_user_func_array([$this, $method . 'Hook'], $params);
161
        }
162
163
        throw new BadMethodCallException("Method [{$method}] doesn't exist on this object.");
164
    }
165
}
166