Passed
Pull Request — master (#1)
by
unknown
01:34
created

Optimizer::optimizer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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