1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 
<?php

namespace SAREhub\Client\Processor;

use SAREhub\Client\Message\Exchange;

class Pipeline implements Processor
{

    /**
     * @var Processor[]
     */
    private $processors = [];

    /**
     * @return Pipeline
     */
    public static function newInstance()
    {
        return new self();
    }

    public function process(Exchange $exchange)
    {
        $currentExchange = $exchange;
        $orginalMessage = $currentExchange->getIn();

        $isFirstTime = true;
        foreach ($this->getProcessors() as $processor) {
            if ($isFirstTime) {
                $isFirstTime = false;
            } else {
                $currentExchange = $this->createNextExchange($currentExchange);
            }

            $processor->process($currentExchange);
            if ($exchange->isFailed()) {
                break;
            }
        }

        if (!$currentExchange->hasOut() && $currentExchange->getIn() !== $orginalMessage) {
            $currentExchange->setOut($currentExchange->getIn());
        }

        $currentExchange->setIn($orginalMessage);
    }

    protected function createNextExchange(Exchange $previousExchange)
    {
        if ($previousExchange->hasOut()) {
            $out = $previousExchange->getOut();
            $previousExchange->clearOut();
            $previousExchange->setIn($out);
        }
        return $previousExchange;
    }

    public function add(Processor $processor): Pipeline
    {
        $this->processors[] = $processor;
        return $this;
    }

    public function addAll(array $processors): Pipeline
    {
        foreach ($processors as $processor) {
            $this->add($processor);
        }

        return $this;
    }

    public function clear(): Pipeline
    {
        $this->processors = [];
        return $this;
    }

    /**
     * @return Processor[]
     */
    public function getProcessors(): array
    {
        return $this->processors;
    }

    public function __toString()
    {
        return "Pipeline[" . implode(" | ", $this->getProcessors()) . "]";
    }


}