1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Vendors\StackOverflow; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Watcher\Communicator\SlackCommunicator; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class StackOverflowSlackCommunicator |
10
|
|
|
* |
11
|
|
|
* @package Coco\SourceWatcher\Vendors\StackOverflow |
12
|
|
|
*/ |
13
|
|
|
class StackOverflowSlackCommunicator extends SlackCommunicator |
14
|
|
|
{ |
15
|
|
|
private array $dataArray; |
16
|
|
|
|
17
|
|
|
public static string $BLOCKS_INDEX = "blocks"; |
18
|
|
|
|
19
|
|
|
public function __construct ( array $listOfJobs ) |
20
|
|
|
{ |
21
|
|
|
parent::__construct(); |
22
|
|
|
|
23
|
|
|
$this->buildDataArray( $listOfJobs ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function buildDataArray ( array $listOfJobs ) : void |
27
|
|
|
{ |
28
|
|
|
$this->dataArray = []; |
29
|
|
|
|
30
|
|
|
foreach ( $listOfJobs as $currentJob ) { |
31
|
|
|
$currentArray = [ StackOverflowSlackCommunicator::$BLOCKS_INDEX => [] ]; |
32
|
|
|
|
33
|
|
|
array_push( $currentArray[StackOverflowSlackCommunicator::$BLOCKS_INDEX], [ "type" => "divider" ] ); |
34
|
|
|
|
35
|
|
|
$currentSection = [ "type" => "section", "text" => null, "accessory" => null ]; |
36
|
|
|
|
37
|
|
|
$text = "*" . $currentJob->getTitle() . "*" . PHP_EOL; |
38
|
|
|
$text .= $currentJob->getCompany() . " | " . $currentJob->getLocation() . PHP_EOL; |
39
|
|
|
$text .= "<" . $currentJob->getRefinedUrl() . ">"; |
40
|
|
|
$currentSection["text"] = [ "type" => "mrkdwn", "text" => $text ]; |
41
|
|
|
|
42
|
|
|
$currentSection["accessory"] = [ |
43
|
|
|
"type" => "image", |
44
|
|
|
"image_url" => $currentJob->getLogo(), |
45
|
|
|
"alt_text" => $currentJob->getCompany() |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
array_push( $currentArray[StackOverflowSlackCommunicator::$BLOCKS_INDEX], $currentSection ); |
49
|
|
|
|
50
|
|
|
array_push( $currentArray[StackOverflowSlackCommunicator::$BLOCKS_INDEX], [ "type" => "divider" ] ); |
51
|
|
|
|
52
|
|
|
array_push( $this->dataArray, $currentArray ); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool|string |
58
|
|
|
*/ |
59
|
|
|
public function send () |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
foreach ( $this->dataArray as $currentSlackMessageBlock ) { |
63
|
|
|
$this->data = json_encode( $currentSlackMessageBlock ); |
64
|
|
|
|
65
|
|
|
parent::send(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} catch ( Exception $e ) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|