Optimizer::twitterCard()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 4
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace KadokWeb\Optimizer;
4
5
/**
6
 * Class KadokWeb Optimizer
7
 *
8
 * @author Doka B. Silva <https://github.com/kadokwebe>
9
 * @package KadokWeb\Optimize
10
 */
11
class Optimizer extends MetaTags
12
{
13
    /**
14
     * @param string $title
15
     * @param string $description
16
     * @param string $url
17
     * @param string $image
18
     * @param bool $follow
19
     * @return Optimizer
20
     */
21
    public function optimize(
22
        string $title,
23
        string $description,
24
        string $url,
25
        string $image,
26
        bool $follow = true
27
    ): Optimizer {
28
        $this->data($title, $description, $url, $image);
29
30
        $title = $this->filter($title);
31
        $description = $this->filter($description);
32
33
        $this->buildTag("title", $title);
34
        $this->buildMeta("name", ["description" => $description]);
35
        $this->buildMeta("name", ["robots" => ($follow ? "index, follow" : "noindex, nofollow")]);
36
        $this->buildLink("canonical", $url);
37
38
        foreach ($this->tags as $meta => $prefix) {
39
            $this->buildMeta($meta, [
40
                "{$prefix}:title" => $title,
41
                "{$prefix}:description" => $description,
42
                "{$prefix}:url" => $url,
43
                "{$prefix}:image" => $image
44
            ]);
45
        }
46
47
        $this->buildMeta("itemprop", [
48
            "name" => $title,
49
            "description" => $description,
50
            "url" => $url,
51
            "image" => $image
52
        ]);
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $fbPage
59
     * @param string $fbAuthor
60
     * @param string $plusPage
61
     * @param string|null $plusAuthor
62
     * @return Optimizer
63
     */
64
    public function publisher(string $fbPage, string $fbAuthor, string $plusPage, string $plusAuthor = null): Optimizer
65
    {
66
        $this->buildMeta("property", [
67
            "article:author" => "https://www.facebook.com/{$fbAuthor}",
68
            "article:publisher" => "https://www.facebook.com/{$fbPage}"
69
        ]);
70
71
        if ($plusAuthor) {
72
            $this->buildLink("author", "https://plus.google.com/{$plusAuthor}");
73
        }
74
75
        if ($plusPage) {
76
            $this->buildLink("publisher", "https://plus.google.com/{$plusPage}");
77
        }
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $siteName
84
     * @param string $locale
85
     * @param string $schema
86
     * @return Optimizer
87
     */
88
    public function openGraph(string $siteName, string $locale = "pt_BR", string $schema = "article"): Optimizer
89
    {
90
        $prefix = "og";
91
        $siteName = $this->filter($siteName);
92
93
        $this->buildMeta("property", [
94
            "{$prefix}:type" => $schema,
95
            "{$prefix}:site_name" => $siteName,
96
            "{$prefix}:locale" => $locale
97
        ]);
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param string $creator
104
     * @param string $site
105
     * @param string $domain
106
     * @param string|null $card
107
     * @return Optimizer
108
     */
109
    public function twitterCard(string $creator, string $site, string $domain, string $card = null): Optimizer
110
    {
111
        $prefix = "twitter";
112
        $card = ($card ?? "summary_large_image");
113
114
        $this->buildMeta("name", [
115
            "{$prefix}:card" => $card,
116
            "{$prefix}:site" => $site,
117
            "{$prefix}:creator" => $creator,
118
            "{$prefix}:domain" => $domain
119
        ]);
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param string|null $appId
126
     * @param array|null $admins
127
     * @return Optimizer
128
     */
129
    public function facebook(string $appId = null, array $admins = null): Optimizer
130
    {
131
        if ($appId) {
132
            $fb = $this->meta->addChild("meta");
133
            $fb->addAttribute("property", "fb:app_id");
134
            $fb->addAttribute("content", $appId);
135
136
            return $this;
137
        }
138
139
        foreach ($admins as $admin) {
140
            $fb = $this->meta->addChild("meta");
141
            $fb->addAttribute("property", "fb:admins");
142
            $fb->addAttribute("content", $admin);
143
        }
144
145
        return $this;
146
    }
147
}