Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AbstractCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Abstract card
*/
abstract class AbstractCard implements TeamsConnectorInterface
abstract class AbstractCard implements CardInterface
{
/**
* @var array
Expand Down
14 changes: 14 additions & 0 deletions src/CardInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Sebbmyr\Teams;

interface CardInterface
{

/**
* Returns message card array
*
* @return array
*/
public function getMessage();
}
4 changes: 2 additions & 2 deletions src/Cards/CustomCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Sebbmyr\Teams\Cards;

use Sebbmyr\Teams\TeamsConnectorInterface;
use Sebbmyr\Teams\CardInterface;

/**
* Sebbmyr\Teams\Cards\CustomCard
*/
class CustomCard implements TeamsConnectorInterface
class CustomCard implements CardInterface
{
/**
* Theme color
Expand Down
48 changes: 33 additions & 15 deletions src/TeamsConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,54 @@
/**
* Teams connector
*/
class TeamsConnector
class TeamsConnector implements TeamsConnectorInterface
{
private $webhookUrl;

public function __construct($webhookUrl)
public function __construct($webhookUrl = '')
{
$this->webhookUrl = $webhookUrl;
}

/**
* @param string $webhookUrl
* @return mixed
*/
public function setWebhookUrl($webhookUrl)
{
$this->webhookUrl = $webhookUrl;

return $this;
}

/**
* Sends card message as POST request
*
* @param TeamsConnectorInterface $card
* @param int $curlOptTimeout by default = 10
* @param int $curlOptConnectTimeout by default = 3
* @param CardInterface $card
* @param int $curlOptTimeout by default = 10
* @param int $curlOptConnectTimeout by default = 3
* @throws Exception
*/
public function send(TeamsConnectorInterface $card, $curlOptTimeout = 10, $curlOptConnectTimeout = 3)
public function send(CardInterface $card, $curlOptTimeout = 10, $curlOptConnectTimeout = 3)
{
if ($this->webhookUrl === '') {
throw new \Exception('No webhook url has been set');
}

$json = json_encode($card->getMessage());

$ch = curl_init($this->webhookUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $curlOptTimeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curlOptConnectTimeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->webhookUrl,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $json,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $curlOptTimeout,
CURLOPT_CONNECTTIMEOUT => $curlOptConnectTimeout,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Content-Length: ' . strlen($json),
]
]);

$result = curl_exec($ch);
Expand Down
10 changes: 3 additions & 7 deletions src/TeamsConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

interface TeamsConnectorInterface
{
public function setWebhookUrl($webhookUrl);

/**
* Returns message card array
*
* @return array
*/
public function getMessage();
}
public function send(CardInterface $card, $curlOptTimeout = 10, $curlOptConnectTimeout = 3);
}