Passed
Push — master ( 4f783c...449b2c )
by Paul
07:14
created

PostManager::normalizeId()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 22.9226

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 23
ccs 5
cts 16
cp 0.3125
rs 8.8333
cc 7
nc 5
nop 1
crap 22.9226
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Modules\Multilingual;
9
10
class PostManager
11
{
12
    /**
13
     * @param int|string $postId
14
     * @return int
15
     */
16 3
    public function normalizeId($postId)
17
    {
18 3
        if ('parent_id' == $postId) {
19
            $postId = wp_get_post_parent_id(intval(get_the_ID()));
20
        }
21 3
        elseif ('post_id' == $postId) {
22
            $postId = get_the_ID();
23
        }
24 3
        elseif (!is_numeric($postId) && is_string($postId)) {
25
            $parts = explode(':', $postId);
26
            $type = Arr::get($parts, 0);
27
            $slug = Arr::get($parts, 1);
28
            if (!empty($slug) && !empty($type)) {
29
                $args = [
30
                    'fields' => 'ids',
31
                    'post_name__in' => [$slug],
32
                    'post_type' => $type,
33
                    'posts_per_page' => 1,
34
                ];
35
                $postId = Arr::get(get_posts($args), 0);
36
            }
37
        }
38 3
        return Helper::getPostId($postId);
39
    }
40
41
    /**
42
     * @param array|string $postIds
43
     * @return array
44
     */
45 21
    public function normalizeIds($postIds)
46
    {
47 21
        $postIds = Cast::toArray($postIds);
48 21
        foreach ($postIds as &$postId) {
49 3
            $postId = $this->normalizeId($postId);
50
        }
51 21
        return Arr::uniqueInt(glsr(Multilingual::class)->getPostIds($postIds));
52
    }
53
}
54