PageSEO   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 33
eloc 146
c 1
b 0
f 0
dl 0
loc 223
rs 9.76

4 Methods

Rating   Name   Duplication   Size   Complexity  
C title() 0 56 12
C description() 0 63 14
B all() 0 81 5
A breadcrumb() 0 15 2
1
<?php
2
3
namespace PageSEO;
4
5
use ChrisKonnertz\OpenGraph\OpenGraph;
6
7
class PageSEO
8
{
9
    public function title($type, array $attr, $locale='en')
10
    {
11
        $name = $attr['name'];
12
        $site = $attr['site_name'];
13
        $brand = isset($attr['brand']) ? $attr['brand'] : null;
14
15
        switch ($locale) {
16
        case 'da':
17
        case 'dk':
18
        case 'danish':
19
        case 'dansk':
20
            switch ($type) {
21
            case 'brand':
22
                $return = sprintf('1000vis af RABATTER på %s @ %s', $name, $site);
23
                break;
24
25
            case 'product':
26
                if ($brand) {
27
                    $return = sprintf('NYT, %s fra %s @ %s', $name, $brand, $site);
28
                } else {
29
                    $return = sprintf('NYT, %s @ %s', $name, $site);
30
                }
31
32
                break;
33
34
            default:
35
                $return = sprintf('%s @ %s', $name, $site);
36
                break;
37
            }
38
39
            break;
40
41
        default:
42
            switch ($type) {
43
            case 'brand':
44
                $return = sprintf('1000s of DISCOUNTS on %s @ %s', $name, $site);
45
                break;
46
47
            case 'product':
48
                if ($brand) {
49
                    $return = sprintf('NEW, %s by %s @ %s', $name, $brand, $site);
50
                } else {
51
                    $return = sprintf('NEW, %s @ %s', $name, $site);
52
                }
53
54
                break;
55
56
            default:
57
                $return = sprintf('%s @ %s', $name, $site);
58
                break;
59
            }
60
61
            break;
62
        }
63
64
        return $return;
65
    }
66
67
    public function description($type, array $attr, $locale='en')
68
    {
69
        $name = $attr['name'];
70
        $brand = isset($attr['brand']) ? $attr['brand'] : null;
71
72
        switch ($locale) {
73
        case 'da':
74
        case 'dk':
75
        case 'danish':
76
        case 'dansk':
77
            switch ($type) {
78
            case 'article':
79
                $return = substr($attr['body'], 0, 155);
80
                break;
81
82
            case 'brand':
83
                $return = sprintf('1000vis af RABATTER på %s', $name);
84
                break;
85
86
            case 'product':
87
                if ($brand) {
88
                    $return = sprintf('Aktuelt TILBUD på %s / %s', $name, $brand);
89
                } else {
90
                    $return = sprintf('Aktuelt TILBUD på %s', $name);
91
                }
92
93
                break;
94
95
            default:
96
                $return = substr($attr['description'], 0, 155);
97
            }
98
99
            break;
100
101
        default:
102
            switch ($type) {
103
            case 'article':
104
                $return = substr($attr['body'], 0, 155);
105
                break;
106
107
            case 'brand':
108
                $return = sprintf('1000s of DISCOUNTS on %s', $name);
109
                break;
110
111
            case 'product':
112
                if ($brand) {
113
                    $return = sprintf('Current DISCOUNTS on %s / %s', $name, $brand);
114
                } else {
115
                    $return = sprintf('Current DISCOUNTS on %s', $name);
116
                }
117
118
                break;
119
120
            default:
121
                $return = substr($attr['description'], 0, 155);
122
123
                break;
124
            }
125
126
            break;
127
        }
128
129
        return $return;
130
    }
131
132
    public function breadcrumb($routes)
133
    {
134
        $params = [];
135
        foreach ($routes as $route) {
136
            $params[] = [
137
                'url' => $route['url'],
138
                'name' => $route['name'],
139
            ];
140
        }
141
142
        $breadcrumbs = \JsonLd\Context::create('breadcrumb_list', [
143
            'itemListElement' => $params,
144
        ]);
145
146
        return $breadcrumbs;
147
    }
148
149
    public function all($type, array $attr, $locale='en')
150
    {
151
        $res = new \StdClass();
152
153
        $brand = isset($attr['brand']) ? $attr['brand'] : null;
154
155
        $res->title = $this->title(
156
            $type,
157
            $attr,
158
            $locale
159
        );
160
161
        $res->description = $this->description(
162
            $type,
163
            $attr,
164
            $locale
165
        );
166
167
        $res->breadcrumb = null;
168
        if (isset($attr['breadcrumb'])) {
169
            $res->breadcrumb = $this->breadcrumb($attr['breadcrumb']);
170
        }
171
172
        switch ($type) {
173
        case 'product':
174
            $res->json = \JsonLd\Context::create('product', [
175
                'name' => $attr['name'],
176
                'description' => $attr['description'],
177
                'brand' => $brand,
178
                'sku' => $attr['sku'],
179
                'url' => $attr['url'],
180
                'offers' => [
181
                    'price' => $attr['price'],
182
                    'priceCurrency' => $attr['currency'],
183
                ],
184
                'category' => $attr['category'],
185
            ]);
186
187
            $res->og = new OpenGraph();
188
            $res->og
189
                ->type('product')
190
                ->title($res->title)
191
                ->image($attr['image_url'])
192
                ->siteName($attr['site_name'])
193
                ->description($res->description)
0 ignored issues
show
Bug introduced by
$res->description of type string is incompatible with the type integer expected by parameter $description of ChrisKonnertz\OpenGraph\OpenGraph::description(). ( Ignorable by Annotation )

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

193
                ->description(/** @scrutinizer ignore-type */ $res->description)
Loading history...
194
                ->url($attr['url'])
195
                ;
196
197
            break;
198
199
        case 'article':
200
            $res->json = \JsonLd\Context::create('article', [
201
                'headline' => $attr['name'],
202
                'image' => $attr['image_url'],
203
                'author' => [
204
                    'name' => $attr['author'],
205
                ],
206
                'publisher' => [
207
                    'name' => $attr['publisher'],
208
                ],
209
                'mainEntityOfPage' => $attr['url'],
210
                'datePublished' => $attr['published_at'],
211
                'dateCreated' => $attr['created_at'],
212
                'dateModified' => $attr['modified_at'],
213
                'description' => $res->description,
214
            ]);
215
216
            $res->og = new OpenGraph();
217
            $res->og
218
                ->type('article')
219
                ->title($res->title)
220
                ->image($attr['image_url'])
221
                ->siteName($attr['site_name'])
222
                ->description($res->description)
223
                ->url($attr['url'])
224
                ;
225
226
            break;
227
        }
228
229
        return $res;
230
    }
231
}
232