<commit_before><?php

namespace PragmaRX\Health;

use Illuminate\Console\Command;
use PragmaRX\Yaml\Package\Yaml;
use PragmaRX\Health\Service as HealthService;

class Commands
{
    /**
     * @var Service
     */
    private $healthService;

    /**
     * Commands constructor.
     *
     * @param Service $healthService
     */
    public function __construct(HealthService $healthService)
    {
        $this->healthService = $healthService;
    }

    private function normalizeMessage($message)
    {
        $message = str_replace("\n", '', $message);
        $message = str_replace("\r", '', $message);
        $message = str_replace("\t", ' ', $message);
        $message = str_replace('<br>', ' ', $message);
        $message = str_replace('  ', ' ', $message);

        $message = wordwrap($message, 60);

        return $message;
    }

    public function panel(Command $command = null)
    {
        $columns = ['Resource', 'State', 'Message'];

        $rows = $this->healthService->health()->map(function ($resource) {
            return [
                $resource['name'],
                $resource['health']['healthy']
                    ? '<info>healthy</info>'
                    : '<fg=red>failing</fg=red>',
                $this->normalizeMessage($resource['health']['message']),
            ];
        })->toArray();

        $this->table($command, $columns, $rows);
    }

    public function check(Command $command = null)
    {
        $checker = $this->healthService->getSilentChecker();

        $errors = $checker()->where('is_global', false)->reduce(function ($carry, $item) {
            return $carry + ($item['health']['healthy'] ? 0 : 1);
        }, 0);

        if ($errors) {
            $this->error(
                $command,
                "Application needs attention, $errors ".
                str_plural('resouce', $errors).' '.
                ($errors > 1 ? 'are' : 'is').
                ' currently failing.'
            );
        }

        $this->info($command, 'Check completed with no errors.');<commit_msg>add exit codes to console health:check command<commit_after><?php

namespace PragmaRX\Health;

use Illuminate\Console\Command;
use PragmaRX\Yaml\Package\Yaml;
use PragmaRX\Health\Service as HealthService;

class Commands
{
    /**
     * @var Service
     */
    private $healthService;

    /**
     * Commands constructor.
     *
     * @param Service $healthService
     */
    public function __construct(HealthService $healthService)
    {
        $this->healthService = $healthService;
    }

    private function normalizeMessage($message)
    {
        $message = str_replace("\n", '', $message);
        $message = str_replace("\r", '', $message);
        $message = str_replace("\t", ' ', $message);
        $message = str_replace('<br>', ' ', $message);
        $message = str_replace('  ', ' ', $message);

        $message = wordwrap($message, 60);

        return $message;
    }

    public function panel(Command $command = null)
    {
        $columns = ['Resource', 'State', 'Message'];

        $rows = $this->healthService->health()->map(function ($resource) {
            return [
                $resource['name'],
                $resource['health']['healthy']
                    ? '<info>healthy</info>'
                    : '<fg=red>failing</fg=red>',
                $this->normalizeMessage($resource['health']['message']),
            ];
        })->toArray();

        $this->table($command, $columns, $rows);
    }

    public function check(Command $command = null)
    {
        $checker = $this->healthService->getSilentChecker();

        $errors = $checker()->where('is_global', false)->reduce(function ($carry, $item) {
            return $carry + ($item['health']['healthy'] ? 0 : 1);
        }, 0);

        $exitCode = 0;

        if ($errors) {
            $this->error(
                $command,
                "Application needs attention, $errors " .
                str_plural('resouce', $errors) . ' ' .
                ($errors > 1 ? 'are' : 'is') .
                ' currently failing.'
            );

            $exitCode = 255;
        } else {
            $this->info($command, 'Check completed with no errors.');
        }

        exit($exitCode);
{'fraction_non_alphanumeric': 0.16330834114339268, 'fraction_numerical': 0.0037488284910965324, 'mean_word_length': 2.00210970464135, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1306433', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1166, 'n_words': 237}