|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Platforms; |
|
4
|
|
|
|
|
5
|
|
|
use App\Ad; |
|
6
|
|
|
use App\Platform; |
|
7
|
|
|
use App\Platforms\Traits\ImageHelper; |
|
8
|
|
|
use App\Platforms\Traits\GetFormValidationRules; |
|
9
|
|
|
use Illuminate\Support\Facades\Storage; |
|
10
|
|
|
|
|
11
|
|
|
class Twitter implements PlatformInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use GetFormValidationRules; |
|
14
|
|
|
use ImageHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @url https://developer.twitter.com/en/docs/media/upload-media/overview |
|
18
|
|
|
*/ |
|
19
|
|
|
const MAX_IMAGE_UPLOAD_SIZE = 5242880; |
|
20
|
|
|
|
|
21
|
|
|
const FORM_FIELDS = [ |
|
22
|
|
|
[ |
|
23
|
|
|
'label' => 'Consumer key', |
|
24
|
|
|
'name' => 'consumer_key', |
|
25
|
|
|
'type' => 'text', |
|
26
|
|
|
'id' => 'consumer_key', |
|
27
|
|
|
'validation' => '', |
|
28
|
|
|
], |
|
29
|
|
|
[ |
|
30
|
|
|
'label' => 'Consumer secret', |
|
31
|
|
|
'name' => 'consumer_secret', |
|
32
|
|
|
'type' => 'text', |
|
33
|
|
|
'id' => 'consumer_secret', |
|
34
|
|
|
'validation' => '', |
|
35
|
|
|
], |
|
36
|
|
|
[ |
|
37
|
|
|
'label' => 'Access token', |
|
38
|
|
|
'name' => 'token', |
|
39
|
|
|
'type' => 'text', |
|
40
|
|
|
'id' => 'access_token', |
|
41
|
|
|
'validation' => '', |
|
42
|
|
|
], |
|
43
|
|
|
[ |
|
44
|
|
|
'label' => 'Access token secret', |
|
45
|
|
|
'name' => 'secret', |
|
46
|
|
|
'type' => 'text', |
|
47
|
|
|
'id' => 'access_token_secret', |
|
48
|
|
|
'validation' => '', |
|
49
|
|
|
], |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Platform $platform |
|
54
|
|
|
* @return \Thujohn\Twitter\Twitter |
|
55
|
|
|
*/ |
|
56
|
|
|
public function authenticate(Platform $platform): \Thujohn\Twitter\Twitter |
|
57
|
|
|
{ |
|
58
|
|
|
return \Thujohn\Twitter\Facades\Twitter::reconfig($platform->config); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getFormFields(): array |
|
65
|
|
|
{ |
|
66
|
|
|
return self::FORM_FIELDS; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param Ad $ad |
|
71
|
|
|
* @param Platform $platform |
|
72
|
|
|
* @return int |
|
73
|
|
|
* @throws \Exception |
|
74
|
|
|
*/ |
|
75
|
|
|
public function publish(Ad $ad, Platform $platform): int |
|
76
|
|
|
{ |
|
77
|
|
|
$twitter = $this->authenticate($platform); |
|
78
|
|
|
|
|
79
|
|
|
$this->imageUrlValidation($ad->img_url); |
|
80
|
|
|
|
|
81
|
|
|
$file = file_get_contents($ad->img_url); |
|
82
|
|
|
$name = $this->getUniqueNameFromUrl($ad->img_url); |
|
83
|
|
|
Storage::put($name, $file); |
|
84
|
|
|
$this->imageSizeValidation($name); |
|
85
|
|
|
Storage::delete($name); |
|
86
|
|
|
|
|
87
|
|
|
$uploadedMedia = $twitter->uploadMedia(['media' => $file]); |
|
88
|
|
|
|
|
89
|
|
|
$response = $twitter->postTweet( |
|
90
|
|
|
['status' => $ad->formattedString, 'media_ids' => $uploadedMedia->media_id_string] |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
return $response->id; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param Ad $ad |
|
98
|
|
|
* @param Platform $platform |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
|
|
public function unpublish(Ad $ad, Platform $platform) |
|
102
|
|
|
{ |
|
103
|
|
|
$twitter = $this->authenticate($platform); |
|
104
|
|
|
|
|
105
|
|
|
$tweetId = $ad->platforms()->where('platform_id', $platform->id)->first()->pivot->publication_item_id; |
|
106
|
|
|
|
|
107
|
|
|
return $twitter->destroyTweet($tweetId); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|