Passed
Branch master (d2b70f)
by Michael
12:26
created

Jsonld::getBreadcrumbs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Publisher;
4
5
6
/**
7
 *
8
 */
9
class Jsonld
10
{
11
    public $website = [];
12
13
    public static function getAuthor(\XoopsUser $xoopsUser)
14
    {
15
        $author = [
16
            '@type'     => 'Person',
17
            'name'      => $xoopsUser->getVar('name') ?? $xoopsUser->getVar('uname'),
18
            'email'     => $xoopsUser->getVar('email') ?? '',
19
            'telephone' => $xoopsUser->getVar('phone') ?? '',
20
            "sameAs"    => [
21
                $xoopsUser->getVar('facebook') ?? '',//"https://www.facebook.com/your-organization-url",
22
                $xoopsUser->getVar('instagram') ?? '',//"https://www.instagram.com/your-organization-url/",
23
            ],
24
        ];
25
        return $author;
26
    }
27
28
    public static function getAuthoritem($xoopsUser, array $xoopsConfig, string $xoops_url)
29
    {
30
        $ret    = '';
31
        $helper = Helper::getInstance();
32
        if ($helper->getConfig('generate_jsonld')) {
33
            $schema['@context'] = 'https://schema.org/';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$schema was never initialized. Although not strictly required by PHP, it is generally a good practice to add $schema = array(); before regardless.
Loading history...
34
            $schema['@type']    = 'Article';
35
            if ($xoopsUser instanceof \XoopsUser) {
36
                $schema['author'] = self::getAuthor($xoopsUser);
37
            }
38
            $schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url);
39
40
            $ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>';
41
        }
42
        return $ret;
43
    }
44
45
    public static function getCategory(Category $categoryObj)
46
    {
47
        $ret    = '';
48
        $helper = Helper::getInstance();
49
        if ($helper->getConfig('generate_jsonld')) {
50
            global $xoopsConfig, $xoopsUser, $xoops_url;
51
            $schema                   = [];
52
            $schema['@context']       = 'https://schema.org/';
53
            $schema['@type']          = 'Article';
54
            $schema['articleSection'] = $categoryObj->getVar('name');
55
            $schema['publisher']      = self::getOrganization($xoopsConfig, $xoops_url);
56
57
            $ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>';
58
        }
59
        return $ret;
60
    }
61
62
    public static function getArticle(Item $itemObj, Category $categoryObj, \XoopsUser $xoopsUser, Helper $helper)
63
    {
64
        $itemImage = $itemObj->getVar('image');
65
        if (isset($itemImage)) {
66
            $imageHandler = xoops_getHandler('image');
67
            $criteria     = new \Criteria('image_id', $itemObj->getVar('image'));
0 ignored issues
show
Bug introduced by
It seems like $itemObj->getVar('image') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

67
            $criteria     = new \Criteria('image_id', /** @scrutinizer ignore-type */ $itemObj->getVar('image'));
Loading history...
68
            $image        = $imageHandler->getObjects($criteria)[0]??null;
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler or XoopsModules\Publisher\PermissionHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

68
            $image        = $imageHandler->/** @scrutinizer ignore-call */ getObjects($criteria)[0]??null;
Loading history...
69
            $imageUrl = '';
70
            if (null!==$image) {
71
                $imageUrl = XOOPS_URL . '/images/' . $image->getVar('image_name');
72
            }
73
        }
74
        if ($xoopsUser instanceof \XoopsUser) {
0 ignored issues
show
introduced by
$xoopsUser is always a sub-type of XoopsUser.
Loading history...
75
            $authorName = $xoopsUser->getVar('name') ?? $xoopsUser->getVar('uname');
76
        }
77
        $item = [
78
            "@context"        => "https://schema.org",
79
            '@type'           => 'Article',
80
            'articleSection'  => $categoryObj->getVar('name'),
81
            'url'             => $helper->url('item.php?itemid=') . $itemObj->getVar('itemid'),
82
            'headline'        => $itemObj->getVar('title'),
83
            'text'            => $itemObj->getVar('body'),
84
            'datePublished'   => $itemObj->getVar('datesub'),
85
            'dateModified'    => $itemObj->getVar('datesub'),
86
            'name'            => $authorName,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $authorName does not seem to be defined for all execution paths leading up to this point.
Loading history...
87
            'aggregateRating' => [
88
                '@type'       => 'AggregateRating',
89
                'ratingValue' => $itemObj->getVar('rating'),
90
                'ratingCount' => $itemObj->getVar('votes'),
91
            ],
92
            'commentCount'    => $itemObj->getVar('comments'),
93
            'image'           => [
94
                '@type'  => 'ImageObject',
95
                'url'    => $imageUrl,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imageUrl does not seem to be defined for all execution paths leading up to this point.
Loading history...
96
                'width'  => '300',
97
                'height' => '60',
98
            ],
99
            'description'     => $itemObj->getVar('summary'),
100
        ];
101
        return $item;
102
    }
