Completed
Push — master ( ca910a...8ffcad )
by Evan
02:52
created

Meta   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 120
rs 10
wmc 10
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get() 0 4 1
A all() 0 4 1
A set() 0 4 1
A add() 0 4 1
A delete() 0 4 1
A exists() 0 4 1
A getObjectId() 0 4 1
A __get() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Silk\Meta;
4
5
use Illuminate\Support\Collection;
6
7
class Meta
8
{
9
    /**
10
     * Meta type
11
     * @var string
12
     */
13
    protected $type;
14
15
    protected $object_id;
16
17
    protected $key;
18
19
    /**
20
     * @param string     $type      Meta type
21
     * @param int|string $object_id ID of the object metadata is for
22
     * @param string     $key       Meta key
23
     */
24
    public function __construct($type, $object_id, $key)
25
    {
26
        $this->type      = $type;
27
        $this->object_id = $object_id;
28
        $this->key       = $key;
29
    }
30
31
    /**
32
     * Get the single meta data
33
     *
34
     * @return mixed
35
     */
36
    public function get()
37
    {
38
        return get_metadata($this->type, $this->object_id, $this->key, true);
39
    }
40
41
    /**
42
     * Get all meta data
43
     *
44
     * @return Collection
45
     */
46
    public function all()
47
    {
48
        return Collection::make(get_metadata($this->type, $this->object_id, $this->key, false));
49
    }
50
51
    /**
52
     * Set the meta value
53
     *
54
     * @param mixed  $value
55
     * @param string $prev_value [description]
56
     * @return bool              True on success, false on failure
57
     */
58
    public function set($value, $prev_value = '')
59
    {
60
        return update_metadata($this->type, $this->object_id, $this->key, $value, $prev_value);
61
    }
62
63
    /**
64
     * Add metadata for the specified object
65
     *
66
     * @param mixed  $value  The value to add
67
     * @param [type] $unique [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
68
     *
69
     * @return int|false The meta ID on success, false on failure.
70
     */
71
    public function add($value, $unique = false)
72
    {
73
        return add_metadata($this->type, $this->object_id, $this->key, $value, $unique);
74
    }
75
76
    /**
77
     * Delete the meta data
78
     *
79
     * Deletes all meta data for the key, if provided, optionally filtered by
80
     * a previous value.
81
     * If no key was provided, all meta data for the object is deleted.
82
     *
83
     * @param  string $value The old value to delete.
84
     *                       This is only necessary when deleting a specific value
85
     *                       from an object which has multiple values for the key.
86
     *
87
     * @return bool              True on success, false on failure
88
     */
89
    public function delete($value = '')
90
    {
91
        return delete_metadata($this->type, $this->object_id, $this->key, $value);
92
    }
93
94
    /**
95
     * Determine if a meta key is set for a given object
96
     *
97
     * @return bool     True of the key is set, false if not.
98
     */
99
    public function exists()
100
    {
101
        return metadata_exists($this->type, $this->object_id, $this->key);
102
    }
103
104
    /**
105
     * Get the object_id
106
     * 
107
     * @return int|string
108
     */
109
    public function getObjectId()
110
    {
111
        return $this->object_id;
112
    }
113
114
    /**
115
     *
116
     */
117
    public function __get($property)
118
    {
119
        return $this->get($property);
0 ignored issues
show
Unused Code introduced by
The call to Meta::get() has too many arguments starting with $property.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
    }
121
122
    public function __toString()
123
    {
124
        return $this->get();
125
    }
126
}
127