<commit_before><?php

namespace Service;

use Entity\Meta;

/**
 * Class MetaService
 * @package Service
 */
class MetaService
{
    /**
     * @param $url
     *
     * @return Meta
     */
    public function getByUrl($url)
    {
        $meta = new Meta;

        $url = strpos($url, 'http') === 0 ? $url : 'http://' . $url;
        $doc = $this->loadContent($url);

        if (!$doc) {
            return $meta;
        }

        $doc = $this->loadContent($url);
        $meta->setUrl($url);
        $meta->setTitle($this->findTitle($doc));
        $meta->setMetaDescription($this->findMetaDescription($doc));

        return $meta;
    }

    /**
     * @param string $url
     * @return \DOMDocument
     */
    protected function loadContent($url)
    {
        $options = [
            'http' => [
                'method' => 'GET',
                'header' => 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' . PHP_EOL
            ]
        ];

        $html = '';
        $html .= @file_get_contents($url, false, stream_context_create($options));
        return @\DOMDocument::loadHTML($html);
    }

    /**
     * @param \DOMDocument $doc
     *
     * @return string
     */
    protected function findTitle(\DOMDocument $doc)
    {
        return $doc->getElementsByTagName('title')->item(0)->nodeValue;
    }

    /**
     * @param \DOMDocument $doc
     *
     * @return string<commit_msg>Empty title fix<commit_after><?php

namespace Service;

use Entity\Meta;

/**
 * Class MetaService
 * @package Service
 */
class MetaService
{
    /**
     * @param $url
     *
     * @return Meta
     */
    public function getByUrl($url)
    {
        $meta = new Meta;

        $url = strpos($url, 'http') === 0 ? $url : 'http://' . $url;
        $doc = $this->loadContent($url);

        if (!$doc) {
            return $meta;
        }

        $doc = $this->loadContent($url);
        $meta->setUrl($url);
        $meta->setTitle($this->findTitle($doc) ?: $url);
        $meta->setMetaDescription($this->findMetaDescription($doc));

        return $meta;
    }

    /**
     * @param string $url
     * @return \DOMDocument
     */
    protected function loadContent($url)
    {
        $options = [
            'http' => [
                'method' => 'GET',
                'header' => 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' . PHP_EOL
            ]
        ];

        $html = @file_get_contents($url, false, stream_context_create($options));
        return @\DOMDocument::loadHTML((string) $html);
    }

    /**
     * @param \DOMDocument $doc
     *
     * @return string|null
     */
    protected function findTitle(\DOMDocument $doc)
    {
        $titleNode = $doc->getElementsByTagName('title');
        $titleContents = $titleNode ? $titleNode->item(0) : null;

        return $titleContents ? $titleContents->nodeValue : null;
    }

    /**
     * @param \DOMDocument $doc
     *
     * @return string
{'fraction_non_alphanumeric': 0.1615186615186615, 'fraction_numerical': 0.018018018018018018, 'mean_word_length': 1.9469194312796207, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '331341', 'n_tokens_mistral': 1027, 'n_tokens_neox': 975, 'n_words': 194}