Test Failed
Push — develop ( 872656...e23a48 )
by Paul
07:41
created

Addon::posts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Addons;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Contracts\DefaultsContract;
7
use GeminiLabs\SiteReviews\Contracts\PluginContract;
8
use GeminiLabs\SiteReviews\Database;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
use GeminiLabs\SiteReviews\Plugin;
11
12
/**
13
 * @property string $file
14
 * @property string $id
15
 * @property string $languages
16
 * @property bool   $licensed
17
 * @property string $name
18
 * @property string $slug
19
 * @property string $testedTo
20
 * @property string $version
21
 */
22
abstract class Addon implements PluginContract
23
{
24
    use Plugin;
25
26
    public const ID = '';
27
    public const LICENSED = false;
28
    public const NAME = '';
29
    public const POST_TYPE = '';
30
    public const SLUG = '';
31
32
    /**
33
     * @return static
34
     */
35
    public function init()
36
    {
37
        $reflection = new \ReflectionClass($this);
38
        $hooks = Str::replaceLast($reflection->getShortname(), 'Hooks', $reflection->getName());
39
        if (class_exists($hooks)) {
40
            glsr()->singleton($hooks);
41
            glsr($hooks)->run();
42
        } else {
43
            glsr_log()->error('The '.static::NAME.' addon is missing a Hooks class');
44
        }
45
        return $this;
46
    }
47
48
    public function make(string $class, array $parameters = [])
49
    {
50
        $class = Str::camelCase($class);
51
        $class = ltrim(str_replace([__NAMESPACE__, 'GeminiLabs\SiteReviews'], '', $class), '\\');
52
        $class = __NAMESPACE__.'\\'.$class;
53
        return glsr($class, $parameters);
54
    }
55
56
    /**
57
     * @param mixed $fallback
58
     *
59
     * @return mixed
60
     */
61
    public function option(string $path = '', $fallback = '', string $cast = '')
62
    {
63
        $path = Str::removePrefix($path, 'settings.');
64
        $path = Str::prefix($path, 'addons.'.static::SLUG.'.');
65
        return glsr_get_option($path, $fallback, $cast);
66
    }
67
68
    /**
69
     * You can pass a Defaults class which will be used to restrict the options.
70
     */
71
    public function options(string $defaultsClass = ''): Arguments
72
    {
73
        $options = glsr_get_option('settings.addons.'.static::SLUG, [], 'array');
74
        if (is_a($defaultsClass, DefaultsContract::class, true)) {
75
            $options = glsr($defaultsClass)->restrict($options);
76
        }
77
        return glsr()->args($options);
78
    }
79
80
    public function posts(int $perPage = -1, string $placeholder = ''): array
81
    {
82
        if (empty(static::POST_TYPE)) {
83
            return [];
84
        }
85
        $results = glsr(Database::class)->posts([
86
            'post_status' => 'publish',
87
            'post_type' => static::POST_TYPE,
88
            'posts_per_page' => $perPage,
89
        ]);
90
        if (!empty($placeholder)) {
91
            return ['' => $placeholder] + $results;
92
        }
93
        return $results;
94
    }
95
}
96