<?php

namespace WPD\WordPress;

final class InstallationLocator
{
    private $guard;

    public function __construct(PathGuard $guard)
    {
        $this->guard = $guard;
    }

    public function detect()
    {
        return $this->validate($this->guard->allowedRoot());
    }

    public function validate($path)
    {
        $root = $this->guard->resolveDirectory($path);
        return $root !== null && $this->isWordPress($root) ? $root : null;
    }

    private function isWordPress($root)
    {
        return is_file($root . '/wp-config.php')
            && is_dir($root . '/wp-admin')
            && is_dir($root . '/wp-includes')
            && is_dir($root . '/wp-content');
    }
}
