GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 912338...efec11 )
by Dwight
03:12 queued 01:14
created

FacebookPosterPost::getBody()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
1
<?php
2
3
namespace NotificationChannels\FacebookPoster;
4
5
use DateTimeInterface;
6
use NotificationChannels\FacebookPoster\Attachments\Image;
7
use NotificationChannels\FacebookPoster\Attachments\Video;
8
use NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent;
9
10
class FacebookPosterPost
11
{
12
    /**
13
     * The post message.
14
     *
15
     * @var string
16
     */
17
    protected $message;
18
19
    /**
20
     * The post link.
21
     *
22
     * @var string
23
     */
24
    protected $link;
25
26
    /**
27
     * The post media.
28
     *
29
     * @var \NotificationChannels\FacebookPoster\Attachments\Attachment
30
     */
31
    protected $media;
32
33
    /**
34
     * Additional post parameters.
35
     *
36
     * @var array
37
     */
38
    protected $params;
39
40
    /**
41
     * The post API endpoint.
42
     *
43
     * @var string
44
     */
45
    protected $endpoint = 'me/feed';
46
47
    /**
48
     * Create a new post instance.
49
     *
50
     * @param  string  $message
51
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
52
     */
53 6
    public function __construct($message)
54
    {
55 6
        $this->message = $message;
56 6
    }
57
58
    /**
59
     * Get the post message.
60
     *
61
     * @return string
62
     */
63 6
    public function getMessage()
64
    {
65 6
        return $this->message;
66
    }
67
68
    /**
69
     * Set a post link.
70
     *
71
     * @param  string  $link
72
     *
73
     * @return $this
74
     */
75 1
    public function withLink($link)
76
    {
77 1
        $this->link = $link;
78
79 1
        return $this;
80
    }
81
82
    /**
83
     * Set a post image.
84
     *
85
     * @param  string  $path
86
     * @param  string  $endpoint
87
     * @return $this
88
     */
89 1
    public function withImage($path, $endpoint = 'me/photos')
90
    {
91 1
        $this->media = new Image($path);
92 1
        $this->endpoint = $endpoint;
93
94 1
        return $this;
95
    }
96
97
    /**
98
     * Set a post video.
99
     *
100
     * @param  string  $path
101
     * @param  string  $title
102
     * @param  string  $description
103
     * @param  string  $endpoint
104
     * @return $this
105
     */
106 1
    public function withVideo($path, $title = null, $description = null, $endpoint = 'me/videos')
107
    {
108 1
        $this->media = new Video($path);
109 1
        $this->params['title'] = $title;
110 1
        $this->params['description'] = $description;
111 1
        $this->endpoint = $endpoint;
112
113 1
        return $this;
114
    }
115
116
    /**
117
     * Schedule the post for a date in the future.
118
     *
119
     * @param  \DateTimeInterface  $timestamp
120
     * @return $this
121
     */
122 2
    public function scheduledFor(DateTimeInterface $timestamp)
123
    {
124 2
        $this->params['published'] = false;
125 2
        $this->params['scheduled_publish_time'] = $timestamp->getTimestamp();
126
127 2
        return $this;
128
    }
129
130
    /**
131
     * Return Facebook API endpoint.
132
     *
133
     * @return string
134
     */
135 4
    public function getEndpoint()
136
    {
137 4
        return $this->endpoint;
138
    }
139
140
    /**
141
     * Get the media attached to the post.
142
     *
143
     * @return \NotificationChannels\FacebookPoster\Attachment
144
     */
145 4
    public function getMedia()
146
    {
147 4
        return $this->media;
148
    }
149
150
    /**
151
     * Build Facebook post request body.
152
     *
153
     * @return array
154
     */
155 6
    public function getBody()
156
    {
157 6
        $this->validate();
158
159
        $body = [
160 6
            'message' => $this->getMessage(),
161
        ];
162
163 6
        if ($this->link != null) {
164 1
            $body['link'] = $this->link;
165
        }
166
167 6
        if ($this->params != null) {
168 3
            $body = array_merge($body, $this->params);
169
        }
170
171 6
        return $body;
172
    }
173
174
    /**
175
     * Validate that there is acceptable post content.
176
     *
177
     * @throws \NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent
178
     */
179 6
    protected function validate()
180
    {
181 6
        if ($this->message || $this->link || $this->media) {
182 6
            return;
183
        }
184
185
        throw InvalidPostContent::noContentSet();
186
    }
187
}
188