<commit_before><?php
/**
 * CoolMS2 User Module (http://www.coolms.com/)
 *
 * @link      http://github.com/coolms/user for the canonical source repository
 * @copyright Copyright (c) 2006-2015 Altgraphic, ALC (http://www.altgraphic.com)
 * @license   http://www.coolms.com/license/new-bsd New BSD License
 * @author    <NAME> <<EMAIL>>
 */

namespace CmsUser\Mapping\Blameable;

use Doctrine\Common\EventArgs,
    Doctrine\Common\NotifyPropertyChanged,
    Gedmo\Blameable\BlameableListener,
    CmsDoctrine\Mapping\Dateable\TimestampableSubscriber;

/**
 * The Blameable subscriber.
 *
 * @author  <NAME> <<EMAIL>>
 */
class BlameableSubscriber extends BlameableListener
{
    /**
     * @var bool
     */
    protected $useAssociations = true;

    /**
     * __construct
     */
    public function __construct($useAssociations = null)
    {
        if (!class_exists(TimestampableSubscriber::CHANGEABLE_ODM_ANNOTATION_ALIAS)) {
            class_alias(TimestampableSubscriber::CHANGEABLE_ANNOTATION, TimestampableSubscriber::CHANGEABLE_ODM_ANNOTATION_ALIAS);
        }

        if (!class_exists(TimestampableSubscriber::CHANGEABLE_ORM_ANNOTATION_ALIAS)) {
            class_alias(TimestampableSubscriber::CHANGEABLE_ANNOTATION, TimestampableSubscriber::CHANGEABLE_ORM_ANNOTATION_ALIAS);
        }

        parent::__construct();

        if (null !== $useAssociations) {
            $this->useAssociations = $useAssociations;
        }
    }

    /**
     * {@inheritDoc}
     */
    public function loadClassMetadata(EventArgs $eventArgs)
    {
        parent::loadClassMetadata($eventArgs);

        $meta = $eventArgs->getClassMetadata();
        $name = $meta->getName();

        if ($this->useAssociations && !empty(self::$configurations[$this->name][$name]['mappings'])) {
            $this->getEventAdapter($eventArgs)
                ->remap($meta, self::$configurations[$this->name][$name]['mappings'], $this->user);
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function getNamespace()
    {
        return __NAMESPACE__;
    }

    /**
     * {@inheritDoc}
     */
    protected function updateField($object, $ea, $meta, $field)
    {
        $property = $meta->getReflectionProperty($field);
        $oldValue = $property->getValue($object);
        $newValue = $this->getUserValue($meta, $field);

        //if blame is reference, persist object
        if ($meta->hasAssociation($field)) {
            $ea->getObjectManager()->persist($newValue);
        }

        $setter = 'set' . ucfirst($field);
        if (method_exists($object, $setter)) {
            $object->$setter($newValue);
        } else {
            $property->setValue($object, $newValue);
        }

        if ($object instanceof NotifyPropertyChanged) {
            $uow = $ea->getObjectManager()->getUnitOfWork();<commit_msg>Initial commit<commit_after><?php
/**
 * CoolMS2 User Module (http://www.coolms.com/)
 *
 * @link      http://github.com/coolms/user for the canonical source repository
 * @copyright Copyright (c) 2006-2015 Altgraphic, ALC (http://www.altgraphic.com)
 * @license   http://www.coolms.com/license/new-bsd New BSD License
 * @author    <NAME> <<EMAIL>>
 */

namespace CmsUser\Mapping\Blameable;

use Doctrine\Common\EventArgs,
    Doctrine\Common\NotifyPropertyChanged,
    Gedmo\Blameable\BlameableListener,
    CmsDoctrine\Mapping\Dateable\TimestampableSubscriber;

/**
 * The Blameable subscriber.
 *
 * @author  <NAME> <<EMAIL>>
 */
class BlameableSubscriber extends BlameableListener
{
    /**
     * @var bool
     */
    protected $useAssociations = true;

    /**
     * __construct
     */
    public function __construct($useAssociations = null)
    {
        if (!class_exists(TimestampableSubscriber::CHANGEABLE_ODM_ANNOTATION_ALIAS)) {
            class_alias(TimestampableSubscriber::CHANGEABLE_ANNOTATION, TimestampableSubscriber::CHANGEABLE_ODM_ANNOTATION_ALIAS);
        }

        if (!class_exists(TimestampableSubscriber::CHANGEABLE_ORM_ANNOTATION_ALIAS)) {
            class_alias(TimestampableSubscriber::CHANGEABLE_ANNOTATION, TimestampableSubscriber::CHANGEABLE_ORM_ANNOTATION_ALIAS);
        }

        parent::__construct();

        if (null !== $useAssociations) {
            $this->useAssociations = (bool) $useAssociations;
        }
    }

    /**
     * {@inheritDoc}
     */
    public function loadClassMetadata(EventArgs $eventArgs)
    {
        parent::loadClassMetadata($eventArgs);

        $meta = $eventArgs->getClassMetadata();
        $name = $meta->getName();

        if ($this->useAssociations && !empty(self::$configurations[$this->name][$name]['mappings'])) {
            $this->getEventAdapter($eventArgs)
                ->remap($meta, self::$configurations[$this->name][$name]['mappings'], $this->user);
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function getNamespace()
    {
        return __NAMESPACE__;
    }

    /**
     * {@inheritDoc}
     */
    protected function updateField($object, $ea, $meta, $field)
    {
        $newValue = $this->getUserValue($meta, $field);
        if (null === $newValue) {
            return;
        }

        $property = $meta->getReflectionProperty($field);
        $oldValue = $property->getValue($object);

        //if blame is reference, persist object
        if (is_object($newValue) && $meta->hasAssociation($field)) {
            $ea->getObjectManager()->persist($newValue);
        }

        $setter = 'set' . ucfirst($field);
        if (method_exists($object, $setter)) {
            $object->$setter($newValue);
        } else {
            $property->setValue($object, $newValue);
        }

        if ($object instanceof NotifyPropertyChanged) {
            $uow = $ea->getObjectManager()->getUnitOfWork();
{'fraction_non_alphanumeric': 0.141546980446444, 'fraction_numerical': 0.0031147257310953453, 'mean_word_length': 2.8303512259774686, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '1360848', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1711, 'n_words': 266}