<commit_before><?php
/**
 * Domain
 * @author edgebal
 */

namespace Minds\Core\Pro;

use Exception;
use Minds\Core\Config;
use Minds\Core\Di\Di;
use Minds\Core\Util\StringValidator;
use Minds\Entities\User;
use Minds\Core\EntitiesBuilder;

class Domain
{
    /** @var Config */
    protected $config;

    /** @var Repository */
    protected $repository;

    /** @var EntitiesBuilder */
    protected $entitiesBuilder;

    /** @var Manager */
    protected $proManager;

    /**
     * Domain constructor.
     * @param Config $config
     * @param Repository $repository
     */
    public function __construct(
        $config = null,
        $repository = null,
        $entitiesBuilder = null,
        $proManager = null
    ) {
        $this->config = $config ?: Di::_()->get('Config');
        $this->repository = $repository ?: new Repository();
        $this->entitiesBuilder = $entitiesBuilder ?? Di::_()->get('EntitiesBuilder');
        $this->proManager = $proManager ?? Di::_()->get('Pro\Manager');
    }

    /**
     * @param string $domain
     * @return Settings|null
     */
    public function lookup(string $domain): ?Settings
    {
        $rootDomains = $this->config->get('pro')['root_domains'] ?? [];

        if (in_array(strtolower($domain), $rootDomains, true)) {
            return null;
        }

        $settings = $this->repository->getList([
            'domain' => $domain,
        ])->first();

        if (!$settings) {
            return null;
        }

        $user = $this->entitiesBuilder->single($settings->getUserGuid());
    
        return $this->proManager
            ->setUser($user)
            ->hydrate($settings);
    }

    /**
     * @param string $domain
     * @param string $userGuid
     * @return bool|null
     */
    public function isAvailable(string $domain, string $userGuid): ?bool
    {
        $rootDomains = $this->config->get('pro')['root_domains'] ?? [];

        if (in_array(strtolower($domain), $rootDomains, true)) {
            return false;
        }

        if (!StringValidator::isDomain($domain)) {
            return null;
        }

        $settings = $this->lookup($domain);
        return !$settings || ((string) $settings->getUserGuid() === $userGuid);
    }

    /**<commit_msg>(fix): If no user, do not load pro settings<commit_after><?php
/**
 * Domain
 * @author edgebal
 */

namespace Minds\Core\Pro;

use Exception;
use Minds\Core\Config;
use Minds\Core\Di\Di;
use Minds\Core\Util\StringValidator;
use Minds\Entities\User;
use Minds\Core\EntitiesBuilder;

class Domain
{
    /** @var Config */
    protected $config;

    /** @var Repository */
    protected $repository;

    /** @var EntitiesBuilder */
    protected $entitiesBuilder;

    /** @var Manager */
    protected $proManager;

    /**
     * Domain constructor.
     * @param Config $config
     * @param Repository $repository
     */
    public function __construct(
        $config = null,
        $repository = null,
        $entitiesBuilder = null,
        $proManager = null
    ) {
        $this->config = $config ?: Di::_()->get('Config');
        $this->repository = $repository ?: new Repository();
        $this->entitiesBuilder = $entitiesBuilder ?? Di::_()->get('EntitiesBuilder');
        $this->proManager = $proManager ?? Di::_()->get('Pro\Manager');
    }

    /**
     * @param string $domain
     * @return Settings|null
     */
    public function lookup(string $domain): ?Settings
    {
        $rootDomains = $this->config->get('pro')['root_domains'] ?? [];

        if (in_array(strtolower($domain), $rootDomains, true)) {
            return null;
        }

        $settings = $this->repository->getList([
            'domain' => $domain,
        ])->first();

        if (!$settings) {
            return null;
        }

        $user = $this->entitiesBuilder->single($settings->getUserGuid());

        if (!$user) {
            return null;
        }
    
        return $this->proManager
            ->setUser($user)
            ->hydrate($settings);
    }

    /**
     * @param string $domain
     * @param string $userGuid
     * @return bool|null
     */
    public function isAvailable(string $domain, string $userGuid): ?bool
    {
        $rootDomains = $this->config->get('pro')['root_domains'] ?? [];

        if (in_array(strtolower($domain), $rootDomains, true)) {
            return false;
        }

        if (!StringValidator::isDomain($domain)) {
            return null;
        }

        $settings = $this->lookup($domain);
        return !$settings || ((string) $settings->getUserGuid() === $userGuid);
    }

    /**
{'fraction_non_alphanumeric': 0.16054451166810718, 'fraction_numerical': 0.0, 'mean_word_length': 2.2235376044568245, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '5788295', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1342, 'n_words': 282}