Skip to content

Instantly share code, notes, and snippets.

@diaspar
Last active April 12, 2018 17:35
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 diaspar/62a87018ac8c4845e59946e33e0fd1ea to your computer and use it in GitHub Desktop.
Save diaspar/62a87018ac8c4845e59946e33e0fd1ea to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
use App\Contracts\Developers\DevelopersServiceContract;
use Symfony\Component\HttpFoundation\Response;
use BotMan\Drivers\Slack\SlackDriver;
use Exception;
class SendMessageToDevelopers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'jtalent:send-message';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send Message to Developer(s)';
/**
* @var DevelopersServiceContract
*/
protected $_service;
/**
* Create a new command instance
*
* @param DevelopersServiceContract $service
*/
public function __construct(DevelopersServiceContract $service)
{
$this->_service = $service;
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->info('Starting process to send Message to Developer(s)!');
$name = $this->ask('Enter Developer First and Last Name (or enter "all")');
// Todo: Refactor to support more cases and avoid more conditionals
if ($name === 'all') {
$this->messageToAllActiveDevelopers();
} else {
$this->messageToSingleDeveloper($name);
}
$this->info('Process finished!');
}
/**
* Message all active developers
*
* @return void
*/
protected function messageToAllActiveDevelopers()
{
// Get developers
$developers = $this->getActiveDevelopers();
// Some info before starting
$this->info('Sending message to ' . $developers->count() . ' active developers');
// Send Message using Slack
// Todo: refactor needed to make this extensible, Slack harcoded for now)
$developers->each(function ($developer) {
$slackUsername = $this->formatSlackUsername($developer->first_name, $developer->last_name);
$message = $this->getMessage();
$response = $this->sendSlackMessage($slackUsername, $message);
$this->outputResponse($response, $slackUsername);
});
}
/**
* Message single developer
*
* @param String $name
* @throws Exception
*/
protected function messageToSingleDeveloper(String $name)
{
// Review entered name
$names = explode(" ", $name);
if (count($names) < 2) {
throw new Exception('Please enter First name and Last name');
}
// Some info before starting
$this->info('Sending message to 1 developer');
// Send Message using Slack
// Todo: refactor needed to make this extensible, Slack harcoded for now)
$slackUsername = $this->formatSlackUsername($names[0], $names[1]);
$message = $this->getMessage();
$response = $this->sendSlackMessage($slackUsername, $message);
$this->outputResponse($response, $slackUsername);
}
/**
* Get Active developers
*
* @return mixed
*/
protected function getActiveDevelopers()
{
return $this->_service->get(['active' => 1], '');
}
/**
* Format a regular name into an Slack Username
*
* @param string $firstName
* @param string $lastName
* @return string
*/
protected function formatSlackUsername(String $firstName, String $lastName)
{
return '@'.strtolower($firstName.'.'.$lastName);
}
/**
* Get Message to send to Slack
*
* @return string
*/
protected function getMessage()
{
$GoogleFormUrl= 'https://docs.google.com/forms/d/1-nVwS-zc-tNEeeoRYiJlk-q2kEP8o-6IqWdYYq-hF-Y';
$message = 'Hi there, don’t forget to fill out the following form: ' . $GoogleFormUrl;
return $message;
}
/**
* Send Slack Message
*
* @param string $slackUsername
* @param string $message
* @return Response
*/
protected function sendSlackMessage(String $slackUsername, String $message)
{
// Load the driver(s) you want to use
DriverManager::loadDriver(SlackDriver::class);
// Create an instance with JobsityBot Token
$botman = BotManFactory::create(config('services'));
// Sending to Slack
return $botman->say($message, $slackUsername, SlackDriver::class);
}
/**
* Output response to console
*
* @param Response $response
* @param String $slackUsername
* @return void
*/
protected function outputResponse(Response $response, String $slackUsername)
{
$responseAsObject = json_decode($response->getContent());
if ($responseAsObject->ok) {
$this->info($slackUsername.' ✓');
} else {
$this->error($slackUsername.': '.$responseAsObject->error);
}
}
}
@JorgeRecioJobsity
Copy link

JorgeRecioJobsity commented Apr 12, 2018

Lo que veo mal a nivel de lenguaje es que en PHP no es obligatoria la llamada al constructor de la clase padre ni que cuando se llame sea en la primera sentencia, es decir se puede escribir cualquier código antes de llamar al constructor de la clase padre o no llamarlo.
Los lenguajes Java, Kotlin, C#, C++, Typescript, ES6 (Class declarations) se aseguran de diferentes formas de que en un constructor de una clase siempre se llame primero al constructor de la clase padre. En los casos de Kotlin, C# y C++ la llamada al constructor de la clase padre se ubica a continuación del constructor que se está definiendo y luego el cuerpo del constructor. En Python sin embargo esto funciona como en PHP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment