Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
crazyfactory/jobs / src / Processors / SlackResultProcessor.php
Size: Mime:
<?php

namespace CrazyFactory\Jobs\Processors;

use CrazyFactory\Jobs\IJobResultProcessor;
use CrazyFactory\Jobs\JobConfig;
use CrazyFactory\Jobs\JobResult;

abstract class SlackResultProcessor implements IJobResultProcessor
{
    /**
     * @param int $code
     * @param JobConfig $jobConfig
     *
     * @return bool
     */
    protected function shouldLog($code, JobConfig $jobConfig)
    {
        return ($code == 0 && $jobConfig->reportSuccess) || ($code != 0 && $jobConfig->reportError);
    }

    /**
     * @param JobResult $jobResult
     * @param JobConfig $jobConfig
     *
     * @return bool|null
     */
    public function process(JobResult $jobResult, JobConfig $jobConfig)
    {
        $code = $jobResult->getCode();

        // Only report in certain situations
        if (!$this->shouldLog($code, $jobConfig)) {
            return null;
        }

        // process
        $name = $jobConfig->name;
        $duration = $jobResult->getDurationFormatted();
        $lastParagraph = $jobResult->getLastParagraph(20);

        $lastParagraphAppendix = !empty($lastParagraph)
            ? PHP_EOL . "```\n" . implode('', $lastParagraph) . "\n```"
            : '';

        // format error
        if ($code > 0) {
            $context = [
                "text" => ":warning: *{$name}* exited with error code {$code} _($duration)_ @channel"
                    . $lastParagraphAppendix,
            ];
        }
        // format success
        else {
            $context = [
                'text' => "*{$name}* finished running _($duration)_" . $lastParagraphAppendix,
            ];
        }

        // send :)
        return $this->send($context);
    }

    /**
     * @param array $context
     *
     * @return bool
     */
    public abstract function send(array $context);
}