Passed
Push — master ( d151aa...1c6332 )
by Jean Paul
01:43
created

StackOverflowSlackCommunicator::buildDataArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
rs 9.8333
cc 2
nc 2
nop 1
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
 * @package Coco\SourceWatcher\Vendors\StackOverflow
11
 */
12
class StackOverflowSlackCommunicator extends SlackCommunicator
13
{
14
    /**
15
     * @var array
16
     */
17
    private array $dataArray;
18
19
    /**
20
     * StackOverflowSlackCommunicator constructor.
21
     * @param array $listOfJobs
22
     */
23
    public function __construct ( array $listOfJobs )
24
    {
25
        parent::__construct();
26
27
        $this->buildDataArray( $listOfJobs );
28
    }
29
30
    /**
31
     * @param array $listOfJobs
32
     */
33
    private function buildDataArray ( array $listOfJobs ) : void
34
    {
35
        $this->dataArray = array();
36
37
        foreach ( $listOfJobs as $currentJob ) {
38
            $currentArray = array( "blocks" => array() );
39
40
            array_push( $currentArray["blocks"], array( "type" => "divider" ) );
41
42
            $currentSection = array( "type" => "section", "text" => null, "accessory" => null );
43
44
            $text = "*" . $currentJob->getTitle() . "*" . PHP_EOL;
45
            $text .= $currentJob->getCompany() . " | " . $currentJob->getLocation() . PHP_EOL;
46
            $text .= "<" . $currentJob->getRefinedUrl() . ">";
47
            $currentSection["text"] = array( "type" => "mrkdwn", "text" => $text );
48
49
            $currentSection["accessory"] = array( "type" => "image", "image_url" => $currentJob->getLogo(), "alt_text" => $currentJob->getCompany() );
50
51
            array_push( $currentArray["blocks"], $currentSection );
52
53
            array_push( $currentArray["blocks"], array( "type" => "divider" ) );
54
55
            array_push( $this->dataArray, $currentArray );
56
        }
57
    }
58
59
    /**
60
     * @return bool|string|void
61
     * @throws Exception
62
     */
63
    public function send ()
64
    {
65
        foreach ( $this->dataArray as $currentSlackMessageBlock ) {
66
            $this->data = json_encode( $currentSlackMessageBlock );
67
            parent::send();
68
        }
69
    }
70
}
71