<commit_before><?php

namespace Concrete\Core\Block\BlockType;

use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Entity\Block\BlockType\BlockType as BlockTypeEntity;
use Concrete\Core\Filesystem\FileLocator;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Support\Facade\Application;
use Doctrine\ORM\EntityManagerInterface;

class BlockType
{
    /**
     * Get a BlockType given its handle.
     *
     * @param string $btHandle
     *
     * @return \Concrete\Core\Entity\Block\BlockType\BlockType|null
     */
    public static function getByHandle($btHandle)
    {
        $result = null;
        $btHandle = (string) $btHandle;
        if ($btHandle !== '') {
            $app = Application::getFacadeApplication();
            $em = $app->make(EntityManagerInterface::class);
            $repo = $em->getRepository(BlockTypeEntity::class);
            $result = $repo->findOneBy(['btHandle' => $btHandle]);
            if ($result !== null) {
                $result->loadController();
            }
        }

        return $result;
    }

    /**
     * Get a BlockType given its ID.
     *
     * @param int $btID
     *
     * @return \Concrete\Core\Entity\Block\BlockType\BlockType|null
     */
    public static function getByID($btID)
    {
        $result = null;
        $btID = (int) $btID;
        if ($btID !== 0) {
            $app = Application::getFacadeApplication();
            $em = $app->make(EntityManagerInterface::class);
            $result = $em->find(BlockTypeEntity::class, $btID);
            if ($result !== null) {
                $result->loadController();
            }
        }

        return $result;
    }

    /**
     * Install a BlockType that is passed via a btHandle string. The core or override directories are parsed.
     *
     * @param string $btHandle The handle of the block type
     * @param \Concrete\Core\Entity\Package|\Concrete\Core\Package\Package|string|false $pkg The package owning the block type (or its handle)
     *<commit_msg>Avoid getting same BlockType object repeatedly<commit_after><?php

namespace Concrete\Core\Block\BlockType;

use Concrete\Core\Cache\Level\RequestCache;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Entity\Block\BlockType\BlockType as BlockTypeEntity;
use Concrete\Core\Filesystem\FileLocator;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Support\Facade\Application;
use Doctrine\ORM\EntityManagerInterface;

class BlockType
{
    /**
     * Get a BlockType given its handle.
     *
     * @param string $btHandle
     *
     * @return \Concrete\Core\Entity\Block\BlockType\BlockType|null
     */
    public static function getByHandle($btHandle)
    {
        $result = null;
        $btHandle = (string) $btHandle;
        if ($btHandle !== '') {
            $app = Application::getFacadeApplication();
            $em = $app->make(EntityManagerInterface::class);
            $repo = $em->getRepository(BlockTypeEntity::class);
            $result = $repo->findOneBy(['btHandle' => $btHandle]);
            if ($result !== null) {
                $result->loadController();
            }
        }

        return $result;
    }

    /**
     * Get a BlockType given its ID.
     *
     * @param int $btID
     *
     * @return \Concrete\Core\Entity\Block\BlockType\BlockType|null
     */
    public static function getByID($btID)
    {
        $app = Application::getFacadeApplication();
        /** @var RequestCache $cache */
        $cache = $app->make('cache/request');
        $key = '/BlockType/' . $btID;
        if ($cache->isEnabled()) {
            $item = $cache->getItem($key);
            if ($item->isHit()) {
                return $item->get();
            }
        }

        $result = null;
        $btID = (int) $btID;
        if ($btID !== 0) {
            $em = $app->make(EntityManagerInterface::class);
            $result = $em->find(BlockTypeEntity::class, $btID);
            if ($result !== null) {
                $result->loadController();
            }
        }

        if (is_object($result) && isset($item) && $item->isMiss()) {
            $item->set($result);
            $cache->save($item);
        }

        return $result;
    }

    /**
     * Install a BlockType that is passed via a btHandle string. The core or override directories are parsed.
     *
     * @param string $btHandle The handle of the block type
     * @param \Concrete\Core\Entity\Package|\Concrete\Core\Package\Package|string|false $pkg The package owning the block type (or its handle)
     *
{'fraction_non_alphanumeric': 0.13272211307574766, 'fraction_numerical': 0.0004365858982754857, 'mean_word_length': 2.2404526166902405, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '7237740', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1309, 'n_words': 289}