Passed
Push — master ( 2ca493...cf52af )
by Evgenii
02:19
created

MetaMaster::getAbsuluteUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 05.07.2018
6
 * Time: 11:11
7
 */
8
9
namespace floor12\metamaster;
10
11
use Yii;
12
use yii\base\Component;
13
use yii\web\Request;
14
use yii\web\View;
15
16
/**
17
 * Class MetaMaster
18
 * @package floor12\metamaster
19
 * @property string $siteName
20
 * @property string $type
21
 * @property string $title
22
 * @property string $keywords
23
 * @property string $description
24
 * @property string $url
25
 * @property string $defaultImage
26
 * @property string $image
27
 * @property string $imagePath
28
 * @property string $web
29
 * @property View $view
30
 */
31
class MetaMaster extends Component
32
{
33
    /**
34
     * @var string
35
     */
36
    public $siteName = 'My Test Application';
37
    /**
38
     * @var string
39
     */
40
    public $web = "@app/web";
41
    /**
42
     * @var string
43
     */
44
    public $defaultImage;
45
    /**
46
     * @var string
47
     */
48
    public $protocol = 'https';
49
    /**
50
     * @var string
51
     */
52
    private $type = 'article';
53
    /**
54
     * @var Request
55
     */
56
    private $request;
57
    /**
58
     * @var string
59
     */
60
    private $title;
61
    /**
62
     * @var string
63
     */
64
    private $description;
65
    /**
66
     * @var string
67
     */
68
    private $url;
69
    /**
70
     * @var string
71
     */
72
    private $image;
73
    /**
74
     * @var string
75
     */
76
    private $imagePath;
77
    /**
78
     * @var string
79
     */
80
    private $view;
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function init()
86
    {
87
        if (!$this->request)
88
            $this->request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\console\Request. However, the property $request is declared as type yii\web\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
89
        parent::init();
90
    }
91
92
    /** Site name setter
93
     * @param $siteName
94
     * @return $this
95
     */
96
    public function setSiteName(string $siteName)
97
    {
98
        $this->siteName = $siteName;
99
        return $this;
100
    }
101
102
    /** Page title setter
103
     * @param $title
104
     * @return $this
105
     */
106
    public function setTitle(string $title)
107
    {
108
        $this->title = $title;
109
        return $this;
110
    }
111
112
    /** Url setter
113
     * @param $title
114
     * @return $this
115
     */
116
    public function setUrl(string $url)
117
    {
118
        $this->url = $url;
119
        return $this;
120
    }
121
122
    /** Set request object
123
     * @param $title
124
     * @return $this
125
     */
126
    public function setRequest($request)
127
    {
128
        $this->request = $request;
129
        return $this;
130
    }
131
132
133
    /** OgType setter
134
     * @param $type
135
     * @return $this
136
     */
137
    public function setType(string $type)
138
    {
139
        $this->type = $type;
140
        return $this;
141
    }
142
143
    /** Meta description setter
144
     * @param string $description
145
     * @return $this
146
     */
147
    public function setDescription(string $description)
148
    {
149
        $this->description = $description;
150
        return $this;
151
    }
152
153
    /** Meta keyword setter is deprecated from 1.1.0
154
     * @param string $keywords
155
     * @return $this
156
     * @deprecated
157
     */
158
    public function setKeywords(string $keywords)
0 ignored issues
show
Unused Code introduced by
The parameter $keywords 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

158
    public function setKeywords(/** @scrutinizer ignore-unused */ string $keywords)

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...
159
    {
160
        return $this;
161
    }
162
163
    /** Set Open Graph image tag
164
     * @param string $image
165
     * @param string|null $imagePath
166
     * @return $this
167
     */
168
    public function setImage(string $image, string $imagePath = null)
169
    {
170
        $this->image = $image;
171
        $this->imagePath = $imagePath;
172
        return $this;
173
    }
174
175
    /** Register meta tags in View
176
     * @param View $view
177
     */
178
    public function register(View $view)
179
    {
180
        $this->view = $view;
181
        $this->registerCoreInfo();
182
        $this->registerTitle();
183
        $this->registerDescription();
184
        $this->registerImage();
185
    }
186
187
    /**
188
     * Register core meta and og tags
189
     */
190
    private function registerCoreInfo()
191
    {
192
        $this->view->registerMetaTag(['property' => 'og:site_name', 'content' => $this->siteName]);
193
        $this->view->registerMetaTag(['property' => 'og:type', 'content' => $this->type]);
194
        $this->view->registerMetaTag(['property' => 'og:url', 'content' => $this->url ?: $this->getAbsoluteUrl()]);
195
        $this->view->registerMetaTag(['name' => 'twitter:card', 'content' => 'summary']);
196
        $this->view->registerMetaTag(['name' => 'twitter:domain', 'content' => str_replace(['http', 'https'], $this->protocol, $this->request->hostInfo)]);
197
        $this->view->registerMetaTag(['name' => 'twitter:site', 'content' => $this->siteName]);
198
        $this->view->registerMetaTag(['name' => 'twitter:site', 'content' => $this->siteName]);
199
        $this->view->registerLinkTag(['rel' => 'canonical', 'href' => $this->url ?: $this->getAbsoluteUrl()]);
200
    }
201
202
    /**
203
     * @param null $absoluteUrl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $absoluteUrl is correct as it would always require null to be passed?
Loading history...
204
     * @return mixed
205
     */
206
    public function getAbsoluteUrl($absoluteUrl = null)
207
    {
208
        if (empty($absoluteUrl))
209
            $absoluteUrl = $this->request->absoluteUrl;
210
        return preg_replace('/https|http/', $this->protocol, $absoluteUrl, -1, $count);
211
    }
212
213
    /**
214
     * Register title
215
     */
216
    private function registerTitle()
217
    {
218
        if ($this->title) {
219
            $this->view->title = $this->title;
220
            $this->view->registerMetaTag(['property' => 'og:title', 'content' => $this->title]);
221
            $this->view->registerMetaTag(['itemprop' => 'name', 'content' => $this->title]);
222
        }
223
    }
224
225
    /**
226
     * Register description
227
     */
228
    private function registerDescription()
229
    {
230
        if ($this->description) {
231
            $this->view->registerMetaTag(['name' => 'description', 'content' => $this->description]);
232
            $this->view->registerMetaTag(['property' => 'og:description', 'content' => $this->description]);
233
            $this->view->registerMetaTag(['name' => 'twitter:description', 'content' => $this->description]);
234
235
        }
236
    }
237
238
    /**
239
     * Register image
240
     */
241
    private function registerImage()
242
    {
243
        $image = $this->image ?: $this->defaultImage;
244
        if ($image) {
245
            $imageUrl = str_replace(['http', 'https'], $this->protocol, $this->request->hostInfo . $image);
246
            $this->view->registerMetaTag(['property' => 'og:image', 'content' => $imageUrl]);
247
            $this->view->registerMetaTag(['property' => 'twitter:image:src', 'content' => $imageUrl]);
248
            $this->view->registerMetaTag(['itemprop' => 'image', 'content' => $imageUrl]);
249
250
        }
251
252
        $path = Yii::getAlias($this->imagePath ?: $this->web . $image);
253
        if ($this->imagePath)
254
            $path = $this->imagePath;
255
        if (file_exists($path)) {
256
            $imageSize = getimagesize($path);
257
            $this->view->registerMetaTag(['property' => 'og:image:width', 'content' => $imageSize[0]]);
258
            $this->view->registerMetaTag(['property' => 'og:image:height', 'content' => $imageSize[1]]);
259
        }
260
    }
261
}