Skip to content

Commit

Permalink
feat: chunker
Browse files Browse the repository at this point in the history
  • Loading branch information
apple-x-co committed Apr 4, 2021
1 parent a8aad64 commit 9f3401f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
44 changes: 44 additions & 0 deletions src/Chunker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rocket;

class Chunker
{
/**
* @param string $text
* @param int $length
*
* @return array<string>
*/
public function __invoke($text, $length)
{
$chunks = [];
$buf = null;

$lines = explode(PHP_EOL, $text);
foreach ($lines as $line) {
if (strlen($line) >= $length) {
foreach (str_split($line, $length) as $chunk) {
$chunks[] = $chunk;
}

continue;
}

$line .= PHP_EOL;

if (strlen($buf . $line) > $length) {
$chunks[] = $buf;
$buf = null;
}

$buf .= $line;
}

if ($buf !== null) {
$chunks[] = $buf;
}

return $chunks;
}
}
7 changes: 5 additions & 2 deletions src/Command/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rocket\Command;

use Rocket\Chunker;
use Rocket\CommandInterface;
use Rocket\Configure;
use Rocket\Main;
Expand Down Expand Up @@ -37,6 +38,8 @@ public function execute()
$configPath = realpath($this->options->getConfig());
$configure = new Configure($configPath);

$chunker = new Chunker();

$self = $this;

if (posix_getpwuid(posix_geteuid())['name'] !== $configure->read('user')) {
Expand Down Expand Up @@ -282,7 +285,7 @@ public function execute()
)
);

$chunks = str_split($gitPullLog, SlackSection::TEXT_MAX_LENGTH - 6);
$chunks = $chunker($gitPullLog, SlackSection::TEXT_MAX_LENGTH - 6);
foreach ($chunks as $chunk) {
$message
->addBlock(
Expand All @@ -306,7 +309,7 @@ public function execute()
)
);

$chunks = str_split($syncLog, SlackSection::TEXT_MAX_LENGTH - 6);
$chunks = $chunker($syncLog, SlackSection::TEXT_MAX_LENGTH - 6);
foreach ($chunks as $chunk) {
$message
->addBlock(
Expand Down
9 changes: 6 additions & 3 deletions src/Command/SlackNotificationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rocket\Command;

use Rocket\Chunker;
use Rocket\CommandInterface;
use Rocket\Configure;
use Rocket\Options;
Expand Down Expand Up @@ -30,14 +31,16 @@ public function execute()
$configPath = realpath($this->options->getConfig());
$configure = new Configure($configPath);

$lines = [];
$content = null;
while ($line = fgets(STDIN)) {
$lines[] = trim($line);
$content .= $line;
}

$message = new SlackMessage();

$chunks = str_split(implode(PHP_EOL, $lines), SlackSection::TEXT_MAX_LENGTH - 6);
$chunker = new Chunker();
$chunks = $chunker($content, SlackSection::TEXT_MAX_LENGTH - 6);

foreach ($chunks as $chunk) {
$message
->addBlock(
Expand Down

0 comments on commit 9f3401f

Please sign in to comment.