Skip to content

Instantly share code, notes, and snippets.

@gbiorczyk
Last active November 15, 2023 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbiorczyk/1df2697d6030a12964d980bb7d135430 to your computer and use it in GitHub Desktop.
Save gbiorczyk/1df2697d6030a12964d980bb7d135430 to your computer and use it in GitHub Desktop.
Maszyna losująca
<?php
class LotterySmsSender {
const SMS_API_URL = 'https://api.smslabs.net.pl/apiSms/sendSms';
const SMS_API_APP_KEY = '';
const SMS_API_SECRET_KEY = '';
const SMS_MESSAGE = 'Ho, Ho, Ho! Losowanie - to sekretna wiadomosc dla: %1$s. W tym roku jestes Mikolajem dla: %2$s. Pozdrawiam - maszyna losujaca z Laponii!';
private array $items;
public function __construct( array $items ) {
$this->items = $items;
}
public function make_lottery(): void {
$gift_from = array_keys( $this->items );
$gift_to = array_keys( $this->items );
$from_phones = array_column( $this->items, 'phone' );
$disabled_bonds = array_column( $this->items, 'disabled' );
for ( $i = 0; $i < 10; $i++ ) {
shuffle( $gift_to );
}
while ( ! $this->check_invalid_lottery( $gift_from, $gift_to, $disabled_bonds ) ) {
shuffle( $gift_to );
}
foreach ( $gift_from as $recipient_index => $recipient_name ) {
$this->send_sms_alert(
$from_phones[ $recipient_index ],
sprintf( self::SMS_MESSAGE, strtoupper( $recipient_name ), strtoupper( $gift_to[ $recipient_index ] ) )
);
}
}
private function check_invalid_lottery( array $gift_from, array $gift_to, array $disabled_bonds ): bool {
foreach ( $gift_from as $recipient_index => $recipient_name ) {
if ( $recipient_name === $gift_to[ $recipient_index ] ) {
return false;
} elseif ( in_array( $gift_to[ $recipient_index ], $disabled_bonds[ $recipient_index ] ) ) {
return false;
}
}
return true;
}
private function send_sms_alert( string $phone_number = null, string $alert_message = null ): void {
if ( ( $phone_number === null ) || ( $alert_message === null ) ) {
return;
}
$curl = curl_init( self::SMS_API_URL );
$data = [
'flash' => '0',
'expiration' => '0',
'phone_number' => sprintf( '+48%s', $phone_number ),
'sender_id' => 'SMS TEST',
'message' => $alert_message,
];
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, http_build_query( $data ) );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $curl, CURLOPT_USERPWD, self::SMS_API_APP_KEY . ':' . self::SMS_API_SECRET_KEY );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $curl );
$code = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
curl_close( $curl );
if ( $code !== 200 ) {
echo '<pre>';
print_r( [ $code, $response ] );
echo '</pre>';
exit;
}
}
}
$items = [
'Mateusz' => [
'phone' => '000000000',
'disabled' => [ 'Mateusz' ],
],
];
( new LotterySmsSender( $items ) )->make_lottery();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment