<commit_before><?php

namespace Oro\Bundle\EmailBundle\Tools;

use Oro\Bundle\EmailBundle\Entity\Email;
use Oro\Bundle\FormBundle\Form\DataTransformer\SanitizeHTMLTransformer;
use Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink;
use Oro\Bundle\SecurityBundle\SecurityFacade;

class EmailHelper
{
    const MAX_DESCRIPTION_LENGTH = 500;

    /**
     * @var SecurityFacade
     */
    protected $securityFacade;

    /**
     * @var string
     */
    protected $cacheDir;

    /**
     * @param ServiceLink $securityFacadeLink
     * @param string|null $cacheDir
     */
    public function __construct(ServiceLink $securityFacadeLink, $cacheDir = null)
    {
        $this->securityFacade = $securityFacadeLink->getService();
        $this->cacheDir = $cacheDir;
    }

    /**
     * @param string $content
     * @return string
     */
    public function getStrippedBody($content)
    {
        $transformer = new SanitizeHTMLTransformer(null, $this->cacheDir);
        $content = $transformer->transform($content);
        return strip_tags($content);
    }

    /**
     * Get shorter email body
     *
     * @param string $content
     * @param int $maxLength
     * @return string
     */
    public function getShortBody($content, $maxLength = self::MAX_DESCRIPTION_LENGTH)
    {
        if (mb_strlen($content) > $maxLength) {
            $content = mb_substr($content, 0, $maxLength);
            $lastOccurrencePos = strrpos($content, ' ');
            if ($lastOccurrencePos !== false) {
                $content = mb_substr($content, 0, $lastOccurrencePos);
            }
        }

        return $content;
    }

    /**
     * @param Email $entity
     * @return bool
     */
    public function isEmailViewGranted(Email $entity)
    {
        return $this->isEmailActionGranted('VIEW', $entity);
    }

    /**
     * @param Email $entity
     * @return bool
     */
    public function isEmailEditGranted(Email $entity)
    {
        return $this->isEmailActionGranted('EDIT', $entity);
    }

    /**
     * @param string $action
     * @param Email $entity
     * @return bool
     */
    public function isEmailActionGranted($action, Email $entity)
    {
        $isGranted = false;
        foreach ($entity->getEmailUsers() as $emailUser) {
            if ($this->securityFacade->isGranted($action, $emailUser)) {
                $isGranted = true;
                break;
            }
        }

        return $isGranted;
    }
}<commit_msg>CRM-2480: User Mailbox Sync with Ownership Type User for Emails. - Fixed Circular reference.<commit_after><?php

namespace Oro\Bundle\EmailBundle\Tools;

use Oro\Bundle\EmailBundle\Entity\Email;
use Oro\Bundle\FormBundle\Form\DataTransformer\SanitizeHTMLTransformer;
use Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink;

class EmailHelper
{
    const MAX_DESCRIPTION_LENGTH = 500;

    /**
     * @var ServiceLink
     */
    protected $securityFacadeLink;

    /**
     * @var string
     */
    protected $cacheDir;

    /**
     * @param ServiceLink $securityFacadeLink
     * @param string|null $cacheDir
     */
    public function __construct(ServiceLink $securityFacadeLink, $cacheDir = null)
    {
        $this->securityFacadeLink = $securityFacadeLink;
        $this->cacheDir = $cacheDir;
    }

    /**
     * @param string $content
     * @return string
     */
    public function getStrippedBody($content)
    {
        $transformer = new SanitizeHTMLTransformer(null, $this->cacheDir);
        $content = $transformer->transform($content);
        return strip_tags($content);
    }

    /**
     * Get shorter email body
     *
     * @param string $content
     * @param int $maxLength
     * @return string
     */
    public function getShortBody($content, $maxLength = self::MAX_DESCRIPTION_LENGTH)
    {
        if (mb_strlen($content) > $maxLength) {
            $content = mb_substr($content, 0, $maxLength);
            $lastOccurrencePos = strrpos($content, ' ');
            if ($lastOccurrencePos !== false) {
                $content = mb_substr($content, 0, $lastOccurrencePos);
            }
        }

        return $content;
    }

    /**
     * @param Email $entity
     * @return bool
     */
    public function isEmailViewGranted(Email $entity)
    {
        return $this->isEmailActionGranted('VIEW', $entity);
    }

    /**
     * @param Email $entity
     * @return bool
     */
    public function isEmailEditGranted(Email $entity)
    {
        return $this->isEmailActionGranted('EDIT', $entity);
    }

    /**
     * @param string $action
     * @param Email $entity
     * @return bool
     */
    public function isEmailActionGranted($action, Email $entity)
    {
        $isGranted = false;
        foreach ($entity->getEmailUsers() as $emailUser) {
            if ($this->securityFacadeLink->getService()->isGranted($action, $emailUser)) {
                $isGranted = true;
                break;
            }
        }

        return $isGranted;
    }
}
{'fraction_non_alphanumeric': 0.11941786283891546, 'fraction_numerical': 0.0027910685805422647, 'mean_word_length': 2.3159286186384667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '1684536', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1465, 'n_words': 294}