Passed
Branch master (2b4380)
by Wilder
02:46 queued 01:22
created

WebPage::creator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 11
c 1
b 0
f 1
nc 1
nop 8
dl 0
loc 25
rs 9.9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
4
namespace WilderAmorim\StructuredData;
5
6
/**
7
 * Class WebPage
8
 * @package WilderAmorim\StructuredData
9
 */
10
class WebPage extends Schema
11
{
12
    /**
13
     * @var \stdClass
14
     */
15
    protected $data;
16
17
    /**
18
     * @see https://schema.org/WebPage
19
     */
20
    const TYPE = 'WebPage';
21
22
    /**
23
     * WebPage constructor.
24
     */
25
    public function __construct()
26
    {
27
        $this->data = new \stdClass();
28
        parent::__construct();
29
    }
30
31
    //PUBLIC METHODS
32
33
    /**
34
     * @return object
35
     */
36
    public function header(): object
37
    {
38
        return $this->data->header;
39
    }
40
41
    public function name(): string
42
    {
43
        return $this->data->name;
44
    }
45
46
    public function url(): string
47
    {
48
        return $this->data->url;
49
    }
50
51
    public function sameAs(): ?array
52
    {
53
        return $this->webPageSameAs;
54
    }
55
56
    public function description(): string
57
    {
58
        return $this->data->description;
59
    }
60
61
    public function start(
62
        string $name,
63
        string $description,
64
        string $image,
65
        string $url,
66
        string $inLanguage = 'en-US'
67
    ): WebPage {
68
69
        $this->data->name = $name;
70
        $this->data->url = $url;
71
        $this->data->description = $description;
72
        $this->data->image = $image;
73
        $this->data->inLanguage = $inLanguage;
74
75
        $this->webPageName = $this->data->name;
76
        $this->webPageUrl = $this->data->url;
77
78
        $this->data->header = (object)[
79
            '@context' => Schema::CONTEXT,
80
            '@type' => self::TYPE,
81
            'name' => $this->data->name,
82
            'description' => $this->data->description,
83
            'image' => $this->data->image,
84
            'url' => $this->data->url,
85
            'inLanguage' => $this->data->inLanguage
86
        ];
87
88
        return $this;
89
    }
90
91
    public function isPartOf(string $logo, string $image, string $type = 'WebSite', ?array $sameAs = null): WebPage
92
    {
93
        $this->webPageSameAs = $sameAs;
94
        //$publisher = $this->organization($logo, $image);
95
        $this->data->isPartOf = (object)[
96
            '@type' => $type,
97
            'name' => $this->data->name,
98
            'url' => $this->data->url,
99
            'publisher' => $this->organization($logo, $image)
100
        ];
101
        return $this;
102
    }
103
104
    public function about(string $image): WebPage
105
    {
106
        $this->data->about = $this->organization($image, $image);
107
        return $this;
108
    }
109
110
    public function creator(
111
        string $imageOrganization,
112
        string $addressLocality,
113
        string $addressRegion,
114
        string $postalCode,
115
        string $streetAddress,
116
        string $namePerson,
117
        string $photoPerson,
118
        ?array $sameAs = null
119
    ): WebPage {
120
        $dataAddress = [
121
            "addressLocality" => $addressLocality,
122
            "addressRegion" => $addressRegion,
123
            "postalCode" => $postalCode,
124
            "streetAddress" => $streetAddress
125
        ];
126
127
        $this->data->organization = $this->organization($imageOrganization, $imageOrganization, $dataAddress);
128
        $this->data->person = $this->person($namePerson, $photoPerson, $sameAs);
129
130
        $this->data->creator = [
131
            "organization" => $this->data->organization,
132
            "person" => $this->data->person
133
        ];
134
        return $this;
135
    }
136
137
    public function render(): string
138
    {
139
140
        $render = [
141
            "@context" => Schema::CONTEXT,
142
            "@type" => self::TYPE,
143
            "name" => $this->data->name,
144
            "description" => $this->data->description,
145
            "image" => $this->data->image,
146
            "url" => $this->data->url,
147
            "inLanguage" => $this->data->inLanguage,
148
            "isPartOf" => [$this->data->isPartOf],
149
            "about" => [$this->data->about],
150
            "creator" => [$this->data->organization, $this->data->person],
151
        ];
152
153
        return $this->json($render);
154
155
    }
156
157
158
    public function data()
159
    {
160
        $this->data = (object)[
161
            "header" => $this->header(),
162
            "isPartOf" => (object)$this->data->isPartOf,
163
            "about" => (object)$this->data->about,
164
            "creator" => $this->data->creator,
165
            "organization" => $this->organization,
166
            "image" => $this->imageObject
167
        ];
168
        return $this->data;
169
    }
170
171
    /**
172
     * Responsible method for debug all data
173
     */
174
    public function debug(): void
175
    {
176
        var_dump($this->data());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->data()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
177
    }
178
179
    //PROTECTED METHODS
180
181
182
    //PRIVATE METHODS
183
184
185
}