Completed
Push — master ( 409ce2...ca0be7 )
by Evan
07:40
created

PostType   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 160
rs 10
wmc 20
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 8 2
A load() 0 8 2
A exists() 0 4 1
A object() 0 4 1
A supports() 0 11 2
A addSupportFor() 0 6 2
A removeSupportFor() 0 9 2
A unregister() 0 14 3
A __get() 0 10 4
1
<?php
2
3
namespace Silk\Post;
4
5
use stdClass;
6
use InvalidArgumentException;
7
use Illuminate\Support\Collection;
8
use Silk\Exception\WP_ErrorException;
9
use Silk\Post\Exception\InvalidPostTypeNameException;
10
use Silk\Post\Exception\NonExistentPostTypeException;
11
use Silk\Post\PostTypeBuilder;
12
13
/**
14
 * @property-read string $slug
15
 * @property-read string $one
16
 * @property-read string $many
17
 */
18
class PostType
19
{
20
    /**
21
     * Post type object
22
     * @var stdClass
23
     */
24
    protected $object;
25
26
    /**
27
     * PostType Constructor
28
     *
29
     * @param object post type object
30
     */
31
    public function __construct(stdClass $object)
32
    {
33
        $this->object = $object;
34
    }
35
36
    /**
37
     * Create a new instance using the post type slug.
38
     *
39
     * Loads an existing type, or returns a new builder for registering a new type.
40
     *
41
     * @param  string $slug  string - the post type slug ("name")
42
     *
43
     * @return mixed         static - if the post type has been registered
44
     *                       PostTypeBuilder otherwise
45
     */
46
    public static function make($slug)
47
    {
48
        if (static::exists($slug)) {
49
            return static::load($slug);
50
        }
51
52
        return new PostTypeBuilder($slug);
53
    }
54
55
    /**
56
     * Create a new instance from an existing type.
57
     *
58
     * @param  string $slug  post type id  (slug/name)
59
     *
60
     * @return static
61
     */
62
    public static function load($slug)
63
    {
64
        if (! $object = get_post_type_object($slug)) {
65
            throw new NonExistentPostTypeException("No post type exists with name '$slug'.");
66
        }
67
68
        return new static($object);
69
    }
70
71
    /**
72
     * Checks if a post type with this slug has been registered.
73
     *
74
     * @return bool
75
     */
76
    public static function exists($slug)
77
    {
78
        return post_type_exists($slug);
79
    }
80
81
    /**
82
     * Get the post type object.
83
     *
84
     * @return object
85
     */
86
    public function object()
87
    {
88
        return $this->object;
89
    }
90
91
    /**
92
     * Check for feature support.
93
     *
94
     * @param mixed $features  string - feature
95
     *                         array - many features
96
     *
97
     * @return mixed
98
     */
99
    public function supports($features)
100
    {
101
        if (! is_array($features)) {
102
            $features = func_get_args();
103
        }
104
105
        return ! collect($features)
106
            ->contains(function ($key, $feature) {
107
                return ! post_type_supports($this->slug, $feature);
108
            });
109
    }
110
111
    /**
112
     * Register support of certain features for an existing post type.
113
     *
114
     * @param mixed $features  string - single feature to add
115
     *                        array - multiple features to add
116
     */
117
    public function addSupportFor($features)
118
    {
119
        add_post_type_support($this->slug, is_array($features) ? $features : func_get_args());
120
121
        return $this;
122
    }
123
124
    /**
125
     * Deregister support of certain features for an existing post type.
126
     *
127
     * @param mixed $features  string - single feature to remove
128
     *                        array - multiple features to remove
129
     */
130
    public function removeSupportFor($features)
131
    {
132
        collect(is_array($features) ? $features : func_get_args())
133
            ->each(function ($features) {
134
                remove_post_type_support($this->slug, $features);
135
            });
136
137
        return $this;
138
    }
139
140
    /**
141
     * Unregister the post type
142
     *
143
     * @return $this
144
     */
145
    public function unregister()
146
    {
147
        if (! static::exists($this->slug)) {
148
            throw new NonExistentPostTypeException("No post type exists with name '{$this->slug}'.");
149
        }
150
151
        $result = unregister_post_type($this->slug);
152
153
        if (is_wp_error($result)) {
154
            throw new WP_ErrorException($result);
155
        }
156
157
        return $this;
158
    }
159
160
    /**
161
     * Magic Getter
162
     *
163
     * @param  string $property
164
     *
165
     * @return mixed
166
     */
167
    public function __get($property)
168
    {
169
        switch ($property) :
170
            case 'slug': return $this->object->name;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
171
            case 'one':  return $this->object->labels->singular_name;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
172
            case 'many': return $this->object->labels->name;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
173
        endswitch;
174
175
        return null;
176
    }
177
}
178