CommentEntityManager::whereAuthorEmail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\System\Schema\Comment;
4
5
use Leonidas\Contracts\System\Schema\Comment\CommentConverterInterface;
6
use Leonidas\Contracts\System\Schema\Comment\CommentEntityManagerInterface;
7
use Leonidas\Contracts\System\Schema\EntityCollectionFactoryInterface;
8
use Leonidas\Library\System\Schema\Abstracts\NoCommitmentsTrait;
9
use Leonidas\Library\System\Schema\Abstracts\ThrowsExceptionOnWpErrorTrait;
10
use WP_Comment;
0 ignored issues
show
Bug introduced by
The type WP_Comment 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...
11
use WP_Comment_Query;
0 ignored issues
show
Bug introduced by
The type WP_Comment_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...
12
13
class CommentEntityManager implements CommentEntityManagerInterface
14
{
15
    use NoCommitmentsTrait;
16
    use ThrowsExceptionOnWpErrorTrait;
17
18
    protected string $type;
19
20
    protected CommentConverterInterface $entityConverter;
21
22
    protected EntityCollectionFactoryInterface $collectionFactory;
23
24
    public function __construct(
25
        string $type,
26
        CommentConverterInterface $commentConverter,
27
        EntityCollectionFactoryInterface $collectionFactory
28
    ) {
29
        $this->type = $type;
30
        $this->entityConverter = $commentConverter;
31
        $this->collectionFactory = $collectionFactory;
32
    }
33
34
    public function select(int $id): ?object
35
    {
36
        return $this->single(['comment_in' => [$id]]);
37
    }
38
39
    public function whereIds(int ...$ids): object
40
    {
41
        return $this->query(['comment_in' => $ids]);
42
    }
43
44
    public function whereUserIds(int ...$userIds): object
45
    {
46
        return $this->query(['author__in' => $userIds]);
47
    }
48
49
    public function whereAuthorEmail(string $authorEmail): object
50
    {
51
        return $this->query(['author_email' => $authorEmail]);
52
    }
53
54
    public function whereAuthorUrl(string $authorUrl): object
55
    {
56
        return $this->query(['author_url' => $authorUrl]);
57
    }
58
59
    public function whereParentIds(int ...$parentId): object
60
    {
61
        return $this->query(['parent__in' => $parentId]);
62
    }
63
64
    public function whereApprovedOnPost(int $postId): object
65
    {
66
        return $this->query(['post_id' => $postId, 'status' => 'approve']);
67
    }
68
69
    public function wherePostAndStatus(int $postId, string $status): object
70
    {
71
        return $this->query(['post_id' => $postId, 'status' => $status]);
72
    }
73
74
    public function all(): object
75
    {
76
        return $this->query([]);
77
    }
78
79
    /**
80
     * @link https://developer.wordpress.org/reference/classes/WP_Comment_Query/__construct/
81
     */
82
    public function query(array $args): object
83
    {
84
        return $this->getCollectionFromQuery($this->getQuery($args));
85
    }
86
87
    public function single(array $args): ?object
88
    {
89
        $comments = $this->getQuery($args)->get_comments();
90
91
        return $comments ? $this->convertEntity($comments[0]) : null;
92
    }
93
94
    public function spawn(array $data): object
95
    {
96
        return $this->convertEntity(
97
            /** @phpstan-ignore-next-line */
98
            new WP_Comment((object) $data)
99
        );
100
    }
101
102
    /**
103
     * @link https://developer.wordpress.org/reference/functions/wp_insert_comment/
104
     */
105
    public function insert(array $data): void
106
    {
107
        wp_insert_comment($this->normalizeDataForEntry($data));
0 ignored issues
show
Bug introduced by
The function wp_insert_comment 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

107
        /** @scrutinizer ignore-call */ 
108
        wp_insert_comment($this->normalizeDataForEntry($data));
Loading history...
108
    }
109
110
    public function update(int $id, array $data): void
111
    {
112
        $this->throwExceptionIfWpError(
113
            wp_update_comment($this->normalizeDataForEntry($data, $id))
0 ignored issues
show
Bug introduced by
The function wp_update_comment 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

113
            /** @scrutinizer ignore-call */ 
114
            wp_update_comment($this->normalizeDataForEntry($data, $id))
Loading history...
114
        );
115
    }
116
117
    public function delete(int $id): void
118
    {
119
        wp_delete_comment($id);
0 ignored issues
show
Bug introduced by
The function wp_delete_comment 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

119
        /** @scrutinizer ignore-call */ 
120
        wp_delete_comment($id);
Loading history...
120
    }
121
122
    protected function getQuery(array $args): WP_Comment_Query
123
    {
124
        return new WP_Comment_Query($this->normalizeQueryArgs($args));
125
    }
126
127
    protected function getCollectionFromQuery(WP_Comment_Query $query): object
128
    {
129
        return $this->createCollection(...$query->get_comments());
130
    }
131
132
    protected function normalizeQueryArgs(array $args): array
133
    {
134
        return [
135
            'type' => $this->type,
136
            'fields' => null,
137
            'hierarchical' => false,
138
        ] + $args;
139
    }
140
141
    protected function normalizeDataForEntry(array $data, int $id = 0)
142
    {
143
        return [
144
            'comment_ID' => $id,
145
            'type' => $this->type,
146
        ] + $data;
147
    }
148
149
    protected function resolveFound($result): ?object
150
    {
151
        return $result instanceof WP_Comment ? $this->convertEntity($result) : null;
152
    }
153
154
    protected function convertEntity(WP_Comment $user): object
155
    {
156
        return $this->entityConverter->convert($user);
157
    }
158
159
    public function createCollection(WP_Comment ...$users): object
160
    {
161
        return $this->collectionFactory->createEntityCollection(
162
            ...array_map([$this, 'convertEntity'], $users)
163
        );
164
    }
165
}
166