Passed
Push — hotfix/fix-counts ( 4b43d1...cc9e05 )
by Paul
03:52
created

Review::setTermIds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 8
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
7
use GeminiLabs\SiteReviews\Defaults\SiteReviewsDefaults;
8
use GeminiLabs\SiteReviews\Modules\Html\Partials\SiteReviews as SiteReviewsPartial;
9
use GeminiLabs\SiteReviews\Modules\Html\ReviewHtml;
10
use WP_Post;
11
12
class Review implements \ArrayAccess
13
{
14
    public $assigned_to;
15
    public $author;
16
    public $avatar;
17
    public $content;
18
    public $custom;
19
    public $date;
20
    public $email;
21
    public $ID;
22
    public $ip_address;
23
    public $modified;
24
    public $pinned;
25
    public $rating;
26
    public $response;
27
    public $review_id;
28
    public $review_type;
29
    public $status;
30
    public $term_ids;
31
    public $title;
32
    public $url;
33
    public $user_id;
34
35 1
    public function __construct(WP_Post $post)
36
    {
37 1
        if (Application::POST_TYPE != $post->post_type) {
38
            return;
39
        }
40 1
        $this->content = $post->post_content;
41 1
        $this->date = $post->post_date;
42 1
        $this->ID = intval($post->ID);
43 1
        $this->status = $post->post_status;
44 1
        $this->title = $post->post_title;
45 1
        $this->user_id = intval($post->post_author);
46 1
        $this->setProperties($post);
47 1
        $this->setTermIds($post);
48 1
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public function __get($key)
54
    {
55
        return $this->offsetGet($key);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        return (string) $this->build();
64
    }
65
66
    /**
67
     * @return ReviewHtml
68
     */
69
    public function build(array $args = [])
70
    {
71
        if (empty($this->ID)) {
72
            return new ReviewHtml($this);
73
        }
74
        $partial = glsr(SiteReviewsPartial::class);
75
        $partial->args = glsr(SiteReviewsDefaults::class)->merge($args);
76
        $partial->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all());
77
        return $partial->buildReview($this);
78
    }
79
80
    /**
81
     * @param mixed $key
82
     * @return bool
83
     */
84
    public function offsetExists($key)
85
    {
86
        return property_exists($this, $key) || array_key_exists($key, (array) $this->custom);
87
    }
88
89
    /**
90
     * @param mixed $key
91
     * @return mixed
92
     */
93
    public function offsetGet($key)
94
    {
95
        if (property_exists($this, $key)) {
96
            return $this->{$key};
97
        }
98
        return array_key_exists($key, (array) $this->custom)
99
            ? $this->custom[$key]
100
            : null;
101
    }
102
103
    /**
104
     * @param mixed $key
105
     * @param mixed $value
106
     * @return void
107
     */
108
    public function offsetSet($key, $value)
109
    {
110
        if (property_exists($this, $key)) {
111
            $this->{$key} = $value;
112
            return;
113
        }
114
        if (!is_array($this->custom)) {
115
            $this->custom = array_filter((array) $this->custom);
116
        }
117
        $this->custom[$key] = $value;
118
    }
119
120
    /**
121
     * @param mixed $key
122
     * @return void
123
     */
124
    public function offsetUnset($key)
125
    {
126
        $this->offsetSet($key, null);
127
    }
128
129
    /**
130
     * @return void
131
     */
132
    public function render()
133
    {
134
        echo $this->build();
135
    }
136
137
    /**
138
     * @return bool
139
     */
140 1
    protected function isModified(array $properties)
141
    {
142 1
        return $this->date != $properties['date']
143 1
            || $this->content != $properties['content']
144 1
            || $this->title != $properties['title'];
145
    }
146
147
    /**
148
     * @return void
149
     */
150 1
    protected function setProperties(WP_Post $post)
151
    {
152
        $defaults = [
153 1
            'author' => __('Anonymous', 'site-reviews'),
154 1
            'date' => '',
155 1
            'review_id' => '',
156 1
            'review_type' => 'local',
157
        ];
158 1
        $meta = array_filter(
159 1
            array_map('array_shift', array_filter((array) get_post_meta($post->ID))),
160 1
            'strlen'
161
        );
162 1
        $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta));
163 1
        $this->modified = $this->isModified($properties);
164 1
        array_walk($properties, function ($value, $key) {
165 1
            if (!property_exists($this, $key) || isset($this->$key)) {
166 1
                return;
167
            }
168 1
            $this->$key = maybe_unserialize($value);
169 1
        });
170 1
    }
171
172
    /**
173
     * @return void
174
     */
175 1
    protected function setTermIds(WP_Post $post)
176
    {
177 1
        $this->term_ids = [];
178 1
        if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) {
179 1
            return;
180
        }
181
        foreach ($terms as $term) {
182
            $this->term_ids[] = $term->term_id;
183
        }
184
    }
185
}
186