bnomei/kirby3-robots-txt

View on GitHub
classes/Robotstxt.php

Summary

Maintainability
B
5 hrs
Test Coverage
<?php

declare(strict_types=1);

namespace Bnomei;

use Kirby\Toolkit\A;

final class Robotstxt
{
    /**
     * @var string[]
     */
    private $txt;
    /**
     * @var array
     */
    private $options;

    /**
     * Robotstxt constructor.
     *
     * @param array $options
     */
    public function __construct(array $options = [])
    {
        $this->txt = [];

        $defaults = [
            'debug' => option('debug'),
            'content' => option('bnomei.robots-txt.content'),
            'groups' => option('bnomei.robots-txt.groups'),
            'sitemap' => option('bnomei.robots-txt.sitemap'),
        ];
        $this->options = array_merge($defaults, $options);

        foreach ($this->options as $key => $call) {
            if (is_callable($call)) {
                $this->options[$key] = $call();
            }
        }

        $this->addContent(A::get($this->options, 'content'));
        $this->addGroups(A::get($this->options, 'groups'));
        $this->addSitemap(A::get($this->options, 'sitemap'));
    }

    /**
     * @return array|null
     */
    public function toArray(): ?array
    {
        return count($this->txt) ? $this->txt : null;
    }

    /**
     * @return string|null
     */
    public function toTxt(): ?string
    {
        return count($this->txt) ? implode(PHP_EOL, $this->txt) . PHP_EOL : null;
    }

    /**
     * @param null $content
     *
     * @return Robotstxt
     */
    private function addContent($content = null): Robotstxt
    {
        if (! $content) {
            return $this;
        }
        $this->txt[] = (string) $content;
        return $this;
    }

    /**
     * @param null $groups
     *
     * @return Robotstxt
     */
    private function addGroups($groups = null): Robotstxt
    {
        if (! $groups) {
            return $this;
        }
        if (A::get($this->options, 'debug')) {
            $groups = ['*' => ['disallow' => ['/']]];
        }
        if (! is_array($groups) && ! is_string($groups) && is_callable($groups)) {
            $groups = $groups();
        }
        if (is_array($groups)) {
            foreach ($groups as $useragent => $group) {
                $this->txt[] = 'user-agent: ' . $useragent;
                foreach ($group as $field => $values) {
                    foreach ($values as $value) {
                        $this->txt[] = $field . ': ' . $value;
                    }
                }
            }
        } else {
            $this->txt[] = (string) $groups;
        }
        return $this;
    }

    /**
     * @param null $sitemap
     *
     * @return Robotstxt
     */
    private function addSitemap($sitemap = null): Robotstxt
    {
        // @codeCoverageIgnoreStart
        if (option('omz13.xmlsitemap.disable') === false) {
            $this->txt[] = 'sitemap: ' . url('/sitemap.xml');
            return $this;
        }
        if (option('fabianmichael.meta.sitemap') === true) {
            $this->txt[] = 'sitemap: ' . url('/sitemap.xml');
            return $this;
        }
        if (option('tobimori.seo.robots.active') === false) {
            $this->txt[] = 'sitemap: ' . url('/sitemap.xml');
            return $this;
        }
        if (option('johannschopplich.helpers.sitemap.enable') === true && option('johannschopplich.helpers.robots.enable') === false) {
            $this->txt[] = 'sitemap: ' . url('/sitemap.xml');
            return $this;
        }
        if (option('kirbyzone.sitemapper.customMap') instanceof \Closure) {
            $this->txt[] = 'sitemap: ' . url('/sitemap.xml');
            return $this;
        }
        $feedPlugin = kirby()->plugin('bnomei/feed');
        if ($feedPlugin && option('bnomei.feed.sitemap.enable') === true && version_compare($feedPlugin->version(), '1.4.0', '>=')) {
            $this->txt[] = 'sitemap: ' . url('/sitemap.xml');
            return $this;
        }
        // @codeCoverageIgnoreEnd

        if (! $sitemap) {
            return $this;
        }

        $this->txt[] = 'sitemap: ' . url($sitemap);
        return $this;
    }
}