PostEntityManager::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\System\Schema\Post;
4
5
use Leonidas\Contracts\System\Schema\EntityCollectionFactoryInterface;
6
use Leonidas\Contracts\System\Schema\Post\PostConverterInterface;
7
use Leonidas\Contracts\System\Schema\Post\PostEntityManagerInterface;
8
use Leonidas\Contracts\System\Schema\Post\QueryContextResolverInterface;
9
use Leonidas\Contracts\System\Schema\Post\QueryFactoryInterface;
10
use Leonidas\Library\System\Schema\Abstracts\NoCommitmentsTrait;
11
use Leonidas\Library\System\Schema\Abstracts\ThrowsExceptionOnWpErrorTrait;
12
use WP_Post;
13
use WP_Query;
0 ignored issues
show
Bug introduced by
The type WP_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class PostEntityManager implements PostEntityManagerInterface
16
{
17
    use NoCommitmentsTrait;
18
    use ThrowsExceptionOnWpErrorTrait;
19
20
    protected string $type;
21
22
    protected PostConverterInterface $entityConverter;
23
24
    protected EntityCollectionFactoryInterface $collectionFactory;
25
26
    protected ?QueryFactoryInterface $queryFactory = null;
27
28
    protected QueryContextResolverInterface $contextResolver;
29
30
    public function __construct(
31
        string $type,
32
        PostConverterInterface $postConverter,
33
        EntityCollectionFactoryInterface $collectionFactory,
34
        ?QueryFactoryInterface $queryFactory = null,
35
        ?QueryContextResolverInterface $contextResolver = null
36
    ) {
37
        $this->type = $type;
38
        $this->entityConverter = $postConverter;
39
        $this->collectionFactory = $collectionFactory;
40
41
        if ($queryFactory) {
42
            $this->queryFactory = $queryFactory;
43
            $this->contextResolver = $contextResolver ?? new QueryContextResolver();
44
        }
45
    }
46
47
    public function select(int $id): ?object
48
    {
49
        return $this->single(['post__in' => [$id]]);
50
    }
51
52
    public function selectName(string $name): ?object
53
    {
54
        return $this->single(['post_name__in' => [$name]]);
55
    }
56
57
    public function whereIds(int ...$ids): object
58
    {
59
        return $this->query(['post__in' => $ids]);
60
    }
61
62
    public function whereNames(string ...$names): object
63
    {
64
        return $this->query(['post_name__in' => $names]);
65
    }
66
67
    public function whereUser(int $user): object
68
    {
69
        return $this->query(['author' => $user, 'post_status' => 'any']);
70
    }
71
72
    public function whereUserAndStatus(int $user, string $status): object
73
    {
74
        return $this->query(['author' => $user, 'post_status' => $status]);
75
    }
76
77
    public function whereParentId(int $parentId): object
78
    {
79
        return $this->query(['post_parent' => $parentId]);
80
    }
81
82
    public function whereStatus(string $status): object
83
    {
84
        return $this->query(['post_status' => $status]);
85
    }
86
87
    /**
88
     * @link https://developer.wordpress.org/reference/classes/wp_tax_query/__construct/
89
     */
90
    public function whereTaxQuery(array $args): object
91
    {
92
        return $this->query(['tax_query' => $args]);
93
    }
94
95
    public function whereTerm(string $taxonomy, int $termId): object
96
    {
97
        return $this->query([
98
            'tax_query' => [
99
                [
100
                    'field' => 'term_id',
101
                    'taxonomy' => $taxonomy,
102
                    'terms' => $termId,
103
                ],
104
            ],
105
        ]);
106
    }
107
108
    /**
109
     * @link https://developer.wordpress.org/reference/classes/wp_meta_query/__construct/
110
     */
111
    public function whereMetaQuery(array $args): object
112
    {
113
        return $this->query(['meta_query' => $args]);
114
    }
115
116
    public function whereMeta(string $key, string $operator, $value): object
117
    {
118
        return $this->query([
119
            'meta_key' => $key,
120
            'meta_value' => $value,
121
            'meta_compare' => $operator,
122
        ]);
123
    }
124
125
    public function all(): object
126
    {
127
        return $this->query([]);
128
    }
129
130
    public function spawn(array $data): object
131
    {
132
        return $this->convertEntity(new WP_Post((object) $data));
133
    }
134
135
    /**
136
     * @link https://developer.wordpress.org/reference/classes/WP_Query/parse_query/
137
     */
138
    public function query(array $args): object
139
    {
140
        return $this->getCollectionFromQuery($this->getQuery($args));
141
    }
142
143
    public function single(array $args): ?object
144
    {
145
        $posts = $this->getQuery($args)->get_posts();
146
147
        return $posts ? $this->convertEntity($posts[0]) : null;
148
    }
149
150
    /**
151
     * @link https://developer.wordpress.org/reference/functions/wp_insert_post/
152
     */
153
    public function insert(array $data): void
154
    {
155
        $this->throwExceptionIfWpError(
156
            wp_insert_post($this->normalizeDataForEntry($data))
0 ignored issues
show
Bug introduced by
The function wp_insert_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
            /** @scrutinizer ignore-call */ 
157
            wp_insert_post($this->normalizeDataForEntry($data))
Loading history...
157
        );
158
    }
159
160
    public function update(int $id, array $data): void
161
    {
162
        $this->throwExceptionIfWpError(
163
            wp_update_post($this->normalizeDataForEntry($data, $id))
0 ignored issues
show
Bug introduced by
The function wp_update_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
            /** @scrutinizer ignore-call */ 
164
            wp_update_post($this->normalizeDataForEntry($data, $id))
Loading history...
164
        );
165
    }
166
167
    public function delete(int $id): void
168
    {
169
        wp_delete_post($id, true);
0 ignored issues
show
Bug introduced by
The function wp_delete_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
        /** @scrutinizer ignore-call */ 
170
        wp_delete_post($id, true);
Loading history...
170
    }
171
172
    public function trash(int $id): void
173
    {
174
        wp_trash_post($id);
0 ignored issues
show
Bug introduced by
The function wp_trash_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
        /** @scrutinizer ignore-call */ 
175
        wp_trash_post($id);
Loading history...
175
    }
176
177
    public function recover(int $id): void
178
    {
179
        wp_untrash_post($id);
0 ignored issues
show
Bug introduced by
The function wp_untrash_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        /** @scrutinizer ignore-call */ 
180
        wp_untrash_post($id);
Loading history...
180
    }
181
182
    protected function getQuery(array $args): WP_Query
183
    {
184
        return new WP_Query($this->normalizeQueryArgs($args));
185
    }
186
187
    protected function normalizeQueryArgs($args): array
188
    {
189
        return [
190
            'post_type' => $this->type,
191
        ] + $args + [
192
            'post_status' => 'publish',
193
            'posts_per_page' => -1,
194
        ];
195
    }
196
197
    protected function normalizeDataForEntry(array $data, int $id = 0): array
198
    {
199
        return [
200
            'ID' => $id,
201
            'post_type' => $this->type,
202
            'post_parent' => is_post_type_hierarchical($this->type)
0 ignored issues
show
Bug introduced by
The function is_post_type_hierarchical was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

202
            'post_parent' => /** @scrutinizer ignore-call */ is_post_type_hierarchical($this->type)
Loading history...
203
                ? $data['post_parent']
204
                : 0,
205
        ] + $data;
206
    }
207
208
    protected function resolveFound($result): ?object
209
    {
210
        return $result instanceof WP_Post ? $this->convertEntity($result) : null;
211
    }
212
213
    protected function convertEntity(WP_Post $post): object
214
    {
215
        return $this->entityConverter->convert($post);
216
    }
217
218
    protected function getCollectionFromQuery(WP_Query $query): object
219
    {
220
        return $this->isQueryContext()
221
            ? $this->createQuery($query)
222
            : $this->createCollection(...$query->get_posts());
223
    }
224
225
    protected function isQueryContext(): bool
226
    {
227
        return isset($this->queryFactory)
228
            && $this->contextResolver->isQueryContext();
229
    }
230
231
    protected function createQuery(WP_Query $query): object
232
    {
233
        return $this->queryFactory->createQuery($query);
0 ignored issues
show
Bug introduced by
The method createQuery() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
        return $this->queryFactory->/** @scrutinizer ignore-call */ createQuery($query);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
234
    }
235
236
    protected function createCollection(WP_Post ...$posts): object
237
    {
238
        return $this->collectionFactory->createEntityCollection(
239
            ...array_map([$this, 'convertEntity'], $posts)
240
        );
241
    }
242
}
243