103
104
    public static function getOrganization(array $xoopsConfig, string $xoops_url)
105
    {
106
        $organization = [
107
            "@context" => "https://schema.org",
108
            "@type"    => "Organization",
109
            "name"     => $xoopsConfig['sitename'],
110
            'slogan'   => $xoopsConfig['slogan'],
111
            "url"      => $xoops_url,
112
            'logo'     => [
113
                '@type'  => 'ImageObject',
114
                'url'    => $xoops_url . '/images/logo.png',
115
                'width'  => '300',
116
                'height' => '60',
117
            ],
118
            //             "sameAs"   => [
119
            //                "https://www.facebook.com/your-organization-url",
120
            //                "https://www.instagram.com/your-organization-url/",
121
            //            ],
122
        ];
123
        return $organization;
124
    }
125
126
    public static function getWebsite($xoopsConfig, $xoops_url)
127
    {
128
        $website = [
129
            "@context" => "https://schema.org",
130
            "@type"    => "WebSite",
131
            "name"     => $xoopsConfig['sitename'],
132
            'slogan'   => $xoopsConfig['slogan'],
133
            "url"      => $xoops_url,
134
        ];
135
        return $website;
136
    }
137
138
    public static function getIndex($xoopsConfig, $xoopsUser, $xoops_url)
139
    {
140
        $ret    = '';
141
        $helper = Helper::getInstance();
142
143
        if ($helper->getConfig('generate_jsonld')) {
144
            $schema = [];
145
146
            //            $website = self::getWebsite($xoopsConfig, $xoops_url);
147
148
            $schema['@context'] = 'https://schema.org/';
149
            $schema['@type']    = 'Article';
150
            if ($xoopsUser instanceof \XoopsUser) {
151
                $schema['author'] = self::getAuthor($xoopsUser);
152
            }
153
            $schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url);
154
            $ret                 = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>';
155
        }
156
157
        return $ret;
158
    }
159
160
    public static function getBreadcrumbs(?array $data, $settings = null)
0 ignored issues
show
Unused Code introduced by
The parameter $settings is not used and could be removed. ( Ignorable by Annotation )

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

160
    public static function getBreadcrumbs(?array $data, /** @scrutinizer ignore-unused */ $settings = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

160
    public static function getBreadcrumbs(/** @scrutinizer ignore-unused */ ?array $data, $settings = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
    {
162
        //
163
        //            $breadcrumbs = [
164
        //                "@context"        => "https://schema.org",
165
        //                "@type"           => "BreadcrumbList",
166
        //                "itemListElement" => $listItems,
167
        //            ];
168
        //        }
169
        //        return $breadcrumbs;
170
    }
171
172
    public static function getItem(Item $itemObj, Category $categoryObj): string
173
    {
174
        $ret    = '';
175
        $helper = Helper::getInstance();
176
        if ($helper->getConfig('generate_jsonld')) {
177
            global $xoopsConfig, $xoopsUser, $xoops_url;
178
            $schema = [];
179
            //            $website      = self::getWebsite($xoopsConfig, $xoops_url);
180
            //            $category     = self::getCategory($categoryObj);
181
182
            if ($xoopsUser instanceof \XoopsUser) {
183
                $schema['article'] = self::getArticle($itemObj, $categoryObj, $xoopsUser, $helper);
184
                $schema['author'] = self::getAuthor($xoopsUser);
185
            }
186
            $schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url);
187
188
            $ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>';
189
        }
190
191
        return $ret;
192
    }
193
}
194
195