<commit_before><?php

namespace Vsavritsky\MorpherBundle\Entity;

use Doctrine\ORM\EntityManager;

use Vsavritsky\MorpherBundle\Exception\ExceededLimitRequests;
use Vsavritsky\MorpherBundle\Exception\IpBlocked;
use Vsavritsky\MorpherBundle\Exception\MethodError;
use Vsavritsky\MorpherBundle\Exception\ExpectedRussianWord;
use Vsavritsky\MorpherBundle\Exception\ExpectedParameter;
use Vsavritsky\MorpherBundle\Exception\ServiceNotPaid;
use Vsavritsky\MorpherBundle\Exception\UserNotFound;
use Vsavritsky\MorpherBundle\Exception\AuthorisationError;
use Vsavritsky\MorpherBundle\Exception\UnknownCaseWord;
use Vsavritsky\MorpherBundle\Exception\UrlTypeIncorrect;

class RequestFacade
{
    private $em;
    private $repository;
    private $requestExec;

    const BASE_URL = 'http://api.morpher.ru/WebService.asmx';
    const INFLECT = '/GetXml?s=';
    const LIMIT = '/GetDailyQueryLimit';

    const REQUEST_INFLECT_TYPE = 'inflect';
    const REQUEST_LIMIT_TYPE = 'limit';

    const CASE_ROD = 'Р';
    const CASE_DAT = 'Д';
    const CASE_VIN = 'В';
    const CASE_TVOR = 'Т';
    const CASE_PREDL = 'П';
    const CASE_GDE = 'М';

    private $cases = array(self::CASE_ROD, self::CASE_DAT, self::CASE_VIN, self::CASE_TVOR, self::CASE_PREDL, self::CASE_GDE);

    public function __construct(EntityManager $entityManager, RequestExec $requestExec)
    {
        $this->em = $entityManager;
        $this->repository = $entityManager->getRepository('VsavritskyMorpherBundle:Request');
        $this->requestExec = $requestExec;
    }

    public function inflect($word, $returnType, $default = '')
    {
        if (!in_array($returnType, $this->cases)) {
            throw new UnknownCaseWord();
        }

        $result = $this->getResultWithCache(self::REQUEST_INFLECT_TYPE, $word, $returnType);

        if (!isset($result[$returnType])) {
            return $default;
        }

        return $result[$returnType];
    }

    public function getLimit()
    {
        return $this->getResultLimit(self::REQUEST_LIMIT_TYPE);
    }

    private function getResultWithCache($type, $word)
    {
        $result = $this->getCache($type, $word);
        if (!empty($result)) {
            return $result;
        }

        $url = $this->getUrlByType($type, $word);
        $result = $this->requestExec->exec($url);

        if (!$this->errorRequest($result)) {
            $this->saveCache($type, $word, $result);
        }

        return $result;
    }

    private function getResultLimit($type)
    {
        $url = $this->getUrlByType($type);
        $result = $this->requestExec->exec($url);

        return $result;
    }<commit_msg>add plural parameter in function inflect<commit_after><?php

namespace Vsavritsky\MorpherBundle\Entity;

use Doctrine\ORM\EntityManager;

use Vsavritsky\MorpherBundle\Exception\ExceededLimitRequests;
use Vsavritsky\MorpherBundle\Exception\IpBlocked;
use Vsavritsky\MorpherBundle\Exception\MethodError;
use Vsavritsky\MorpherBundle\Exception\ExpectedRussianWord;
use Vsavritsky\MorpherBundle\Exception\ExpectedParameter;
use Vsavritsky\MorpherBundle\Exception\ServiceNotPaid;
use Vsavritsky\MorpherBundle\Exception\UserNotFound;
use Vsavritsky\MorpherBundle\Exception\AuthorisationError;
use Vsavritsky\MorpherBundle\Exception\UnknownCaseWord;
use Vsavritsky\MorpherBundle\Exception\UrlTypeIncorrect;

class RequestFacade
{
    private $em;
    private $repository;
    private $requestExec;

    const BASE_URL = 'http://api.morpher.ru/WebService.asmx';
    const INFLECT = '/GetXml?s=';
    const LIMIT = '/GetDailyQueryLimit';

    const REQUEST_INFLECT_TYPE = 'inflect';
    const REQUEST_LIMIT_TYPE = 'limit';

    const CASE_ROD = 'Р';
    const CASE_DAT = 'Д';
    const CASE_VIN = 'В';
    const CASE_TVOR = 'Т';
    const CASE_PREDL = 'П';
    const CASE_GDE = 'М';
    const PLURAL = 'множественное';

    private $cases = array(self::CASE_ROD, self::CASE_DAT, self::CASE_VIN, self::CASE_TVOR, self::CASE_PREDL, self::CASE_GDE);

    public function __construct(EntityManager $entityManager, RequestExec $requestExec)
    {
        $this->em = $entityManager;
        $this->repository = $entityManager->getRepository('VsavritskyMorpherBundle:Request');
        $this->requestExec = $requestExec;
    }

    public function inflect($word, $returnType, $default = '', $plural = false)
    {
        if (!in_array($returnType, $this->cases)) {
            throw new UnknownCaseWord();
        }

        $result = $this->getResultWithCache(self::REQUEST_INFLECT_TYPE, $word, $returnType);

        $data = $result;
        if ($plural) {
            $data = $result[self::PLURAL];
        }

        if (!isset($data[$returnType])) {
            return $default;
        }

        return $data[$returnType];
    }

    public function getLimit()
    {
        return $this->getResultLimit(self::REQUEST_LIMIT_TYPE);
    }

    private function getResultWithCache($type, $word)
    {
        $result = $this->getCache($type, $word);
        if (!empty($result)) {
            return $result;
        }

        $url = $this->getUrlByType($type, $word);
        $result = $this->requestExec->exec($url);

        if (!$this->errorRequest($result)) {
            $this->saveCache($type, $word, $result);
        }

        return $result;
    }

    private function getResultLimit($type)
    {
        $url = $this->getUrlByType($type);
        $result = $this->requestExec->exec($url);

        return $result;
    }
{'fraction_non_alphanumeric': 0.12838332114118509, 'fraction_numerical': 0.0, 'mean_word_length': 3.3335974643423136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '7214023', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1697, 'n_words': 304}