ocrampete16/php-chain

View on GitHub
src/Link.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace MarcoPetersen\Chain;

abstract class Link
{
    /**
     * @var Link|null
     */
    private $successor;

    /**
     * @param Link|string $successor
     *
     * @return $this
     */
    public function then($successor)
    {
        if (is_string($successor)) {
            $successor = new $successor();
        }

        $this->successor = $successor;

        return $this;
    }

    /**
     * @return Link|null
     */
    public function getSuccessor()
    {
        return $this->successor;
    }

    /**
     * @param $payload
     *
     * @return mixed
     */
    protected function next($payload)
    {
        if (!$this->successor) {
            return $payload;
        }

        return $this->successor->execute($payload);
    }

    /**
     * @param $payload
     *
     * @return mixed
     */
    abstract public function execute($payload);
}