<commit_before><?php

namespace Watson\Canonical;

use Closure;
use Illuminate\Contracts\Config\Repository;

class CanonicalMiddleware
{
    /**
     * The config repository.
     *
     * @var \Illuminate\Contracts\Config\Repository
     */
    protected $config;

    /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Config\Repository  $config
     * @return void
     */
    public function __construct(Repository $config)
    {
        $this->config = $config;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->config->get('canonical.host') !== null) {
            if ($this->isIncorrectHost($request)) {
                if (in_array($request->header('Host'), (array) $this->config->get('canonical.ignore'))) {
                    return;
                }

                return $this->redirect($request);
            }

            if ($this->isIncorrectScheme($request)) {
                return $this->redirect($request, true);
            }
        }

        return $next($request);
    }

    /**
     * Determine whether the request has the incorrect host.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function isIncorrectHost($request)
    {
        return $request->header('Host') !== $this->config->get('canonical.host');
    }

    /**
     * Determine whether the request has the incorrect scheme.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function isIncorrectScheme($request)
    {
        return $this->config->get('canonical.secure') && ! $request->secure();
    }

    /**
     * Redirect the request to the canonical host, secure if neccessary.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  bool $secure
     * @return \Illuminate\Http\Redirect
     */
    protected function redirect($request, $secure = false)
    {
        $headers = ['Host' => $this->config->get('canonical.host')];

        return redirect()->to($request->path(), 301, $headers, $secure);;
    }
}<commit_msg>Don't inject repository<commit_after><?php

namespace Watson\Canonical;

use Closure;

class CanonicalMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (config('canonical.host') !== null) {
            if ($this->isIncorrectHost($request)) {
                if (in_array($request->header('Host'), (array) config('canonical.ignore'))) {
                    return;
                }

                return $this->redirect($request);
            }

            if ($this->isIncorrectScheme($request)) {
                return $this->redirect($request, true);
            }
        }

        return $next($request);
    }

    /**
     * Determine whether the request has the incorrect host.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function isIncorrectHost($request)
    {
        return $request->header('Host') !== config('canonical.host');
    }

    /**
     * Determine whether the request has the incorrect scheme.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function isIncorrectScheme($request)
    {
        return config('canonical.secure') && ! $request->secure();
    }

    /**
     * Redirect the request to the canonical host, secure if neccessary.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  bool $secure
     * @return \Illuminate\Http\Redirect
     */
    protected function redirect($request, $secure = false)
    {
        $headers = ['Host' => config('canonical.host')];

        return redirect()->to($request->path(), 301, $headers, $secure);;
    }
}
{'fraction_non_alphanumeric': 0.13869149718206322, 'fraction_numerical': 0.001470227885322225, 'mean_word_length': 2.0761115297663904, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4411460', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1139, 'n_words': 251}