Completed
Push — master ( 883ebd...28a518 )
by Gino
03:07 queued 01:32
created

PostListExceptionsTrait::handleExceptionsByPost()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 5
nop 2
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Classes;
4
5
use GinoPane\BlogTaxonomy\Plugin;
6
use October\Rain\Database\Builder;
7
8
/**
9
 * Class PostListExceptionsTrait
10
 *
11
 * @package GinoPane\BlogTaxonomy\Classes
12
 */
13
trait PostListExceptionsTrait
14
{
15
    /**
16
     * Filter out posts based on their slugs or ids
17
     *
18
     * @var array
19
     */
20
    protected $exceptPosts;
21
22
    /**
23
     * Filter out posts based on their categories slugs or ids
24
     *
25
     * @var array
26
     */
27
    protected $exceptCategories;
28
29
    /**
30
     * Properties for list exceptions handling
31
     *
32
     * @return array
33
     */
34
    private function getPostExceptionProperties(): array
35
    {
36
        return [
37
            'exceptPosts' => [
38
                'group'             => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.exceptions_group',
39
                'title'             => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.except_posts_title',
40
                'description'       => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.except_posts_description',
41
                'type'              => 'string',
42
                'default'           => '',
43
                'showExternalParam' => false,
44
            ],
45
            'exceptCategories' => [
46
                'group'             => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.exceptions_group',
47
                'title'             => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.except_categories_title',
48
                'description'       => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.except_categories_description',
49
                'type'              => 'string',
50
                'default'           => '',
51
                'showExternalParam' => false,
52
            ],
53
        ];
54
    }
55
56
    /**
57
     * @return void
58
     */
59
    private function populateExceptions()
60
    {
61
        $this->exceptPosts = $this->extractArrayFromProperty('exceptPosts');
62
        $this->exceptCategories = $this->extractArrayFromProperty('exceptCategories');
63
    }
64
65
    /**
66
     * @param $query
67
     */
68
    private function handlePostExceptions(Builder $query)
69
    {
70
        self::handleExceptionsByPost($query, $this->exceptPosts);
71
        self::handleExceptionByCategory($query, $this->exceptCategories);
72
    }
73
74
    /**
75
     * @param string $property
76
     *
77
     * @return array
78
     */
79
    private function extractArrayFromProperty(string $property): array
80
    {
81
        return array_map('trim', array_filter(explode(',', $this->property($property))));
0 ignored issues
show
Bug introduced by
The method property() does not exist on GinoPane\BlogTaxonomy\Cl...PostListExceptionsTrait. Did you maybe mean extractArrayFromProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
82
    }
83
84
    /**
85
     * Separates parameters into two arrays: ids and slugs
86
     *
87
     * @param array $parameters
88
     *
89
     * @return array Ids array an slugs array
90
     */
91
    private static function separateParameters(array $parameters): array
92
    {
93
        $slugs = $parameters;
94
        $ids = [];
95
96
        foreach ($slugs as $index => $potentialId) {
97
            if (is_numeric($potentialId)) {
98
                $ids[] = $potentialId;
99
                unset($slugs[$index]);
100
            }
101
        }
102
103
        return [$ids, $slugs];
104
    }
105
106
    /**
107
     * @param Builder $query
108
     * @param array   $exceptCategories
109
     */
110
    public static function handleExceptionByCategory($query, array $exceptCategories)
111
    {
112
        if (!empty($exceptCategories)) {
113
            list($ids, $slugs) = self::separateParameters($exceptCategories);
114
115
            $query->whereDoesntHave('categories', static function (Builder $innerQuery) use ($ids, $slugs) {
116
                if (!empty($ids)) {
117
                    $innerQuery->whereIn('id', $ids);
118
                }
119
120
                if (!empty($slugs)) {
121
                    $innerQuery->whereIn('slug', $slugs);
122
                }
123
            });
124
        }
125
    }
126
127
    /**
128
     * @param Builder $query
129
     * @param array   $exceptPosts
130
     */
131
    public static function handleExceptionsByPost(Builder $query, array $exceptPosts)
132
    {
133
        if (!empty($exceptPosts)) {
134
            list($ids, $slugs) = self::separateParameters($exceptPosts);
135
136
            if (!empty($ids)) {
137
                $query->whereNotIn('id', $ids);
138
            }
139
140
            if (!empty($slugs)) {
141
                $query->whereNotIn('slug', $slugs);
142
            }
143
        }
144
    }
145
}