1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Andreas Heigl<[email protected]> |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
5
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
6
|
|
|
* in the Software without restriction, including without limitation the rights |
7
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
8
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
9
|
|
|
* furnished to do so, subject to the following conditions: |
10
|
|
|
* The above copyright notice and this permission notice shall be included in |
11
|
|
|
* all copies or substantial portions of the Software. |
12
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
13
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
14
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
15
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
16
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
17
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18
|
|
|
* THE SOFTWARE. |
19
|
|
|
* |
20
|
|
|
* @author Andreas Heigl<[email protected]> |
21
|
|
|
* @copyright Andreas Heigl |
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
23
|
|
|
* @since 22.09.2016 |
24
|
|
|
* @link http://github.com/heiglandreas/callingallpapers |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Callingallpapers\Notification; |
28
|
|
|
|
29
|
|
|
use Callingallpapers\Entity\Cfp; |
30
|
|
|
use GuzzleHttp\Client; |
31
|
|
|
use GuzzleHttp\Psr7\Request; |
32
|
|
|
|
33
|
|
|
class TwitterNotifier implements NotificationInterface |
34
|
|
|
{ |
35
|
|
|
protected $client; |
36
|
|
|
|
37
|
|
|
public function __construct(Client $client) |
38
|
|
|
{ |
39
|
|
|
$this->client = $client; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function notify(Cfp $cfp) |
43
|
|
|
{ |
44
|
|
|
$name = $this->shortenName($cfp->conferenceName); |
45
|
|
|
$uri = $this->shortenUri($cfp->uri); |
46
|
|
|
|
47
|
|
|
$notificationString = sprintf( |
48
|
|
|
'24 hours until the CfP for "%1$s" closes: %2$s', |
49
|
|
|
$name, |
50
|
|
|
$uri |
51
|
|
|
); |
52
|
|
|
$request = new Request('POST', |
53
|
|
|
'statuses/update.json?' . $this->formUrlEncode([ |
54
|
|
|
'status' => $notificationString, |
55
|
|
|
'lat' => $cfp->latitude, |
56
|
|
|
'long' => $cfp->longitude, |
57
|
|
|
'display_coordinates' => 'true', |
58
|
|
|
])); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$this->client->send($request); |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
// |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function shortenName($name) |
69
|
|
|
{ |
70
|
|
|
if (strlen($name) < 70) { |
71
|
|
|
return $name; |
72
|
|
|
} |
73
|
|
|
return substr($name, 0, 69) . '…'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function shortenUri($uri) |
77
|
|
|
{ |
78
|
|
|
return $uri; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function formUrlEncode(array $stuff) |
82
|
|
|
{ |
83
|
|
|
return http_build_query($stuff); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|