Passed
Branch master (e5d775)
by Michael
25:14 queued 11:39
created

Jsonld::getWebsite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Publisher;
4
5
/**
6
 *
7
 */
8
class Jsonld
9
{
10
    public $website = [];
11
12
    public static function getAuthor(\XoopsUser $xoopsUser)
13
    {
14
        $author = [
15
            '@type'     => 'Person',
16
            'name'      => $xoopsUser->getVar('name') ?? $xoopsUser->getVar('uname'),
17
            'email'     => $xoopsUser->getVar('email') ?? '',
18
            'telephone' => $xoopsUser->getVar('phone') ?? '',
19
            'sameAs' => [
20
                $xoopsUser->getVar('facebook') ?? '',//"https://www.facebook.com/your-organization-url",
21
                $xoopsUser->getVar('instagram') ?? '',//"https://www.instagram.com/your-organization-url/",
22
            ],
23
        ];
24
        return $author;
25
    }
26
27
    public static function getAuthoritem($xoopsUser, array $xoopsConfig, string $xoops_url)
28
    {
29
        $ret    = '';
30
        $helper = Helper::getInstance();
31
        if ($helper->getConfig('generate_jsonld')) {
32
            $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...
33
            $schema['@type']    = 'Article';
34
            if ($xoopsUser instanceof \XoopsUser) {
35
                $schema['author'] = self::getAuthor($xoopsUser);
36
            }
37
            $schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url);
38
39
            $ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>';
40
        }
41
        return $ret;
42
    }
43
44
    public static function getCategory(Category $categoryObj)
45
    {
46
        $ret    = '';
47
        $helper = Helper::getInstance();
48
        if ($helper->getConfig('generate_jsonld')) {
49
            global $xoopsConfig, $xoopsUser, $xoops_url;
50
            $schema                   = [];
51
            $schema['@context']       = 'https://schema.org/';
52
            $schema['@type']          = 'Article';
53
            $schema['articleSection'] = $categoryObj->getVar('name');
54
            $schema['publisher']      = self::getOrganization($xoopsConfig, $xoops_url);
55
56
            $ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>';
57
        }
58
        return $ret;
59
    }
60
61
    public static function getArticle(Item $itemObj, Category $categoryObj, \XoopsUser $xoopsUser, Helper $helper)
62
    {
63
        $itemImage = $itemObj->getVar('image');
64
        if (isset($itemImage)) {
65
            $imageHandler = \xoops_getHandler('image');
66
            $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

66
            $criteria     = new \Criteria('image_id', /** @scrutinizer ignore-type */ $itemObj->getVar('image'));
Loading history...
67
            $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

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

159
    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...
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

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