From 275f5d5f748c2795192a97806eea88076f77177d Mon Sep 17 00:00:00 2001 From: imclaughlin Date: Thu, 19 Jan 2023 13:19:08 -0600 Subject: [PATCH 1/5] adding slack command --- src/EchoSlackCommand.php | 31 +++++++++++++++++++++++++++++++ src/main.php | 1 + 2 files changed, 32 insertions(+) create mode 100644 src/EchoSlackCommand.php diff --git a/src/EchoSlackCommand.php b/src/EchoSlackCommand.php new file mode 100644 index 0000000..f55e27d --- /dev/null +++ b/src/EchoSlackCommand.php @@ -0,0 +1,31 @@ +setDescription("Executes a command from the list chosen by the user"); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $helper = $this->getHelper('question'); + + $question = new Question('What would you like to do?', 'response'); + + $response = $helper->ask($input, $output, $question); + + $output->writeln("I WANT TO $response"); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/main.php b/src/main.php index 868edee..df54445 100644 --- a/src/main.php +++ b/src/main.php @@ -9,6 +9,7 @@ $application = new Application("PHP Symfony CLI", "v0.0.1"); $application->add(new EchoNameCommand()); +$application->add(new EchoSlackCommand()); try { $application->run(); From 229c5a885987270a85f83ae78809c15966fb29bc Mon Sep 17 00:00:00 2001 From: imclaughlin Date: Thu, 26 Jan 2023 10:19:15 -0600 Subject: [PATCH 2/5] add template, list template, update template, delete template, list user, show message functions --- composer.json | 2 +- composer.lock | 2 +- src/EchoSlackCommand.php | 31 ----- src/SlackCommand.php | 226 ++++++++++++++++++++++++++++++++ src/data/messages.json.example | 7 - src/data/templates.json.example | 6 - src/data/users.json.example | 8 -- src/main.php | 2 +- 8 files changed, 229 insertions(+), 55 deletions(-) delete mode 100644 src/EchoSlackCommand.php create mode 100644 src/SlackCommand.php delete mode 100644 src/data/messages.json.example delete mode 100644 src/data/templates.json.example delete mode 100644 src/data/users.json.example diff --git a/composer.json b/composer.json index ff94a8b..7c967e5 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "php": "^8.1", "symfony/console": "^6.2", "symfony/finder": "*", - "symfony/filesystem": "*", + "symfony/filesystem": "^6.2", "symfony/process": "*" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 2b4f557..e6280b4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f8f1d8c05344edd6674fbfa8f92e4640", + "content-hash": "bbc2d909d42900896f3fa14ef861fac4", "packages": [ { "name": "psr/container", diff --git a/src/EchoSlackCommand.php b/src/EchoSlackCommand.php deleted file mode 100644 index f55e27d..0000000 --- a/src/EchoSlackCommand.php +++ /dev/null @@ -1,31 +0,0 @@ -setDescription("Executes a command from the list chosen by the user"); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $helper = $this->getHelper('question'); - - $question = new Question('What would you like to do?', 'response'); - - $response = $helper->ask($input, $output, $question); - - $output->writeln("I WANT TO $response"); - - return Command::SUCCESS; - } -} \ No newline at end of file diff --git a/src/SlackCommand.php b/src/SlackCommand.php new file mode 100644 index 0000000..948d6a2 --- /dev/null +++ b/src/SlackCommand.php @@ -0,0 +1,226 @@ +setDescription("Asks user to choose from the actions list."); + } + + + protected function execute(InputInterface $input, OutputInterface $output): int + { + + $output->writeln([ + '====**** SLACK MESSAGE SENDER ****====', + '==========================================', + '', + ]); + + $output->writeln([ + 'What would you like to do?', + '1. Send a message', + '2. List templates', + '3. Add a template', + '4. Update a template', + '5. Delete a template', + '6. List users', + '7. Add a user', + '8. Show sent messages', + '9. Exit', + '' + ]); + + $helper = $this->getHelper('question'); + + $question = new Question('Please enter a number from the above options.', 'None'); + + $choice = $helper->ask($input, $output, $question); + + switch ($choice) { + case "1": + echo "\nSEND A MESSAGE\n\n"; + break; + case "2": + echo "\nLIST TEMPLATES\n\n"; + $this->listTemplates(); + break; + case "3": + echo "\nADD A TEMPLATE\n\n"; + $this->addTemplate($input, $output); + break; + case "4": + echo "\nUPDATE A TEMPLATE\n\n"; + $this->updateTemplate($input, $output); + break; + case "5": + echo "\nDELETE A TEMPLATE\n\n"; + $this->deleteTemplate($input, $output); + break; + case "6": + echo "\nLIST USERS\n\n"; + $this->listUsers(); + break; + case "7": + echo "\nADD A USER\n\n"; + break; + case "8": + echo "\nSHOW SENT MESSAGES\n\n"; + $this->sentMessages(); + break; + case "9": + echo "\nExit\n\n"; + break; + default: + echo 'no response chosen'; + break; + } + + return Command::SUCCESS; + } + + + private function listTemplates() + { + $finder = new Finder(); + + $finder->files()->in(__DIR__)->path('data')->name("templates.json"); + + if ($finder->hasResults()) { + foreach ($finder as $file) { + $contents = $file->getContents(); + var_dump($contents); + } + } + + } + + private function addTemplate(InputInterface $input, OutputInterface $output): int + { + $filesystem = new Filesystem(); + $path = "./src/data/templates.json"; + $jsonString = file_get_contents($path); + $templates = json_decode($jsonString, true); + + echo "Available variables:\n* {name}\n* {username}\n* {displayName}\n\n"; + + $helper = $this->getHelper('question'); + + $question = new Question('Enter your new template and press enter to save:', ''); + $newTemplate = $helper->ask($input, $output, $question); + + $lastKey = array_key_last($templates); + $newId = $lastKey + 2; + + $jsonArray = json_encode(array( + "id" => strval($newId), + "message" => $newTemplate + )); + + $filesystem->appendToFile("./src/data/templates.json", $jsonArray, JSON_PRETTY_PRINT); + + return Command::SUCCESS; + + } + + + private function updateTemplate(InputInterface $input, OutputInterface $output): int + { + $path = "./src/data/templates.json"; + $jsonString = file_get_contents($path); + $templates = json_decode($jsonString, true); + + $helper = $this->getHelper('question'); + + $whichTemplate = new Question('Which template would you like to update?', ''); + $selectedTemplate = $helper->ask($input, $output, $whichTemplate); + + $helper2 = $this->getHelper('question'); + $updatedTemplate = new Question('Enter your new template and press enter to save:', ''); + $newTemplate = $helper2->ask($input, $output, $updatedTemplate); + + foreach($templates as &$e) { + if ($e['id'] === $selectedTemplate) { + $e['message'] = $newTemplate; + } + } + $finalArray = json_encode($templates, JSON_PRETTY_PRINT); + $filesystem = new Filesystem(); + $filesystem->dumpFile($path, $finalArray); + + + return Command::SUCCESS; + } + + private function deleteTemplate(InputInterface $input, OutputInterface $output): int { + + $path = "./src/data/templates.json"; + $jsonString = file_get_contents($path); + $templates = json_decode($jsonString, true); + + $helper = $this->getHelper('question'); + + $this->listTemplates(); + + $whichTemplate = new Question('Please type the id of the template you want to delete.', ""); + $selectedTemplate = $helper->ask($input, $output, $whichTemplate); + + foreach($templates as $key => &$value) { + if ($value['id'] === $selectedTemplate) { + unset($templates[$key]); + } + } + + + $finalArray = json_encode(array_values($templates), JSON_PRETTY_PRINT); + $filesystem = new Filesystem(); + $filesystem->dumpFile($path, $finalArray); + + return Command::SUCCESS; + } + + private function listUsers() + { + + $finder = new Finder(); + + $finder->files()->in(__DIR__)->path('data')->name("users.json"); + + if ($finder->hasResults()) { + foreach ($finder as $file) { + $contents = $file->getContents(); + var_dump($contents); + } + } + } + + private function sentMessages() + { + $finder = new Finder(); + + $finder->files()->in(__DIR__)->path('data')->name("messages.json"); + + if ($finder->hasResults()) { + foreach ($finder as $file) { + $contents = $file->getContents(); + var_dump($contents); + } + } + + } + +} \ No newline at end of file diff --git a/src/data/messages.json.example b/src/data/messages.json.example deleted file mode 100644 index 1482bca..0000000 --- a/src/data/messages.json.example +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "id": 1, - "message": "Hello, World", - "date": "Mon Jan 16 2023 17:39:43 GMT-0600 (CST)" - } -] diff --git a/src/data/templates.json.example b/src/data/templates.json.example deleted file mode 100644 index 2a6a749..0000000 --- a/src/data/templates.json.example +++ /dev/null @@ -1,6 +0,0 @@ -[ - { - "id": 1, - "message": "Hello, {displayName}" - } -] diff --git a/src/data/users.json.example b/src/data/users.json.example deleted file mode 100644 index f41184f..0000000 --- a/src/data/users.json.example +++ /dev/null @@ -1,8 +0,0 @@ -[ - { - "name": "Slacky Slackbot", - "userId": "U01ABCDWXYZ", - "username": "slackbot", - "displayName": "Slackbot" - } -] diff --git a/src/main.php b/src/main.php index df54445..b0ad619 100644 --- a/src/main.php +++ b/src/main.php @@ -9,7 +9,7 @@ $application = new Application("PHP Symfony CLI", "v0.0.1"); $application->add(new EchoNameCommand()); -$application->add(new EchoSlackCommand()); +$application->add(new SlackCommand()); try { $application->run(); From 28184f61516a384da8f0b81232543c29ddead85e Mon Sep 17 00:00:00 2001 From: imclaughlin Date: Thu, 26 Jan 2023 20:45:45 -0600 Subject: [PATCH 3/5] updating sendMessage function --- src/SlackCommand.php | 223 ++++++++++++++++++++++++++++++++----------- 1 file changed, 168 insertions(+), 55 deletions(-) diff --git a/src/SlackCommand.php b/src/SlackCommand.php index 948d6a2..3fd0c70 100644 --- a/src/SlackCommand.php +++ b/src/SlackCommand.php @@ -10,6 +10,8 @@ use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\Process; class SlackCommand extends Command{ @@ -31,69 +33,145 @@ protected function execute(InputInterface $input, OutputInterface $output): int '', ]); - $output->writeln([ - 'What would you like to do?', - '1. Send a message', - '2. List templates', - '3. Add a template', - '4. Update a template', - '5. Delete a template', - '6. List users', - '7. Add a user', - '8. Show sent messages', - '9. Exit', - '' - ]); + + $keepGoing = true; + + while ($keepGoing) { + $output->writeln([ + 'What would you like to do?', + '1. Send a message', + '2. List templates', + '3. Add a template', + '4. Update a template', + '5. Delete a template', + '6. List users', + '7. Add a user', + '8. Show sent messages', + '9. Exit', + '' + ]); + + $helper = $this->getHelper('question'); + + $question = new Question("Please enter a number from the above options. ", 'None'); + + $choice = $helper->ask($input, $output, $question); + + + switch ($choice) { + case "1": + echo "\nSEND A MESSAGE\n\n"; + $this->sendMessage($input, $output); + break; + case "2": + echo "\nLIST TEMPLATES\n\n"; + $this->listTemplates(); + break; + case "3": + echo "\nADD A TEMPLATE\n\n"; + $this->addTemplate($input, $output); + break; + case "4": + echo "\nUPDATE A TEMPLATE\n\n"; + $this->updateTemplate($input, $output); + break; + case "5": + echo "\nDELETE A TEMPLATE\n\n"; + $this->deleteTemplate($input, $output); + break; + case "6": + echo "\nLIST USERS\n\n"; + $this->listUsers(); + break; + case "7": + echo "\nADD A USER\n\n"; + $this->addUser($input, $output); + break; + case "8": + echo "\nSHOW SENT MESSAGES\n\n"; + $this->sentMessages(); + break; + case "9": + echo "Have a nice day!"; + $keepGoing = false; + break; + default: + echo "\nOPE! You need to select a number silly!\n\n"; + break; + } + } + + + return Command::SUCCESS; + } + + private function sendMessage(InputInterface $input, OutputInterface $output): int { + + $path1 = "./src/data/templates.json"; + $path2 = "./src/data/users.json";; + $jsonString1 = file_get_contents($path1); + $jsonString2 = file_get_contents($path2); + $templates = json_decode($jsonString1, true); + $users = json_decode($jsonString2, true); $helper = $this->getHelper('question'); - $question = new Question('Please enter a number from the above options.', 'None'); - - $choice = $helper->ask($input, $output, $question); - - switch ($choice) { - case "1": - echo "\nSEND A MESSAGE\n\n"; - break; - case "2": - echo "\nLIST TEMPLATES\n\n"; - $this->listTemplates(); - break; - case "3": - echo "\nADD A TEMPLATE\n\n"; - $this->addTemplate($input, $output); - break; - case "4": - echo "\nUPDATE A TEMPLATE\n\n"; - $this->updateTemplate($input, $output); - break; - case "5": - echo "\nDELETE A TEMPLATE\n\n"; - $this->deleteTemplate($input, $output); - break; - case "6": - echo "\nLIST USERS\n\n"; - $this->listUsers(); - break; - case "7": - echo "\nADD A USER\n\n"; - break; - case "8": - echo "\nSHOW SENT MESSAGES\n\n"; - $this->sentMessages(); - break; - case "9": - echo "\nExit\n\n"; - break; - default: - echo 'no response chosen'; - break; + $whichTemplate = new Question("What template? \n", '1'); + $num = 1; + foreach($templates as $e) { + echo strval($num) . "." . $e['message'] . "\n"; + $num++; + } + $selectedTemplate = $helper->ask($input, $output, $whichTemplate); + + foreach($templates as &$e) { + if ($e['id'] === $selectedTemplate) { + $message = $e['message']; + } + } + + $whichUser = new Question("\n\nWhat user (Please type their name)? \n", 'name'); + $num2 = 1; + foreach($users as $e) { + echo strval($num2) . "." . $e['name'] . "\n"; + $num2++; + } + $selectedUser = $helper->ask($input, $output, $whichUser); + + + foreach($users as &$e) { + if ($e['name'] === $selectedUser) { + $user = $e['name']; + } + } + + echo "Sending to " . $user . ":\n\n"; + $message = str_replace("{name}", $user, $message); + echo $message . "\n\n"; + $sendMessage = new Question("Enter 'yes' to send.", 'No'); + $choice = $helper->ask($input, $output, $sendMessage); + + $command = + 'curl -X POST --data-urlencode + \"payload={\"channel\": \"#accelerated-engineer-program\", + \"username\": \"yourname\", + \"text\": \"textChange.\", + \"icon_emoji\": \":ghost:\"}\"https://hooks.slack.com/services/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'; + $command = str_replace("yourname", $user, $command); + $finalChange = str_replace("textChange", $message, $command); + var_dump($finalChange); + + $process = new Process($command); + + if ($choice === 'yes') { + $process->run(); + } else { + echo "You chose not to send the message."; } return Command::SUCCESS; } - private function listTemplates() { $finder = new Finder(); @@ -208,6 +286,41 @@ private function listUsers() } } + private function addUser(InputInterface $input, OutputInterface $output): int { + + $filesystem = new Filesystem(); + $path = "./src/data/users.json"; + $jsonString = file_get_contents($path); + $users = json_decode($jsonString, true); + + $helper = $this->getHelper('question'); + + $question = new Question('Enter the user\'s name: ', 'name'); + echo "\n"; + $name = $helper->ask($input, $output, $question); + $question2 = new Question('Enter the user\'s ID: ', 'userID'); + echo "\n"; + $userID = $helper->ask($input, $output, $question2); + $question3 = new Question('Enter the user\'s username: ', 'username'); + echo "\n"; + $username = $helper->ask($input, $output, $question3); + $question4 = new Question('Enter the user\'s display name: ', 'display name'); + echo "\n"; + $displayName = $helper->ask($input, $output, $question4); + + $jsonArray = json_encode(array( + "name" => $name, + "userID" => $userID, + "username" => $username, + "displayName" => $displayName + )); + + $filesystem->appendToFile($path, $jsonArray, JSON_PRETTY_PRINT); + + return Command::SUCCESS; + } + + private function sentMessages() { $finder = new Finder(); From 7c064284eaa55d4dd70c926f92c018ca500dba67 Mon Sep 17 00:00:00 2001 From: imclaughlin Date: Fri, 27 Jan 2023 09:51:05 -0600 Subject: [PATCH 4/5] updating sendMessage function --- src/SlackCommand.php | 127 +++++++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 42 deletions(-) diff --git a/src/SlackCommand.php b/src/SlackCommand.php index 3fd0c70..42dd25c 100644 --- a/src/SlackCommand.php +++ b/src/SlackCommand.php @@ -12,6 +12,7 @@ use Symfony\Component\Filesystem\Path; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; +use Symfony\Component\Console\Helper\Table; class SlackCommand extends Command{ @@ -89,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int break; case "8": echo "\nSHOW SENT MESSAGES\n\n"; - $this->sentMessages(); + $this->sentMessages($input, $output); break; case "9": echo "Have a nice day!"; @@ -108,20 +109,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function sendMessage(InputInterface $input, OutputInterface $output): int { $path1 = "./src/data/templates.json"; - $path2 = "./src/data/users.json";; + $path2 = "./src/data/users.json"; + $path3 = "./src/data/messages.json"; $jsonString1 = file_get_contents($path1); $jsonString2 = file_get_contents($path2); + $jsonString3 = file_get_contents($path3); $templates = json_decode($jsonString1, true); $users = json_decode($jsonString2, true); + $sentMessages = json_decode($jsonString3, true); $helper = $this->getHelper('question'); + $this->listTemplates(); + $whichTemplate = new Question("What template? \n", '1'); - $num = 1; - foreach($templates as $e) { - echo strval($num) . "." . $e['message'] . "\n"; - $num++; - } $selectedTemplate = $helper->ask($input, $output, $whichTemplate); foreach($templates as &$e) { @@ -130,15 +131,11 @@ private function sendMessage(InputInterface $input, OutputInterface $output): in } } + $this->listUsers(); + $whichUser = new Question("\n\nWhat user (Please type their name)? \n", 'name'); - $num2 = 1; - foreach($users as $e) { - echo strval($num2) . "." . $e['name'] . "\n"; - $num2++; - } $selectedUser = $helper->ask($input, $output, $whichUser); - foreach($users as &$e) { if ($e['name'] === $selectedUser) { $user = $e['name']; @@ -151,17 +148,16 @@ private function sendMessage(InputInterface $input, OutputInterface $output): in $sendMessage = new Question("Enter 'yes' to send.", 'No'); $choice = $helper->ask($input, $output, $sendMessage); - $command = + $command = [ 'curl -X POST --data-urlencode \"payload={\"channel\": \"#accelerated-engineer-program\", \"username\": \"yourname\", \"text\": \"textChange.\", - \"icon_emoji\": \":ghost:\"}\"https://hooks.slack.com/services/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'; + \"icon_emoji\": \":ghost:\"}\"https://hooks.slack.com/services/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']; $command = str_replace("yourname", $user, $command); - $finalChange = str_replace("textChange", $message, $command); - var_dump($finalChange); + $finalCommand = str_replace("textChange", $message, $command); - $process = new Process($command); + $process = new Process($finalCommand); if ($choice === 'yes') { $process->run(); @@ -169,6 +165,23 @@ private function sendMessage(InputInterface $input, OutputInterface $output): in echo "You chose not to send the message."; } + $lastKey = array_key_last($sentMessages); + $newId = $lastKey + 2; + $date = date('d-m-y h:i:s'); + $jsonArray = [[ + "id" => strval($newId), + "message" => $message, + "date" => $date + ]]; + + $merge = array_merge($sentMessages, $jsonArray); + + $finalArray = json_encode(array_values($merge), JSON_PRETTY_PRINT); + + $filesystem = new Filesystem(); + $filesystem->dumpFile($path3, $finalArray); + + return Command::SUCCESS; } @@ -178,10 +191,15 @@ private function listTemplates() $finder->files()->in(__DIR__)->path('data')->name("templates.json"); + $num = 1; if ($finder->hasResults()) { foreach ($finder as $file) { $contents = $file->getContents(); - var_dump($contents); + $templates = json_decode($contents, true); + foreach($templates as $e) { + echo strval($num) . "." . $e['message'] . "\n"; + $num++; + } } } @@ -204,12 +222,16 @@ private function addTemplate(InputInterface $input, OutputInterface $output): in $lastKey = array_key_last($templates); $newId = $lastKey + 2; - $jsonArray = json_encode(array( + $jsonArray = [[ "id" => strval($newId), "message" => $newTemplate - )); + ]]; + + $merge = array_merge($templates, $jsonArray); + + $finalArray = json_encode(array_values($merge), JSON_PRETTY_PRINT); - $filesystem->appendToFile("./src/data/templates.json", $jsonArray, JSON_PRETTY_PRINT); + $filesystem->dumpFile($path, $finalArray); return Command::SUCCESS; @@ -224,7 +246,12 @@ private function updateTemplate(InputInterface $input, OutputInterface $output): $helper = $this->getHelper('question'); - $whichTemplate = new Question('Which template would you like to update?', ''); + $whichTemplate = new Question('Which template would you like to update?', '1'); + $num = 1; + foreach($templates as $e) { + echo strval($num) . "." . $e['message'] . "\n"; + $num++; + } $selectedTemplate = $helper->ask($input, $output, $whichTemplate); $helper2 = $this->getHelper('question'); @@ -252,9 +279,12 @@ private function deleteTemplate(InputInterface $input, OutputInterface $output): $helper = $this->getHelper('question'); - $this->listTemplates(); - - $whichTemplate = new Question('Please type the id of the template you want to delete.', ""); + $whichTemplate = new Question('Please type the id of the template you want to delete.', '1'); + $num = 1; + foreach($templates as $e) { + echo strval($num) . "." . $e['message'] . "\n"; + $num++; + } $selectedTemplate = $helper->ask($input, $output, $whichTemplate); foreach($templates as $key => &$value) { @@ -263,7 +293,6 @@ private function deleteTemplate(InputInterface $input, OutputInterface $output): } } - $finalArray = json_encode(array_values($templates), JSON_PRETTY_PRINT); $filesystem = new Filesystem(); $filesystem->dumpFile($path, $finalArray); @@ -278,11 +307,16 @@ private function listUsers() $finder->files()->in(__DIR__)->path('data')->name("users.json"); + $num = 1; if ($finder->hasResults()) { foreach ($finder as $file) { - $contents = $file->getContents(); - var_dump($contents); - } + $contents = $file->getContents(); + $users = json_decode($contents, true); + foreach($users as $e) { + echo strval($num) . "." . $e['name'] . "\n"; + $num++; + } + } } } @@ -308,31 +342,40 @@ private function addUser(InputInterface $input, OutputInterface $output): int { echo "\n"; $displayName = $helper->ask($input, $output, $question4); - $jsonArray = json_encode(array( + $jsonArray = [[ "name" => $name, "userID" => $userID, "username" => $username, "displayName" => $displayName - )); + ]]; - $filesystem->appendToFile($path, $jsonArray, JSON_PRETTY_PRINT); + $merge = array_merge($users, $jsonArray); + $finalArray = json_encode(array_values($merge), JSON_PRETTY_PRINT); + $filesystem->dumpFile($path, $finalArray); return Command::SUCCESS; } - private function sentMessages() + private function sentMessages(InputInterface $input, OutputInterface $output): int { - $finder = new Finder(); + $path = "./src/data/messages.json"; + $jsonString = file_get_contents($path); + $messages = json_decode($jsonString, true); + + var_dump($messages); + $table = new Table($output); + $table->setHeaders(['DATE', 'MESSAGE']); + foreach($messages as $e) { + var_dump($e); + $table-> setRows([ + [$e['date'], $e['message']] + ]); + }; - $finder->files()->in(__DIR__)->path('data')->name("messages.json"); + $table->render(); - if ($finder->hasResults()) { - foreach ($finder as $file) { - $contents = $file->getContents(); - var_dump($contents); - } - } + return Command::SUCCESS; } From 3263aec3585280a46f1cbcc9bb282a39e933fd01 Mon Sep 17 00:00:00 2001 From: imclaughlin Date: Fri, 27 Jan 2023 14:08:32 -0600 Subject: [PATCH 5/5] project ready for demo --- src/SlackCommand.php | 93 ++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/src/SlackCommand.php b/src/SlackCommand.php index 42dd25c..f57fa08 100644 --- a/src/SlackCommand.php +++ b/src/SlackCommand.php @@ -13,10 +13,10 @@ use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Formatter\OutputFormatterStyle; class SlackCommand extends Command{ - protected static $defaultName = 'slack'; protected function configure(): void @@ -27,10 +27,12 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { + $outputStyle = new OutputFormatterStyle('bright-blue'); + $output->getFormatter()->setStyle('fun', $outputStyle); $output->writeln([ - '====**** SLACK MESSAGE SENDER ****====', - '==========================================', + '====**** SLACK MESSAGE SENDER ****====', + '==========================================', '', ]); @@ -38,19 +40,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int $keepGoing = true; while ($keepGoing) { - $output->writeln([ - 'What would you like to do?', - '1. Send a message', - '2. List templates', - '3. Add a template', - '4. Update a template', - '5. Delete a template', - '6. List users', - '7. Add a user', - '8. Show sent messages', - '9. Exit', - '' - ]); + $output->writeln([' + What would you like to do?, + 1. Send a message + 2. List templates + 3. Add a template + 4. Update a template + 5. Delete a template + 6. List users + 7. Add a user + 8. Show sent messages + 9. Exit + ']); $helper = $this->getHelper('question'); @@ -61,43 +62,43 @@ protected function execute(InputInterface $input, OutputInterface $output): int switch ($choice) { case "1": - echo "\nSEND A MESSAGE\n\n"; + $output->writeln('SEND A MESSAGE'); $this->sendMessage($input, $output); break; case "2": - echo "\nLIST TEMPLATES\n\n"; - $this->listTemplates(); + $output->writeln('LIST TEMPLATES'); + $this->listTemplates($input, $output); break; case "3": - echo "\nADD A TEMPLATE\n\n"; + $output->writeln('ADD A TEMPLATE'); $this->addTemplate($input, $output); break; case "4": - echo "\nUPDATE A TEMPLATE\n\n"; + $output->writeln('UPDATE A TEMPLATE'); $this->updateTemplate($input, $output); break; case "5": - echo "\nDELETE A TEMPLATE\n\n"; + $output->writeln('DELETE A TEMPLATE'); $this->deleteTemplate($input, $output); break; case "6": - echo "\nLIST USERS\n\n"; + $output->writeln('LIST USERS'); $this->listUsers(); break; case "7": - echo "\nADD A USER\n\n"; + $output->writeln('ADD A USER'); $this->addUser($input, $output); break; case "8": - echo "\nSHOW SENT MESSAGES\n\n"; + $output->writeln('SHOW SENT MESSAGES'); $this->sentMessages($input, $output); break; case "9": - echo "Have a nice day!"; + $output->writeln('Have a nice day!'); $keepGoing = false; break; default: - echo "\nOPE! You need to select a number silly!\n\n"; + $output->writeln('OPE! You need to select a number silly!'); break; } } @@ -120,7 +121,7 @@ private function sendMessage(InputInterface $input, OutputInterface $output): in $helper = $this->getHelper('question'); - $this->listTemplates(); + $this->listTemplates($input, $output); $whichTemplate = new Question("What template? \n", '1'); $selectedTemplate = $helper->ask($input, $output, $whichTemplate); @@ -131,7 +132,7 @@ private function sendMessage(InputInterface $input, OutputInterface $output): in } } - $this->listUsers(); + $this->listUsers($input, $output); $whichUser = new Question("\n\nWhat user (Please type their name)? \n", 'name'); $selectedUser = $helper->ask($input, $output, $whichUser); @@ -167,7 +168,7 @@ private function sendMessage(InputInterface $input, OutputInterface $output): in $lastKey = array_key_last($sentMessages); $newId = $lastKey + 2; - $date = date('d-m-y h:i:s'); + $date = date("D M d Y H:i:s e"); $jsonArray = [[ "id" => strval($newId), "message" => $message, @@ -185,24 +186,26 @@ private function sendMessage(InputInterface $input, OutputInterface $output): in return Command::SUCCESS; } - private function listTemplates() + private function listTemplates(InputInterface $input, OutputInterface $output): int { $finder = new Finder(); - $finder->files()->in(__DIR__)->path('data')->name("templates.json"); + $outputStyle = new OutputFormatterStyle('bright-blue'); + $output->getFormatter()->setStyle('fun', $outputStyle); + $num = 1; if ($finder->hasResults()) { foreach ($finder as $file) { $contents = $file->getContents(); $templates = json_decode($contents, true); foreach($templates as $e) { - echo strval($num) . "." . $e['message'] . "\n"; + $output->writeln( strval($num) . "." . $e['message']); $num++; } } } - + return Command::SUCCESS; } private function addTemplate(InputInterface $input, OutputInterface $output): int @@ -212,7 +215,14 @@ private function addTemplate(InputInterface $input, OutputInterface $output): in $jsonString = file_get_contents($path); $templates = json_decode($jsonString, true); - echo "Available variables:\n* {name}\n* {username}\n* {displayName}\n\n"; + $outputStyle = new OutputFormatterStyle('bright-blue'); + $output->getFormatter()->setStyle('fun', $outputStyle); + + $output->writeln('Available variables:'); + $output->writeln('* {name}'); + $output->writeln('* {username}'); + $output->writeln('* {displayName}'); + echo "\n\n"; $helper = $this->getHelper('question'); @@ -363,15 +373,14 @@ private function sentMessages(InputInterface $input, OutputInterface $output): i $jsonString = file_get_contents($path); $messages = json_decode($jsonString, true); - var_dump($messages); $table = new Table($output); - $table->setHeaders(['DATE', 'MESSAGE']); + $table->setHeaders(['ID', 'MESSAGE', 'DATE']); foreach($messages as $e) { - var_dump($e); - $table-> setRows([ - [$e['date'], $e['message']] - ]); - }; + $date = $e['date']; + $message = $e['message']; + //$table-> setRows([ [$date, $message] ]); + $table->addRow($e); + } $table->render();