SocialShare::linkedin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
4
namespace ElePHPant\SocialShare;
5
6
7
/**
8
 * Class SocialShare
9
 *
10
 * Please report bugs on https://github.com/wilderamorim/social-share/issues
11
 *
12
 * @package ElePHPant\SocialShare
13
 * @author Wilder Amorim <[email protected]>
14
 * @copyright Copyright (c) 2020, Uebi. All rights reserved
15
 * @license MIT License
16
 */
17
class SocialShare
18
{
19
    /** @var string */
20
    protected $url;
21
22
    /** @var string */
23
    protected $text;
24
25
    /**
26
     * SocialShare constructor.
27
     * @param string $url   Address of the page to be shared
28
     * @param string $text  Page title or whatever title you want to assign to the content
29
     */
30
    public function __construct(string $url, string $text)
31
    {
32
        $this->url = $url;
33
        $this->text = $text;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function facebook(): string
40
    {
41
        $url = 'https://www.facebook.com/sharer/sharer.php?';
42
        $url .= http_build_query(['u' => $this->url]);
43
        return $url;
44
    }
45
46
    /**
47
     * @param string|null $username Your Twitter username, e.g., rasmus
48
     * @return string
49
     */
50
    public function twitter(?string $username = null): string
51
    {
52
        $url = 'http://twitter.com/share?';
53
        $url .= http_build_query([
54
            'text' => $this->text,
55
            'url' => $this->url,
56
            'via' => str_replace('@', null, $username)
57
        ]);
58
        return $url;
59
    }
60
61
    /**
62
     * @param string|null $summary  Description of page content
63
     * @param string|null $source   Name of the content source, such as the name of the website or blog where the content is
64
     * @return string
65
     */
66
    public function linkedin(?string $summary = null, ?string $source = null): string
67
    {
68
        $url = 'https://www.linkedin.com/shareArticle?mini=true&';
69
        $url .= http_build_query([
70
            'title' => $this->text,
71
            'summary' => $summary,
72
            'url' => $this->url,
73
            'source' => $source
74
        ]);
75
        return $url;
76
    }
77
78
    /**
79
     * @param string|null $image Path (URL) to the image.
80
     * @return string
81
     */
82
    public function pinterest(?string $image = null): string
83
    {
84
        $url = 'https://pinterest.com/pin/create/button/?';
85
        $url .= http_build_query([
86
            'url' => $this->url,
87
            'media' => $image,
88
            'description' => $this->text
89
        ]);
90
        return $url;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function whatsapp(): string
97
    {
98
        $url = 'https://wa.me/?';
99
        $url .= http_build_query(['text' => $this->text . ' - ' . $this->url]);
100
        return $url;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function telegram(): string
107
    {
108
        $url = 'https://telegram.me/share/url?';
109
        $url .= http_build_query([
110
            'url' => $this->url,
111
            'text' => $this->text
112
        ]);
113
        return $url;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function reddit(): string
120
    {
121
        $url = 'https://www.reddit.com/submit?';
122
        $url .= http_build_query([
123
            'title' => $this->text,
124
            'url' => $this->url
125
        ]);
126
        return $url;
127
    }
128
129
    /**
130
     * @param string|null $recipientEmail   Recipient's Email
131
     * @return string
132
     */
133
    public function email(?string $recipientEmail = null): string
134
    {
135
        $url = 'mailto:' . $recipientEmail;
136
        $url .= '?subject=' . $this->text;
137
        $url .= '&body=' . urlencode($this->url);
138
        return $url;
139
    }
140
}