zenstruck / redirect-bundle

Store redirects for your site and keeps statistics on redirects and 404 errors.
MIT License
25 stars 8 forks source link

Inheritance Issues - Trying to override model functions #26

Closed Emirii closed 5 years ago

Emirii commented 5 years ago

Hello there, I have a fairly large project and your bundle would fit in great with it, aside from one hiccup. The only issue I have is that I have multiple subdomains in my project. When I try to save a redirect it automatically transforms 'https://suba.domain.com/test' to '/test' even though I would like to control multiple sub domains. The redirect happens on all subdomains instead of this one. I assume multiple subdomain functionality is not built in the project.

There are also some issues with inheritance because the model has all private methods, so overwriting functions is impossible as well.

I was thinking about just forking your bundle and using for my own projects, but would you be interested in a PR for this functionality?

EDIT: I added a PR for the extensible classes:

27

Emirii commented 5 years ago

This allows for proper extending and overriding, and with it I am able to easily solve my other problem by overriding createAbsoluteUri. Thanks!

<?php

namespace Zenstruck\RedirectBundle\Model;

/**
 * @author Kevin Bond <kevinbond@gmail.com>
 */
abstract class Redirect
{
    /**
     * @var string
     */
    protected $source;

    /**
     * @var string
     */
    protected $destination;

    /**
     * @var boolean
     */
    protected $permanent;

    /**
     * @var integer
     */
    protected $count = 0;

    /**
     * @var \DateTime
     */
    protected $lastAccessed = null;

    /**
     * @param NotFound $notFound
     * @param string   $destination
     * @param bool     $permanent
     *
     * @return static
     */
    public static function createFromNotFound(NotFound $notFound, $destination, $permanent = true)
    {
        return new static($notFound->getPath(), $destination, $permanent);
    }

    /**
     * @param string $source
     * @param string $destination
     * @param bool   $permanent
     */
    public function __construct($source, $destination, $permanent = true)
    {
        $this->setSource($source);
        $this->setDestination($destination);
        $this->setPermanent($permanent);
    }

    /**
     * @return string
     */
    public function getSource()
    {
        return $this->source;
    }

    /**
     * @param string $source
     */
    public function setSource($source)
    {
        $source = trim($source);
        $source = !empty($source) ? $source : null;

        if (null !== $source) {
            $source = $this->createAbsoluteUri($source);
        }

        $this->source = $source;
    }

    /**
     * @return string
     */
    public function getDestination()
    {
        return $this->destination;
    }

    /**
     * @param string $destination
     */
    public function setDestination($destination)
    {
        $destination = trim($destination);
        $destination = !empty($destination) ? $destination : null;

        if (null !== $destination && null === parse_url($destination, PHP_URL_SCHEME)) {
            $destination = $this->createAbsoluteUri($destination, true);
        }

        $this->destination = $destination;
    }

    /**
     * @return bool
     */
    public function isPermanent()
    {
        return $this->permanent;
    }

    /**
     * @param bool $permanent
     */
    public function setPermanent($permanent)
    {
        $this->permanent = $permanent;
    }

    /**
     * @return int
     */
    public function getCount()
    {
        return $this->count;
    }

    /**
     * @param int $amount
     */
    public function increaseCount($amount = 1)
    {
        $this->count += $amount;
    }

    /**
     * @return \DateTime
     */
    public function getLastAccessed()
    {
        return $this->lastAccessed;
    }

    /**
     * @param \DateTime $time
     */
    public function updateLastAccessed(\DateTime $time = null)
    {
        if (null === $time) {
            $time = new \DateTime('now');
        }

        $this->lastAccessed = $time;
    }

    /**
     * @param string $path
     * @param bool   $allowQueryString
     *
     * @return string
     */
    protected function createAbsoluteUri($path, $allowQueryString = false)
    {
        $value = '/'.ltrim(parse_url($path, PHP_URL_PATH), '/');

        if ($allowQueryString && $query = parse_url($path, PHP_URL_QUERY)) {
            $value .= '?'.$query;
        }

        return $value;
    }
}